blob: 24baad2571e314740f3d85f32292e9fac2072648 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Christoph Lameterb20a3502006-03-22 00:09:12 -08002/*
Hugh Dickins14e0f9b2015-11-05 18:49:43 -08003 * Memory Migration functionality - linux/mm/migrate.c
Christoph Lameterb20a3502006-03-22 00:09:12 -08004 *
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>
Christoph Lametercde53532008-07-04 09:59:22 -070013 * Christoph Lameter
Christoph Lameterb20a3502006-03-22 00:09:12 -080014 */
15
16#include <linux/migrate.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040017#include <linux/export.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080018#include <linux/swap.h>
Christoph Lameter06972122006-06-23 02:03:35 -070019#include <linux/swapops.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080020#include <linux/pagemap.h>
Christoph Lametere23ca002006-04-10 22:52:57 -070021#include <linux/buffer_head.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080022#include <linux/mm_inline.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070023#include <linux/nsproxy.h>
Hugh Dickinse9995ef2009-12-14 17:59:31 -080024#include <linux/ksm.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080025#include <linux/rmap.h>
26#include <linux/topology.h>
27#include <linux/cpu.h>
28#include <linux/cpuset.h>
Christoph Lameter04e62a22006-06-23 02:03:38 -070029#include <linux/writeback.h>
Christoph Lameter742755a2006-06-23 02:03:55 -070030#include <linux/mempolicy.h>
31#include <linux/vmalloc.h>
David Quigley86c3a762006-06-23 02:04:02 -070032#include <linux/security.h>
Hugh Dickins42cb14b2015-11-05 18:50:05 -080033#include <linux/backing-dev.h>
Minchan Kimbda807d2016-07-26 15:23:05 -070034#include <linux/compaction.h>
Adrian Bunk4f5ca262008-07-23 21:27:02 -070035#include <linux/syscalls.h>
Dominik Brodowski7addf442018-03-17 16:08:03 +010036#include <linux/compat.h>
Naoya Horiguchi290408d2010-09-08 10:19:35 +090037#include <linux/hugetlb.h>
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -070038#include <linux/hugetlb_cgroup.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/gfp.h>
Jérôme Glissedf6ad692017-09-08 16:12:24 -070040#include <linux/pfn_t.h>
Jérôme Glissea5430dd2017-09-08 16:12:17 -070041#include <linux/memremap.h>
Jérôme Glisse8315ada2017-09-08 16:12:21 -070042#include <linux/userfaultfd_k.h>
Rafael Aquinibf6bddf12012-12-11 16:02:42 -080043#include <linux/balloon_compaction.h>
Vladimir Davydov33c3fc72015-09-09 15:35:45 -070044#include <linux/page_idle.h>
Vlastimil Babkad435edc2016-03-15 14:56:15 -070045#include <linux/page_owner.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010046#include <linux/sched/mm.h>
Linus Torvalds197e7e52017-08-20 13:26:27 -070047#include <linux/ptrace.h>
Ralph Campbell34290e22020-01-30 22:14:44 -080048#include <linux/oom.h>
Dave Hansen884a6e52021-09-02 14:59:09 -070049#include <linux/memory.h>
Baolin Wangac16ec82022-01-14 14:08:43 -080050#include <linux/random.h>
Huang Yingc574bbe2022-03-22 14:46:23 -070051#include <linux/sched/sysctl.h>
Aneesh Kumar K.V467b1712022-08-18 18:40:41 +053052#include <linux/memory-tiers.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080053
Michal Nazarewicz0d1836c2010-12-21 17:24:26 -080054#include <asm/tlbflush.h>
55
Mel Gorman7b2a2d42012-10-19 14:07:31 +010056#include <trace/events/migrate.h>
57
Christoph Lameterb20a3502006-03-22 00:09:12 -080058#include "internal.h"
59
Baolin Wangcd775582023-02-15 18:39:37 +080060bool isolate_movable_page(struct page *page, isolate_mode_t mode)
Minchan Kimbda807d2016-07-26 15:23:05 -070061{
Vishal Moola (Oracle)19979492023-01-30 13:43:51 -080062 struct folio *folio = folio_get_nontail_page(page);
Matthew Wilcox (Oracle)68f27362022-06-07 15:38:48 -040063 const struct movable_operations *mops;
Minchan Kimbda807d2016-07-26 15:23:05 -070064
65 /*
66 * Avoid burning cycles with pages that are yet under __free_pages(),
67 * or just got freed under us.
68 *
69 * In case we 'win' a race for a movable page being freed under us and
70 * raise its refcount preventing __free_pages() from doing its job
71 * the put_page() at the end of this block will take care of
72 * release this page, thus avoiding a nasty leakage.
73 */
Vishal Moola (Oracle)19979492023-01-30 13:43:51 -080074 if (!folio)
Minchan Kimbda807d2016-07-26 15:23:05 -070075 goto out;
76
Vishal Moola (Oracle)19979492023-01-30 13:43:51 -080077 if (unlikely(folio_test_slab(folio)))
78 goto out_putfolio;
Vlastimil Babka8b881762022-11-04 15:57:26 +010079 /* Pairs with smp_wmb() in slab freeing, e.g. SLUB's __free_slab() */
80 smp_rmb();
Minchan Kimbda807d2016-07-26 15:23:05 -070081 /*
Vlastimil Babka8b881762022-11-04 15:57:26 +010082 * 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.
Minchan Kimbda807d2016-07-26 15:23:05 -070085 */
Vishal Moola (Oracle)19979492023-01-30 13:43:51 -080086 if (unlikely(!__folio_test_movable(folio)))
87 goto out_putfolio;
Vlastimil Babka8b881762022-11-04 15:57:26 +010088 /* Pairs with smp_wmb() in slab allocation, e.g. SLUB's alloc_slab_page() */
89 smp_rmb();
Vishal Moola (Oracle)19979492023-01-30 13:43:51 -080090 if (unlikely(folio_test_slab(folio)))
91 goto out_putfolio;
Vlastimil Babka8b881762022-11-04 15:57:26 +010092
Minchan Kimbda807d2016-07-26 15:23:05 -070093 /*
94 * As movable pages are not isolated from LRU lists, concurrent
95 * compaction threads can race against page migration functions
96 * as well as race against the releasing a page.
97 *
98 * In order to avoid having an already isolated movable page
99 * being (wrongly) re-isolated while it is under migration,
100 * or to avoid attempting to isolate pages being released,
101 * lets be sure we have the page lock
102 * before proceeding with the movable page isolation steps.
103 */
Vishal Moola (Oracle)19979492023-01-30 13:43:51 -0800104 if (unlikely(!folio_trylock(folio)))
105 goto out_putfolio;
Minchan Kimbda807d2016-07-26 15:23:05 -0700106
Vishal Moola (Oracle)19979492023-01-30 13:43:51 -0800107 if (!folio_test_movable(folio) || folio_test_isolated(folio))
Minchan Kimbda807d2016-07-26 15:23:05 -0700108 goto out_no_isolated;
109
Vishal Moola (Oracle)19979492023-01-30 13:43:51 -0800110 mops = folio_movable_ops(folio);
111 VM_BUG_ON_FOLIO(!mops, folio);
Minchan Kimbda807d2016-07-26 15:23:05 -0700112
Vishal Moola (Oracle)19979492023-01-30 13:43:51 -0800113 if (!mops->isolate_page(&folio->page, mode))
Minchan Kimbda807d2016-07-26 15:23:05 -0700114 goto out_no_isolated;
115
116 /* Driver shouldn't use PG_isolated bit of page->flags */
Vishal Moola (Oracle)19979492023-01-30 13:43:51 -0800117 WARN_ON_ONCE(folio_test_isolated(folio));
118 folio_set_isolated(folio);
119 folio_unlock(folio);
Minchan Kimbda807d2016-07-26 15:23:05 -0700120
Baolin Wangcd775582023-02-15 18:39:37 +0800121 return true;
Minchan Kimbda807d2016-07-26 15:23:05 -0700122
123out_no_isolated:
Vishal Moola (Oracle)19979492023-01-30 13:43:51 -0800124 folio_unlock(folio);
125out_putfolio:
126 folio_put(folio);
Minchan Kimbda807d2016-07-26 15:23:05 -0700127out:
Baolin Wangcd775582023-02-15 18:39:37 +0800128 return false;
Minchan Kimbda807d2016-07-26 15:23:05 -0700129}
130
Vishal Moola (Oracle)280d7242023-01-30 13:43:52 -0800131static void putback_movable_folio(struct folio *folio)
Minchan Kimbda807d2016-07-26 15:23:05 -0700132{
Vishal Moola (Oracle)280d7242023-01-30 13:43:52 -0800133 const struct movable_operations *mops = folio_movable_ops(folio);
Minchan Kimbda807d2016-07-26 15:23:05 -0700134
Vishal Moola (Oracle)280d7242023-01-30 13:43:52 -0800135 mops->putback_page(&folio->page);
136 folio_clear_isolated(folio);
Minchan Kimbda807d2016-07-26 15:23:05 -0700137}
138
Christoph Lameterb20a3502006-03-22 00:09:12 -0800139/*
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800140 * Put previously isolated pages back onto the appropriate lists
141 * from where they were once taken off for compaction/migration.
142 *
Joonsoo Kim59c82b72014-01-21 15:51:17 -0800143 * This function shall be used whenever the isolated pageset has been
144 * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range()
Miaohe Lin7ce82f42022-05-30 19:30:15 +0800145 * and isolate_hugetlb().
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800146 */
147void putback_movable_pages(struct list_head *l)
148{
Vishal Moola (Oracle)280d7242023-01-30 13:43:52 -0800149 struct folio *folio;
150 struct folio *folio2;
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800151
Vishal Moola (Oracle)280d7242023-01-30 13:43:52 -0800152 list_for_each_entry_safe(folio, folio2, l, lru) {
153 if (unlikely(folio_test_hugetlb(folio))) {
154 folio_putback_active_hugetlb(folio);
Naoya Horiguchi31caf662013-09-11 14:21:59 -0700155 continue;
156 }
Vishal Moola (Oracle)280d7242023-01-30 13:43:52 -0800157 list_del(&folio->lru);
Minchan Kimbda807d2016-07-26 15:23:05 -0700158 /*
Vishal Moola (Oracle)280d7242023-01-30 13:43:52 -0800159 * We isolated non-lru movable folio so here we can use
160 * __PageMovable because LRU folio's mapping cannot have
Minchan Kimbda807d2016-07-26 15:23:05 -0700161 * PAGE_MAPPING_MOVABLE.
162 */
Vishal Moola (Oracle)280d7242023-01-30 13:43:52 -0800163 if (unlikely(__folio_test_movable(folio))) {
164 VM_BUG_ON_FOLIO(!folio_test_isolated(folio), folio);
165 folio_lock(folio);
166 if (folio_test_movable(folio))
167 putback_movable_folio(folio);
Minchan Kimbda807d2016-07-26 15:23:05 -0700168 else
Vishal Moola (Oracle)280d7242023-01-30 13:43:52 -0800169 folio_clear_isolated(folio);
170 folio_unlock(folio);
171 folio_put(folio);
Minchan Kimbda807d2016-07-26 15:23:05 -0700172 } else {
Vishal Moola (Oracle)280d7242023-01-30 13:43:52 -0800173 node_stat_mod_folio(folio, NR_ISOLATED_ANON +
174 folio_is_file_lru(folio), -folio_nr_pages(folio));
175 folio_putback_lru(folio);
Minchan Kimbda807d2016-07-26 15:23:05 -0700176 }
Christoph Lameterb20a3502006-03-22 00:09:12 -0800177 }
Christoph Lameterb20a3502006-03-22 00:09:12 -0800178}
179
Christoph Lameter06972122006-06-23 02:03:35 -0700180/*
181 * Restore a potential migration pte to a working pte entry
182 */
Matthew Wilcox (Oracle)2f031c62022-01-29 16:06:53 -0500183static bool remove_migration_pte(struct folio *folio,
184 struct vm_area_struct *vma, unsigned long addr, void *old)
Christoph Lameter06972122006-06-23 02:03:35 -0700185{
Matthew Wilcox (Oracle)4eecb8b2022-01-28 23:32:59 -0500186 DEFINE_FOLIO_VMA_WALK(pvmw, old, vma, addr, PVMW_SYNC | PVMW_MIGRATION);
Christoph Lameter06972122006-06-23 02:03:35 -0700187
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800188 while (page_vma_mapped_walk(&pvmw)) {
David Hildenbrand6c287602022-05-09 18:20:44 -0700189 rmap_t rmap_flags = RMAP_NONE;
Ryan Robertsc33c7942023-06-12 16:15:45 +0100190 pte_t old_pte;
Matthew Wilcox (Oracle)4eecb8b2022-01-28 23:32:59 -0500191 pte_t pte;
192 swp_entry_t entry;
193 struct page *new;
194 unsigned long idx = 0;
195
196 /* pgoff is invalid for ksm pages, but they are never large */
197 if (folio_test_large(folio) && !folio_test_hugetlb(folio))
198 idx = linear_page_index(vma, pvmw.address) - pvmw.pgoff;
199 new = folio_page(folio, idx);
Christoph Lameter06972122006-06-23 02:03:35 -0700200
Zi Yan616b8372017-09-08 16:10:57 -0700201#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
202 /* PMD-mapped THP migration entry */
203 if (!pvmw.pte) {
Matthew Wilcox (Oracle)4eecb8b2022-01-28 23:32:59 -0500204 VM_BUG_ON_FOLIO(folio_test_hugetlb(folio) ||
205 !folio_test_pmd_mappable(folio), folio);
Zi Yan616b8372017-09-08 16:10:57 -0700206 remove_migration_pmd(&pvmw, new);
207 continue;
208 }
209#endif
210
Matthew Wilcox (Oracle)4eecb8b2022-01-28 23:32:59 -0500211 folio_get(folio);
Peter Xu2e346872022-08-11 12:13:29 -0400212 pte = mk_pte(new, READ_ONCE(vma->vm_page_prot));
Ryan Robertsc33c7942023-06-12 16:15:45 +0100213 old_pte = ptep_get(pvmw.pte);
214 if (pte_swp_soft_dirty(old_pte))
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800215 pte = pte_mksoft_dirty(pte);
Christoph Lameter06972122006-06-23 02:03:35 -0700216
Ryan Robertsc33c7942023-06-12 16:15:45 +0100217 entry = pte_to_swp_entry(old_pte);
Peter Xu2e346872022-08-11 12:13:29 -0400218 if (!is_migration_entry_young(entry))
219 pte = pte_mkold(pte);
220 if (folio_test_dirty(folio) && is_migration_entry_dirty(entry))
221 pte = pte_mkdirty(pte);
Alistair Popple4dd845b2021-06-30 18:54:09 -0700222 if (is_writable_migration_entry(entry))
David Hildenbrandf3ebdf02023-04-18 16:21:13 +0200223 pte = pte_mkwrite(pte);
Ryan Robertsc33c7942023-06-12 16:15:45 +0100224 else if (pte_swp_uffd_wp(old_pte))
Peter Xuf45ec5f2020-04-06 20:06:01 -0700225 pte = pte_mkuffd_wp(pte);
Mel Gormand3cb8bf2014-10-02 19:47:41 +0100226
David Hildenbrand6c287602022-05-09 18:20:44 -0700227 if (folio_test_anon(folio) && !is_readable_migration_entry(entry))
228 rmap_flags |= RMAP_EXCLUSIVE;
229
Ralph Campbell61287632020-09-04 16:36:04 -0700230 if (unlikely(is_device_private_page(new))) {
Alistair Popple4dd845b2021-06-30 18:54:09 -0700231 if (pte_write(pte))
232 entry = make_writable_device_private_entry(
233 page_to_pfn(new));
234 else
235 entry = make_readable_device_private_entry(
236 page_to_pfn(new));
Ralph Campbell61287632020-09-04 16:36:04 -0700237 pte = swp_entry_to_pte(entry);
Ryan Robertsc33c7942023-06-12 16:15:45 +0100238 if (pte_swp_soft_dirty(old_pte))
Ralph Campbell3d321bf82020-09-04 16:36:07 -0700239 pte = pte_swp_mksoft_dirty(pte);
Ryan Robertsc33c7942023-06-12 16:15:45 +0100240 if (pte_swp_uffd_wp(old_pte))
Ralph Campbell61287632020-09-04 16:36:04 -0700241 pte = pte_swp_mkuffd_wp(pte);
Lars Perssond2b2c6dd2019-03-28 20:44:28 -0700242 }
Jérôme Glissea5430dd2017-09-08 16:12:17 -0700243
Andi Kleen3ef8fd72010-10-11 16:03:21 +0200244#ifdef CONFIG_HUGETLB_PAGE
Matthew Wilcox (Oracle)4eecb8b2022-01-28 23:32:59 -0500245 if (folio_test_hugetlb(folio)) {
Christophe Leroy79c1c592021-06-30 18:48:00 -0700246 unsigned int shift = huge_page_shift(hstate_vma(vma));
247
Christophe Leroy79c1c592021-06-30 18:48:00 -0700248 pte = arch_make_huge_pte(pte, shift, vma->vm_flags);
Matthew Wilcox (Oracle)4eecb8b2022-01-28 23:32:59 -0500249 if (folio_test_anon(folio))
David Hildenbrand28c52092022-05-09 18:20:43 -0700250 hugepage_add_anon_rmap(new, vma, pvmw.address,
David Hildenbrand6c287602022-05-09 18:20:44 -0700251 rmap_flags);
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800252 else
David Hildenbrandfb3d8242022-05-09 18:20:43 -0700253 page_dup_file_rmap(new, true);
Pasha Tatashin1eba86c2022-01-14 14:06:29 -0800254 set_huge_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
Aneesh Kumar K.V383321a2017-07-06 15:38:41 -0700255 } else
256#endif
257 {
Matthew Wilcox (Oracle)4eecb8b2022-01-28 23:32:59 -0500258 if (folio_test_anon(folio))
David Hildenbrandf1e2db12022-05-09 18:20:43 -0700259 page_add_anon_rmap(new, vma, pvmw.address,
David Hildenbrand6c287602022-05-09 18:20:44 -0700260 rmap_flags);
Aneesh Kumar K.V383321a2017-07-06 15:38:41 -0700261 else
Hugh Dickinscea86fe2022-02-14 18:26:39 -0800262 page_add_file_rmap(new, vma, false);
Pasha Tatashin1eba86c2022-01-14 14:06:29 -0800263 set_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
Aneesh Kumar K.V383321a2017-07-06 15:38:41 -0700264 }
Hugh Dickinsb7435502022-02-14 18:38:47 -0800265 if (vma->vm_flags & VM_LOCKED)
Lorenzo Stoakes96f97c42023-01-12 12:39:31 +0000266 mlock_drain_local();
Kirill A. Shutemove125fe42018-10-05 15:51:41 -0700267
Anshuman Khandual4cc79b32022-03-24 18:10:01 -0700268 trace_remove_migration_pte(pvmw.address, pte_val(pte),
269 compound_order(new));
270
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800271 /* No need to invalidate - it was non-present before */
272 update_mmu_cache(vma, pvmw.address, pvmw.pte);
273 }
274
Minchan Kime4b82222017-05-03 14:54:27 -0700275 return true;
Christoph Lameter06972122006-06-23 02:03:35 -0700276}
277
278/*
Christoph Lameter04e62a22006-06-23 02:03:38 -0700279 * Get rid of all migration entries and replace them by
280 * references to the indicated page.
281 */
Matthew Wilcox (Oracle)4eecb8b2022-01-28 23:32:59 -0500282void remove_migration_ptes(struct folio *src, struct folio *dst, bool locked)
Christoph Lameter04e62a22006-06-23 02:03:38 -0700283{
Joonsoo Kim051ac832014-01-21 15:49:48 -0800284 struct rmap_walk_control rwc = {
285 .rmap_one = remove_migration_pte,
Matthew Wilcox (Oracle)4eecb8b2022-01-28 23:32:59 -0500286 .arg = src,
Joonsoo Kim051ac832014-01-21 15:49:48 -0800287 };
288
Kirill A. Shutemove3884662016-03-17 14:20:07 -0700289 if (locked)
Matthew Wilcox (Oracle)2f031c62022-01-29 16:06:53 -0500290 rmap_walk_locked(dst, &rwc);
Kirill A. Shutemove3884662016-03-17 14:20:07 -0700291 else
Matthew Wilcox (Oracle)2f031c62022-01-29 16:06:53 -0500292 rmap_walk(dst, &rwc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700293}
294
295/*
Christoph Lameter06972122006-06-23 02:03:35 -0700296 * Something used the pte of a page under migration. We need to
297 * get to the page and wait until migration is finished.
298 * When we return from this function the fault will be retried.
Christoph Lameter06972122006-06-23 02:03:35 -0700299 */
Hugh Dickins0cb8fd42023-06-08 18:08:20 -0700300void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
301 unsigned long address)
Christoph Lameter06972122006-06-23 02:03:35 -0700302{
Hugh Dickins0cb8fd42023-06-08 18:08:20 -0700303 spinlock_t *ptl;
304 pte_t *ptep;
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700305 pte_t pte;
Christoph Lameter06972122006-06-23 02:03:35 -0700306 swp_entry_t entry;
Christoph Lameter06972122006-06-23 02:03:35 -0700307
Hugh Dickins0cb8fd42023-06-08 18:08:20 -0700308 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
Hugh Dickins04dee9e2023-06-08 18:29:22 -0700309 if (!ptep)
310 return;
311
Ryan Robertsc33c7942023-06-12 16:15:45 +0100312 pte = ptep_get(ptep);
Hugh Dickins0cb8fd42023-06-08 18:08:20 -0700313 pte_unmap(ptep);
314
Christoph Lameter06972122006-06-23 02:03:35 -0700315 if (!is_swap_pte(pte))
316 goto out;
317
318 entry = pte_to_swp_entry(pte);
319 if (!is_migration_entry(entry))
320 goto out;
321
Hugh Dickins0cb8fd42023-06-08 18:08:20 -0700322 migration_entry_wait_on_locked(entry, ptl);
Christoph Lameter06972122006-06-23 02:03:35 -0700323 return;
324out:
Hugh Dickins0cb8fd42023-06-08 18:08:20 -0700325 spin_unlock(ptl);
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700326}
327
Miaohe Linad1ac592022-05-30 19:30:16 +0800328#ifdef CONFIG_HUGETLB_PAGE
Peter Xufcd48542022-12-16 10:50:55 -0500329/*
330 * The vma read lock must be held upon entry. Holding that lock prevents either
331 * the pte or the ptl from being freed.
332 *
333 * This function will release the vma lock before returning.
334 */
Hugh Dickins0cb8fd42023-06-08 18:08:20 -0700335void migration_entry_wait_huge(struct vm_area_struct *vma, pte_t *ptep)
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700336{
Hugh Dickins0cb8fd42023-06-08 18:08:20 -0700337 spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), vma->vm_mm, ptep);
Miaohe Linad1ac592022-05-30 19:30:16 +0800338 pte_t pte;
339
Peter Xufcd48542022-12-16 10:50:55 -0500340 hugetlb_vma_assert_locked(vma);
Miaohe Linad1ac592022-05-30 19:30:16 +0800341 spin_lock(ptl);
342 pte = huge_ptep_get(ptep);
343
Peter Xufcd48542022-12-16 10:50:55 -0500344 if (unlikely(!is_hugetlb_entry_migration(pte))) {
Miaohe Linad1ac592022-05-30 19:30:16 +0800345 spin_unlock(ptl);
Peter Xufcd48542022-12-16 10:50:55 -0500346 hugetlb_vma_unlock_read(vma);
347 } else {
348 /*
349 * If migration entry existed, safe to release vma lock
350 * here because the pgtable page won't be freed without the
351 * pgtable lock released. See comment right above pgtable
352 * lock release in migration_entry_wait_on_locked().
353 */
354 hugetlb_vma_unlock_read(vma);
Hugh Dickins0cb8fd42023-06-08 18:08:20 -0700355 migration_entry_wait_on_locked(pte_to_swp_entry(pte), ptl);
Peter Xufcd48542022-12-16 10:50:55 -0500356 }
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700357}
Miaohe Linad1ac592022-05-30 19:30:16 +0800358#endif
359
Zi Yan616b8372017-09-08 16:10:57 -0700360#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
361void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd)
362{
363 spinlock_t *ptl;
Zi Yan616b8372017-09-08 16:10:57 -0700364
365 ptl = pmd_lock(mm, pmd);
366 if (!is_pmd_migration_entry(*pmd))
367 goto unlock;
Hugh Dickins0cb8fd42023-06-08 18:08:20 -0700368 migration_entry_wait_on_locked(pmd_to_swp_entry(*pmd), ptl);
Zi Yan616b8372017-09-08 16:10:57 -0700369 return;
370unlock:
371 spin_unlock(ptl);
372}
373#endif
374
Matthew Wilcox (Oracle)108ca832022-06-06 16:25:10 -0400375static int folio_expected_refs(struct address_space *mapping,
376 struct folio *folio)
Jan Kara0b3901b2018-12-28 00:39:01 -0800377{
Matthew Wilcox (Oracle)108ca832022-06-06 16:25:10 -0400378 int refs = 1;
379 if (!mapping)
380 return refs;
Jan Kara0b3901b2018-12-28 00:39:01 -0800381
Matthew Wilcox (Oracle)108ca832022-06-06 16:25:10 -0400382 refs += folio_nr_pages(folio);
383 if (folio_test_private(folio))
384 refs++;
385
386 return refs;
Jan Kara0b3901b2018-12-28 00:39:01 -0800387}
388
Christoph Lameterb20a3502006-03-22 00:09:12 -0800389/*
Christoph Lameterc3fcf8a2006-06-23 02:03:32 -0700390 * Replace the page in the mapping.
Christoph Lameter5b5c7122006-06-23 02:03:29 -0700391 *
392 * The number of remaining references must be:
393 * 1 for anonymous pages without a mapping
394 * 2 for pages with a mapping
David Howells266cf652009-04-03 16:42:36 +0100395 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800396 */
Matthew Wilcox (Oracle)34170132021-05-07 07:28:40 -0400397int folio_migrate_mapping(struct address_space *mapping,
398 struct folio *newfolio, struct folio *folio, int extra_count)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800399{
Matthew Wilcox (Oracle)34170132021-05-07 07:28:40 -0400400 XA_STATE(xas, &mapping->i_pages, folio_index(folio));
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800401 struct zone *oldzone, *newzone;
402 int dirty;
Matthew Wilcox (Oracle)108ca832022-06-06 16:25:10 -0400403 int expected_count = folio_expected_refs(mapping, folio) + extra_count;
Matthew Wilcox (Oracle)34170132021-05-07 07:28:40 -0400404 long nr = folio_nr_pages(folio);
Jérôme Glisse8763cb42017-09-08 16:12:09 -0700405
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700406 if (!mapping) {
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700407 /* Anonymous page without mapping */
Matthew Wilcox (Oracle)34170132021-05-07 07:28:40 -0400408 if (folio_ref_count(folio) != expected_count)
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700409 return -EAGAIN;
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800410
411 /* No turning back from here */
Matthew Wilcox (Oracle)34170132021-05-07 07:28:40 -0400412 newfolio->index = folio->index;
413 newfolio->mapping = folio->mapping;
414 if (folio_test_swapbacked(folio))
415 __folio_set_swapbacked(newfolio);
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800416
Rafael Aquini78bd5202012-12-11 16:02:31 -0800417 return MIGRATEPAGE_SUCCESS;
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700418 }
419
Matthew Wilcox (Oracle)34170132021-05-07 07:28:40 -0400420 oldzone = folio_zone(folio);
421 newzone = folio_zone(newfolio);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800422
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500423 xas_lock_irq(&xas);
Matthew Wilcox (Oracle)34170132021-05-07 07:28:40 -0400424 if (!folio_ref_freeze(folio, expected_count)) {
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500425 xas_unlock_irq(&xas);
Nick Piggine2867812008-07-25 19:45:30 -0700426 return -EAGAIN;
427 }
428
Christoph Lameterb20a3502006-03-22 00:09:12 -0800429 /*
Matthew Wilcox (Oracle)34170132021-05-07 07:28:40 -0400430 * Now we know that no one else is looking at the folio:
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800431 * no turning back from here.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800432 */
Matthew Wilcox (Oracle)34170132021-05-07 07:28:40 -0400433 newfolio->index = folio->index;
434 newfolio->mapping = folio->mapping;
435 folio_ref_add(newfolio, nr); /* add cache reference */
436 if (folio_test_swapbacked(folio)) {
437 __folio_set_swapbacked(newfolio);
438 if (folio_test_swapcache(folio)) {
439 folio_set_swapcache(newfolio);
440 newfolio->private = folio_get_private(folio);
Nicholas Piggin6326fec2016-12-25 13:00:29 +1000441 }
442 } else {
Matthew Wilcox (Oracle)34170132021-05-07 07:28:40 -0400443 VM_BUG_ON_FOLIO(folio_test_swapcache(folio), folio);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800444 }
445
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800446 /* Move dirty while page refs frozen and newpage not yet exposed */
Matthew Wilcox (Oracle)34170132021-05-07 07:28:40 -0400447 dirty = folio_test_dirty(folio);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800448 if (dirty) {
Matthew Wilcox (Oracle)34170132021-05-07 07:28:40 -0400449 folio_clear_dirty(folio);
450 folio_set_dirty(newfolio);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800451 }
452
Matthew Wilcox (Oracle)34170132021-05-07 07:28:40 -0400453 xas_store(&xas, newfolio);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800454
455 /*
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800456 * Drop cache reference from old page by unfreezing
457 * to one less reference.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800458 * We know this isn't the last reference.
459 */
Matthew Wilcox (Oracle)34170132021-05-07 07:28:40 -0400460 folio_ref_unfreeze(folio, expected_count - nr);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800461
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500462 xas_unlock(&xas);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800463 /* Leave irq disabled to prevent preemption while updating stats */
464
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700465 /*
466 * If moved to a different zone then also account
467 * the page for that zone. Other VM counters will be
468 * taken care of when we establish references to the
469 * new page and drop references to the old page.
470 *
471 * Note that anonymous pages are accounted for
Mel Gorman4b9d0fa2016-07-28 15:46:17 -0700472 * via NR_FILE_PAGES and NR_ANON_MAPPED if they
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700473 * are mapped to swap space.
474 */
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800475 if (newzone != oldzone) {
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700476 struct lruvec *old_lruvec, *new_lruvec;
477 struct mem_cgroup *memcg;
478
Matthew Wilcox (Oracle)34170132021-05-07 07:28:40 -0400479 memcg = folio_memcg(folio);
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700480 old_lruvec = mem_cgroup_lruvec(memcg, oldzone->zone_pgdat);
481 new_lruvec = mem_cgroup_lruvec(memcg, newzone->zone_pgdat);
482
Shakeel Butt5c447d22021-01-23 21:01:15 -0800483 __mod_lruvec_state(old_lruvec, NR_FILE_PAGES, -nr);
484 __mod_lruvec_state(new_lruvec, NR_FILE_PAGES, nr);
Matthew Wilcox (Oracle)34170132021-05-07 07:28:40 -0400485 if (folio_test_swapbacked(folio) && !folio_test_swapcache(folio)) {
Shakeel Butt5c447d22021-01-23 21:01:15 -0800486 __mod_lruvec_state(old_lruvec, NR_SHMEM, -nr);
487 __mod_lruvec_state(new_lruvec, NR_SHMEM, nr);
Jan Glauber0b52c422023-06-19 12:33:51 +0200488
489 if (folio_test_pmd_mappable(folio)) {
490 __mod_lruvec_state(old_lruvec, NR_SHMEM_THPS, -nr);
491 __mod_lruvec_state(new_lruvec, NR_SHMEM_THPS, nr);
492 }
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800493 }
Shakeel Buttb6038942021-02-24 12:03:55 -0800494#ifdef CONFIG_SWAP
Matthew Wilcox (Oracle)34170132021-05-07 07:28:40 -0400495 if (folio_test_swapcache(folio)) {
Shakeel Buttb6038942021-02-24 12:03:55 -0800496 __mod_lruvec_state(old_lruvec, NR_SWAPCACHE, -nr);
497 __mod_lruvec_state(new_lruvec, NR_SWAPCACHE, nr);
498 }
499#endif
Christoph Hellwigf56753a2020-09-24 08:51:40 +0200500 if (dirty && mapping_can_writeback(mapping)) {
Shakeel Butt5c447d22021-01-23 21:01:15 -0800501 __mod_lruvec_state(old_lruvec, NR_FILE_DIRTY, -nr);
502 __mod_zone_page_state(oldzone, NR_ZONE_WRITE_PENDING, -nr);
503 __mod_lruvec_state(new_lruvec, NR_FILE_DIRTY, nr);
504 __mod_zone_page_state(newzone, NR_ZONE_WRITE_PENDING, nr);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800505 }
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700506 }
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800507 local_irq_enable();
Christoph Lameterb20a3502006-03-22 00:09:12 -0800508
Rafael Aquini78bd5202012-12-11 16:02:31 -0800509 return MIGRATEPAGE_SUCCESS;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800510}
Matthew Wilcox (Oracle)34170132021-05-07 07:28:40 -0400511EXPORT_SYMBOL(folio_migrate_mapping);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800512
513/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900514 * The expected number of remaining references is the same as that
Matthew Wilcox (Oracle)34170132021-05-07 07:28:40 -0400515 * of folio_migrate_mapping().
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900516 */
517int migrate_huge_page_move_mapping(struct address_space *mapping,
Matthew Wilcox (Oracle)b890ec22022-06-06 10:47:21 -0400518 struct folio *dst, struct folio *src)
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900519{
Matthew Wilcox (Oracle)b890ec22022-06-06 10:47:21 -0400520 XA_STATE(xas, &mapping->i_pages, folio_index(src));
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900521 int expected_count;
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900522
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500523 xas_lock_irq(&xas);
Matthew Wilcox (Oracle)b890ec22022-06-06 10:47:21 -0400524 expected_count = 2 + folio_has_private(src);
525 if (!folio_ref_freeze(src, expected_count)) {
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500526 xas_unlock_irq(&xas);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900527 return -EAGAIN;
528 }
529
Matthew Wilcox (Oracle)b890ec22022-06-06 10:47:21 -0400530 dst->index = src->index;
531 dst->mapping = src->mapping;
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700532
Matthew Wilcox (Oracle)b890ec22022-06-06 10:47:21 -0400533 folio_get(dst);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900534
Matthew Wilcox (Oracle)b890ec22022-06-06 10:47:21 -0400535 xas_store(&xas, dst);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900536
Matthew Wilcox (Oracle)b890ec22022-06-06 10:47:21 -0400537 folio_ref_unfreeze(src, expected_count - 1);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900538
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500539 xas_unlock_irq(&xas);
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700540
Rafael Aquini78bd5202012-12-11 16:02:31 -0800541 return MIGRATEPAGE_SUCCESS;
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900542}
543
544/*
Matthew Wilcox (Oracle)19138342021-05-07 15:26:29 -0400545 * Copy the flags and some other ancillary information
Christoph Lameterb20a3502006-03-22 00:09:12 -0800546 */
Matthew Wilcox (Oracle)19138342021-05-07 15:26:29 -0400547void folio_migrate_flags(struct folio *newfolio, struct folio *folio)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800548{
Rik van Riel7851a452013-10-07 11:29:23 +0100549 int cpupid;
550
Matthew Wilcox (Oracle)19138342021-05-07 15:26:29 -0400551 if (folio_test_error(folio))
552 folio_set_error(newfolio);
553 if (folio_test_referenced(folio))
554 folio_set_referenced(newfolio);
555 if (folio_test_uptodate(folio))
556 folio_mark_uptodate(newfolio);
557 if (folio_test_clear_active(folio)) {
558 VM_BUG_ON_FOLIO(folio_test_unevictable(folio), folio);
559 folio_set_active(newfolio);
560 } else if (folio_test_clear_unevictable(folio))
561 folio_set_unevictable(newfolio);
562 if (folio_test_workingset(folio))
563 folio_set_workingset(newfolio);
564 if (folio_test_checked(folio))
565 folio_set_checked(newfolio);
David Hildenbrand6c287602022-05-09 18:20:44 -0700566 /*
567 * PG_anon_exclusive (-> PG_mappedtodisk) is always migrated via
568 * migration entries. We can still have PG_anon_exclusive set on an
569 * effectively unmapped and unreferenced first sub-pages of an
570 * anonymous THP: we can simply copy it here via PG_mappedtodisk.
571 */
Matthew Wilcox (Oracle)19138342021-05-07 15:26:29 -0400572 if (folio_test_mappedtodisk(folio))
573 folio_set_mappedtodisk(newfolio);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800574
Matthew Wilcox (Oracle)34170132021-05-07 07:28:40 -0400575 /* Move dirty on pages not done by folio_migrate_mapping() */
Matthew Wilcox (Oracle)19138342021-05-07 15:26:29 -0400576 if (folio_test_dirty(folio))
577 folio_set_dirty(newfolio);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800578
Matthew Wilcox (Oracle)19138342021-05-07 15:26:29 -0400579 if (folio_test_young(folio))
580 folio_set_young(newfolio);
581 if (folio_test_idle(folio))
582 folio_set_idle(newfolio);
Vladimir Davydov33c3fc72015-09-09 15:35:45 -0700583
Rik van Riel7851a452013-10-07 11:29:23 +0100584 /*
585 * Copy NUMA information to the new page, to prevent over-eager
586 * future migrations of this same page.
587 */
Matthew Wilcox (Oracle)19138342021-05-07 15:26:29 -0400588 cpupid = page_cpupid_xchg_last(&folio->page, -1);
Huang Ying33024532022-07-13 16:39:51 +0800589 /*
590 * For memory tiering mode, when migrate between slow and fast
591 * memory node, reset cpupid, because that is used to record
592 * page access time in slow memory node.
593 */
594 if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) {
595 bool f_toptier = node_is_toptier(page_to_nid(&folio->page));
596 bool t_toptier = node_is_toptier(page_to_nid(&newfolio->page));
597
598 if (f_toptier != t_toptier)
599 cpupid = -1;
600 }
Matthew Wilcox (Oracle)19138342021-05-07 15:26:29 -0400601 page_cpupid_xchg_last(&newfolio->page, cpupid);
Rik van Riel7851a452013-10-07 11:29:23 +0100602
Matthew Wilcox (Oracle)19138342021-05-07 15:26:29 -0400603 folio_migrate_ksm(newfolio, folio);
Hugh Dickinsc8d65532013-02-22 16:35:10 -0800604 /*
605 * Please do not reorder this without considering how mm/ksm.c's
606 * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache().
607 */
Matthew Wilcox (Oracle)19138342021-05-07 15:26:29 -0400608 if (folio_test_swapcache(folio))
609 folio_clear_swapcache(folio);
610 folio_clear_private(folio);
Muchun Songad2fa372021-06-30 18:47:21 -0700611
612 /* page->private contains hugetlb specific flags */
Matthew Wilcox (Oracle)19138342021-05-07 15:26:29 -0400613 if (!folio_test_hugetlb(folio))
614 folio->private = NULL;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800615
616 /*
617 * If any waiters have accumulated on the new page then
618 * wake them up.
619 */
Matthew Wilcox (Oracle)19138342021-05-07 15:26:29 -0400620 if (folio_test_writeback(newfolio))
621 folio_end_writeback(newfolio);
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700622
Yang Shi6aeff242020-04-06 20:04:21 -0700623 /*
624 * PG_readahead shares the same bit with PG_reclaim. The above
625 * end_page_writeback() may clear PG_readahead mistakenly, so set the
626 * bit after that.
627 */
Matthew Wilcox (Oracle)19138342021-05-07 15:26:29 -0400628 if (folio_test_readahead(folio))
629 folio_set_readahead(newfolio);
Yang Shi6aeff242020-04-06 20:04:21 -0700630
Matthew Wilcox (Oracle)19138342021-05-07 15:26:29 -0400631 folio_copy_owner(newfolio, folio);
Johannes Weiner74485cf2016-03-15 14:57:54 -0700632
Matthew Wilcox (Oracle)19138342021-05-07 15:26:29 -0400633 if (!folio_test_hugetlb(folio))
Matthew Wilcox (Oracle)d21bba22021-05-06 18:14:59 -0400634 mem_cgroup_migrate(folio, newfolio);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800635}
Matthew Wilcox (Oracle)19138342021-05-07 15:26:29 -0400636EXPORT_SYMBOL(folio_migrate_flags);
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700637
Matthew Wilcox (Oracle)715cbfd2021-05-07 15:05:06 -0400638void folio_migrate_copy(struct folio *newfolio, struct folio *folio)
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700639{
Matthew Wilcox (Oracle)715cbfd2021-05-07 15:05:06 -0400640 folio_copy(newfolio, folio);
641 folio_migrate_flags(newfolio, folio);
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700642}
Matthew Wilcox (Oracle)715cbfd2021-05-07 15:05:06 -0400643EXPORT_SYMBOL(folio_migrate_copy);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800644
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700645/************************************************************
646 * Migration functions
647 ***********************************************************/
648
Alistair Popple16ce1012022-09-28 22:01:15 +1000649int migrate_folio_extra(struct address_space *mapping, struct folio *dst,
650 struct folio *src, enum migrate_mode mode, int extra_count)
651{
652 int rc;
653
654 BUG_ON(folio_test_writeback(src)); /* Writeback must be complete */
655
656 rc = folio_migrate_mapping(mapping, dst, src, extra_count);
657
658 if (rc != MIGRATEPAGE_SUCCESS)
659 return rc;
660
661 if (mode != MIGRATE_SYNC_NO_COPY)
662 folio_migrate_copy(dst, src);
663 else
664 folio_migrate_flags(dst, src);
665 return MIGRATEPAGE_SUCCESS;
666}
667
Matthew Wilcox (Oracle)54184652022-06-06 10:27:41 -0400668/**
669 * migrate_folio() - Simple folio migration.
670 * @mapping: The address_space containing the folio.
671 * @dst: The folio to migrate the data to.
672 * @src: The folio containing the current data.
673 * @mode: How to migrate the page.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800674 *
Matthew Wilcox (Oracle)54184652022-06-06 10:27:41 -0400675 * Common logic to directly migrate a single LRU folio suitable for
676 * folios that do not use PagePrivate/PagePrivate2.
677 *
678 * Folios are locked upon entry and exit.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800679 */
Matthew Wilcox (Oracle)54184652022-06-06 10:27:41 -0400680int migrate_folio(struct address_space *mapping, struct folio *dst,
681 struct folio *src, enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800682{
Alistair Popple16ce1012022-09-28 22:01:15 +1000683 return migrate_folio_extra(mapping, dst, src, mode, 0);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800684}
Matthew Wilcox (Oracle)54184652022-06-06 10:27:41 -0400685EXPORT_SYMBOL(migrate_folio);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800686
David Howells93614012006-09-30 20:45:40 +0200687#ifdef CONFIG_BLOCK
Jan Kara84ade7c2018-12-28 00:39:09 -0800688/* Returns true if all buffers are successfully locked */
689static bool buffer_migrate_lock_buffers(struct buffer_head *head,
690 enum migrate_mode mode)
691{
692 struct buffer_head *bh = head;
Douglas Anderson4bb6dc72023-04-28 13:54:38 -0700693 struct buffer_head *failed_bh;
Jan Kara84ade7c2018-12-28 00:39:09 -0800694
Jan Kara84ade7c2018-12-28 00:39:09 -0800695 do {
Jan Kara84ade7c2018-12-28 00:39:09 -0800696 if (!trylock_buffer(bh)) {
Douglas Anderson4bb6dc72023-04-28 13:54:38 -0700697 if (mode == MIGRATE_ASYNC)
698 goto unlock;
699 if (mode == MIGRATE_SYNC_LIGHT && !buffer_uptodate(bh))
700 goto unlock;
701 lock_buffer(bh);
Jan Kara84ade7c2018-12-28 00:39:09 -0800702 }
703
704 bh = bh->b_this_page;
705 } while (bh != head);
Douglas Anderson4bb6dc72023-04-28 13:54:38 -0700706
Jan Kara84ade7c2018-12-28 00:39:09 -0800707 return true;
Douglas Anderson4bb6dc72023-04-28 13:54:38 -0700708
709unlock:
710 /* We failed to lock the buffer and cannot stall. */
711 failed_bh = bh;
712 bh = head;
713 while (bh != failed_bh) {
714 unlock_buffer(bh);
715 bh = bh->b_this_page;
716 }
717
718 return false;
Jan Kara84ade7c2018-12-28 00:39:09 -0800719}
720
Matthew Wilcox (Oracle)67235182022-06-06 10:20:31 -0400721static int __buffer_migrate_folio(struct address_space *mapping,
722 struct folio *dst, struct folio *src, enum migrate_mode mode,
Jan Kara89cb0882018-12-28 00:39:12 -0800723 bool check_refs)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700724{
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700725 struct buffer_head *bh, *head;
726 int rc;
Jan Karacc4f11e2018-12-28 00:39:05 -0800727 int expected_count;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700728
Matthew Wilcox (Oracle)67235182022-06-06 10:20:31 -0400729 head = folio_buffers(src);
730 if (!head)
Matthew Wilcox (Oracle)54184652022-06-06 10:27:41 -0400731 return migrate_folio(mapping, dst, src, mode);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700732
Jan Karacc4f11e2018-12-28 00:39:05 -0800733 /* Check whether page does not have extra refs before we do more work */
Matthew Wilcox (Oracle)108ca832022-06-06 16:25:10 -0400734 expected_count = folio_expected_refs(mapping, src);
Matthew Wilcox (Oracle)67235182022-06-06 10:20:31 -0400735 if (folio_ref_count(src) != expected_count)
Jan Karacc4f11e2018-12-28 00:39:05 -0800736 return -EAGAIN;
737
Jan Karacc4f11e2018-12-28 00:39:05 -0800738 if (!buffer_migrate_lock_buffers(head, mode))
739 return -EAGAIN;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700740
Jan Kara89cb0882018-12-28 00:39:12 -0800741 if (check_refs) {
742 bool busy;
743 bool invalidated = false;
744
745recheck_buffers:
746 busy = false;
747 spin_lock(&mapping->private_lock);
748 bh = head;
749 do {
750 if (atomic_read(&bh->b_count)) {
751 busy = true;
752 break;
753 }
754 bh = bh->b_this_page;
755 } while (bh != head);
Jan Kara89cb0882018-12-28 00:39:12 -0800756 if (busy) {
757 if (invalidated) {
758 rc = -EAGAIN;
759 goto unlock_buffers;
760 }
Jan Karaebdf4de2019-08-02 21:48:47 -0700761 spin_unlock(&mapping->private_lock);
Jan Kara89cb0882018-12-28 00:39:12 -0800762 invalidate_bh_lrus();
763 invalidated = true;
764 goto recheck_buffers;
765 }
766 }
767
Matthew Wilcox (Oracle)67235182022-06-06 10:20:31 -0400768 rc = folio_migrate_mapping(mapping, dst, src, 0);
Rafael Aquini78bd5202012-12-11 16:02:31 -0800769 if (rc != MIGRATEPAGE_SUCCESS)
Jan Karacc4f11e2018-12-28 00:39:05 -0800770 goto unlock_buffers;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700771
Matthew Wilcox (Oracle)67235182022-06-06 10:20:31 -0400772 folio_attach_private(dst, folio_detach_private(src));
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700773
774 bh = head;
775 do {
Matthew Wilcox (Oracle)67235182022-06-06 10:20:31 -0400776 set_bh_page(bh, &dst->page, bh_offset(bh));
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700777 bh = bh->b_this_page;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700778 } while (bh != head);
779
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700780 if (mode != MIGRATE_SYNC_NO_COPY)
Matthew Wilcox (Oracle)67235182022-06-06 10:20:31 -0400781 folio_migrate_copy(dst, src);
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700782 else
Matthew Wilcox (Oracle)67235182022-06-06 10:20:31 -0400783 folio_migrate_flags(dst, src);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700784
Jan Karacc4f11e2018-12-28 00:39:05 -0800785 rc = MIGRATEPAGE_SUCCESS;
786unlock_buffers:
Jan Karaebdf4de2019-08-02 21:48:47 -0700787 if (check_refs)
788 spin_unlock(&mapping->private_lock);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700789 bh = head;
790 do {
791 unlock_buffer(bh);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700792 bh = bh->b_this_page;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700793 } while (bh != head);
794
Jan Karacc4f11e2018-12-28 00:39:05 -0800795 return rc;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700796}
Jan Kara89cb0882018-12-28 00:39:12 -0800797
Matthew Wilcox (Oracle)67235182022-06-06 10:20:31 -0400798/**
799 * buffer_migrate_folio() - Migration function for folios with buffers.
800 * @mapping: The address space containing @src.
801 * @dst: The folio to migrate to.
802 * @src: The folio to migrate from.
803 * @mode: How to migrate the folio.
804 *
805 * This function can only be used if the underlying filesystem guarantees
806 * that no other references to @src exist. For example attached buffer
807 * heads are accessed only under the folio lock. If your filesystem cannot
808 * provide this guarantee, buffer_migrate_folio_norefs() may be more
809 * appropriate.
810 *
811 * Return: 0 on success or a negative errno on failure.
Jan Kara89cb0882018-12-28 00:39:12 -0800812 */
Matthew Wilcox (Oracle)67235182022-06-06 10:20:31 -0400813int buffer_migrate_folio(struct address_space *mapping,
814 struct folio *dst, struct folio *src, enum migrate_mode mode)
Jan Kara89cb0882018-12-28 00:39:12 -0800815{
Matthew Wilcox (Oracle)67235182022-06-06 10:20:31 -0400816 return __buffer_migrate_folio(mapping, dst, src, mode, false);
Jan Kara89cb0882018-12-28 00:39:12 -0800817}
Matthew Wilcox (Oracle)67235182022-06-06 10:20:31 -0400818EXPORT_SYMBOL(buffer_migrate_folio);
Jan Kara89cb0882018-12-28 00:39:12 -0800819
Matthew Wilcox (Oracle)67235182022-06-06 10:20:31 -0400820/**
821 * buffer_migrate_folio_norefs() - Migration function for folios with buffers.
822 * @mapping: The address space containing @src.
823 * @dst: The folio to migrate to.
824 * @src: The folio to migrate from.
825 * @mode: How to migrate the folio.
826 *
827 * Like buffer_migrate_folio() except that this variant is more careful
828 * and checks that there are also no buffer head references. This function
829 * is the right one for mappings where buffer heads are directly looked
830 * up and referenced (such as block device mappings).
831 *
832 * Return: 0 on success or a negative errno on failure.
Jan Kara89cb0882018-12-28 00:39:12 -0800833 */
Matthew Wilcox (Oracle)67235182022-06-06 10:20:31 -0400834int buffer_migrate_folio_norefs(struct address_space *mapping,
835 struct folio *dst, struct folio *src, enum migrate_mode mode)
Jan Kara89cb0882018-12-28 00:39:12 -0800836{
Matthew Wilcox (Oracle)67235182022-06-06 10:20:31 -0400837 return __buffer_migrate_folio(mapping, dst, src, mode, true);
Jan Kara89cb0882018-12-28 00:39:12 -0800838}
Jan Karae26355e2022-12-07 12:27:14 +0100839EXPORT_SYMBOL_GPL(buffer_migrate_folio_norefs);
David Howells93614012006-09-30 20:45:40 +0200840#endif
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700841
Matthew Wilcox (Oracle)2ec810d2022-06-06 12:55:08 -0400842int filemap_migrate_folio(struct address_space *mapping,
843 struct folio *dst, struct folio *src, enum migrate_mode mode)
844{
845 int ret;
846
847 ret = folio_migrate_mapping(mapping, dst, src, 0);
848 if (ret != MIGRATEPAGE_SUCCESS)
849 return ret;
850
851 if (folio_get_private(src))
852 folio_attach_private(dst, folio_detach_private(src));
853
854 if (mode != MIGRATE_SYNC_NO_COPY)
855 folio_migrate_copy(dst, src);
856 else
857 folio_migrate_flags(dst, src);
858 return MIGRATEPAGE_SUCCESS;
859}
860EXPORT_SYMBOL_GPL(filemap_migrate_folio);
861
Christoph Lameter04e62a22006-06-23 02:03:38 -0700862/*
Matthew Wilcox (Oracle)2be7fa12022-06-06 09:41:03 -0400863 * Writeback a folio to clean the dirty state
Christoph Lameter04e62a22006-06-23 02:03:38 -0700864 */
Matthew Wilcox (Oracle)2be7fa12022-06-06 09:41:03 -0400865static int writeout(struct address_space *mapping, struct folio *folio)
Christoph Lameter04e62a22006-06-23 02:03:38 -0700866{
867 struct writeback_control wbc = {
868 .sync_mode = WB_SYNC_NONE,
869 .nr_to_write = 1,
870 .range_start = 0,
871 .range_end = LLONG_MAX,
Christoph Lameter04e62a22006-06-23 02:03:38 -0700872 .for_reclaim = 1
873 };
874 int rc;
875
876 if (!mapping->a_ops->writepage)
877 /* No write method for the address space */
878 return -EINVAL;
879
Matthew Wilcox (Oracle)2be7fa12022-06-06 09:41:03 -0400880 if (!folio_clear_dirty_for_io(folio))
Christoph Lameter04e62a22006-06-23 02:03:38 -0700881 /* Someone else already triggered a write */
882 return -EAGAIN;
883
884 /*
Matthew Wilcox (Oracle)2be7fa12022-06-06 09:41:03 -0400885 * A dirty folio may imply that the underlying filesystem has
886 * the folio on some queue. So the folio must be clean for
887 * migration. Writeout may mean we lose the lock and the
888 * folio state is no longer what we checked for earlier.
Christoph Lameter04e62a22006-06-23 02:03:38 -0700889 * At this point we know that the migration attempt cannot
890 * be successful.
891 */
Matthew Wilcox (Oracle)4eecb8b2022-01-28 23:32:59 -0500892 remove_migration_ptes(folio, folio, false);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700893
Matthew Wilcox (Oracle)2be7fa12022-06-06 09:41:03 -0400894 rc = mapping->a_ops->writepage(&folio->page, &wbc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700895
896 if (rc != AOP_WRITEPAGE_ACTIVATE)
897 /* unlocked. Relock */
Matthew Wilcox (Oracle)2be7fa12022-06-06 09:41:03 -0400898 folio_lock(folio);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700899
Hugh Dickinsbda85502008-11-19 15:36:36 -0800900 return (rc < 0) ? -EIO : -EAGAIN;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700901}
902
903/*
904 * Default handling if a filesystem does not provide a migration function.
905 */
Matthew Wilcox (Oracle)8faa8ef2022-06-06 09:34:36 -0400906static int fallback_migrate_folio(struct address_space *mapping,
907 struct folio *dst, struct folio *src, enum migrate_mode mode)
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700908{
Matthew Wilcox (Oracle)8faa8ef2022-06-06 09:34:36 -0400909 if (folio_test_dirty(src)) {
910 /* Only writeback folios in full synchronous migration */
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700911 switch (mode) {
912 case MIGRATE_SYNC:
913 case MIGRATE_SYNC_NO_COPY:
914 break;
915 default:
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800916 return -EBUSY;
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700917 }
Matthew Wilcox (Oracle)2be7fa12022-06-06 09:41:03 -0400918 return writeout(mapping, src);
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800919 }
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700920
921 /*
922 * Buffers may be managed in a filesystem specific way.
923 * We must have no buffers or drop them.
924 */
Matthew Wilcox (Oracle)8faa8ef2022-06-06 09:34:36 -0400925 if (folio_test_private(src) &&
926 !filemap_release_folio(src, GFP_KERNEL))
Mel Gorman806031b2019-03-05 15:44:43 -0800927 return mode == MIGRATE_SYNC ? -EAGAIN : -EBUSY;
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700928
Matthew Wilcox (Oracle)54184652022-06-06 10:27:41 -0400929 return migrate_folio(mapping, dst, src, mode);
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700930}
931
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700932/*
Christoph Lametere24f0b82006-06-23 02:03:51 -0700933 * Move a page to a newly allocated page
934 * The page is locked and all ptes have been successfully removed.
935 *
936 * The new page will have replaced the old page if this function
937 * is successful.
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700938 *
939 * Return value:
940 * < 0 - error code
Rafael Aquini78bd5202012-12-11 16:02:31 -0800941 * MIGRATEPAGE_SUCCESS - success
Christoph Lametere24f0b82006-06-23 02:03:51 -0700942 */
Matthew Wilcox (Oracle)e7e3ffe2022-05-12 20:23:05 -0700943static int move_to_new_folio(struct folio *dst, struct folio *src,
Hugh Dickins5c3f9a62015-11-05 18:49:53 -0800944 enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700945{
Minchan Kimbda807d2016-07-26 15:23:05 -0700946 int rc = -EAGAIN;
Matthew Wilcox (Oracle)e7e3ffe2022-05-12 20:23:05 -0700947 bool is_lru = !__PageMovable(&src->page);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700948
Matthew Wilcox (Oracle)e7e3ffe2022-05-12 20:23:05 -0700949 VM_BUG_ON_FOLIO(!folio_test_locked(src), src);
950 VM_BUG_ON_FOLIO(!folio_test_locked(dst), dst);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700951
Minchan Kimbda807d2016-07-26 15:23:05 -0700952 if (likely(is_lru)) {
Matthew Wilcox (Oracle)68f27362022-06-07 15:38:48 -0400953 struct address_space *mapping = folio_mapping(src);
954
Minchan Kimbda807d2016-07-26 15:23:05 -0700955 if (!mapping)
Matthew Wilcox (Oracle)54184652022-06-06 10:27:41 -0400956 rc = migrate_folio(mapping, dst, src, mode);
Matthew Wilcox (Oracle)5490da42022-06-06 09:00:16 -0400957 else if (mapping->a_ops->migrate_folio)
Minchan Kimbda807d2016-07-26 15:23:05 -0700958 /*
Matthew Wilcox (Oracle)5490da42022-06-06 09:00:16 -0400959 * Most folios have a mapping and most filesystems
960 * provide a migrate_folio callback. Anonymous folios
Minchan Kimbda807d2016-07-26 15:23:05 -0700961 * are part of swap space which also has its own
Matthew Wilcox (Oracle)5490da42022-06-06 09:00:16 -0400962 * migrate_folio callback. This is the most common path
Minchan Kimbda807d2016-07-26 15:23:05 -0700963 * for page migration.
964 */
Matthew Wilcox (Oracle)5490da42022-06-06 09:00:16 -0400965 rc = mapping->a_ops->migrate_folio(mapping, dst, src,
966 mode);
Minchan Kimbda807d2016-07-26 15:23:05 -0700967 else
Matthew Wilcox (Oracle)8faa8ef2022-06-06 09:34:36 -0400968 rc = fallback_migrate_folio(mapping, dst, src, mode);
Minchan Kimbda807d2016-07-26 15:23:05 -0700969 } else {
Matthew Wilcox (Oracle)68f27362022-06-07 15:38:48 -0400970 const struct movable_operations *mops;
971
Christoph Lametere24f0b82006-06-23 02:03:51 -0700972 /*
Minchan Kimbda807d2016-07-26 15:23:05 -0700973 * In case of non-lru page, it could be released after
974 * isolation step. In that case, we shouldn't try migration.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700975 */
Matthew Wilcox (Oracle)e7e3ffe2022-05-12 20:23:05 -0700976 VM_BUG_ON_FOLIO(!folio_test_isolated(src), src);
977 if (!folio_test_movable(src)) {
Minchan Kimbda807d2016-07-26 15:23:05 -0700978 rc = MIGRATEPAGE_SUCCESS;
Matthew Wilcox (Oracle)e7e3ffe2022-05-12 20:23:05 -0700979 folio_clear_isolated(src);
Minchan Kimbda807d2016-07-26 15:23:05 -0700980 goto out;
981 }
982
Vishal Moola (Oracle)da707a62023-01-30 13:43:50 -0800983 mops = folio_movable_ops(src);
Matthew Wilcox (Oracle)68f27362022-06-07 15:38:48 -0400984 rc = mops->migrate_page(&dst->page, &src->page, mode);
Minchan Kimbda807d2016-07-26 15:23:05 -0700985 WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS &&
Matthew Wilcox (Oracle)e7e3ffe2022-05-12 20:23:05 -0700986 !folio_test_isolated(src));
Minchan Kimbda807d2016-07-26 15:23:05 -0700987 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700988
Hugh Dickins5c3f9a62015-11-05 18:49:53 -0800989 /*
Matthew Wilcox (Oracle)e7e3ffe2022-05-12 20:23:05 -0700990 * When successful, old pagecache src->mapping must be cleared before
991 * src is freed; but stats require that PageAnon be left as PageAnon.
Hugh Dickins5c3f9a62015-11-05 18:49:53 -0800992 */
993 if (rc == MIGRATEPAGE_SUCCESS) {
Matthew Wilcox (Oracle)e7e3ffe2022-05-12 20:23:05 -0700994 if (__PageMovable(&src->page)) {
995 VM_BUG_ON_FOLIO(!folio_test_isolated(src), src);
Minchan Kimbda807d2016-07-26 15:23:05 -0700996
997 /*
998 * We clear PG_movable under page_lock so any compactor
999 * cannot try to migrate this page.
1000 */
Matthew Wilcox (Oracle)e7e3ffe2022-05-12 20:23:05 -07001001 folio_clear_isolated(src);
Minchan Kimbda807d2016-07-26 15:23:05 -07001002 }
1003
1004 /*
Matthew Wilcox (Oracle)e7e3ffe2022-05-12 20:23:05 -07001005 * Anonymous and movable src->mapping will be cleared by
Minchan Kimbda807d2016-07-26 15:23:05 -07001006 * free_pages_prepare so don't reset it here for keeping
1007 * the type to work PageAnon, for example.
1008 */
Matthew Wilcox (Oracle)e7e3ffe2022-05-12 20:23:05 -07001009 if (!folio_mapping_flags(src))
1010 src->mapping = NULL;
Lars Perssond2b2c6dd2019-03-28 20:44:28 -07001011
Matthew Wilcox (Oracle)e7e3ffe2022-05-12 20:23:05 -07001012 if (likely(!folio_is_zone_device(dst)))
1013 flush_dcache_folio(dst);
Mel Gorman3fe20112010-05-24 14:32:20 -07001014 }
Minchan Kimbda807d2016-07-26 15:23:05 -07001015out:
Christoph Lametere24f0b82006-06-23 02:03:51 -07001016 return rc;
1017}
1018
Huang Ying64c89022023-02-13 20:34:39 +08001019/*
1020 * To record some information during migration, we use some unused
1021 * fields (mapping and private) of struct folio of the newly allocated
1022 * destination folio. This is safe because nobody is using them
1023 * except us.
1024 */
Linus Torvaldse77d5872023-03-04 14:03:27 -08001025union migration_ptr {
1026 struct anon_vma *anon_vma;
1027 struct address_space *mapping;
1028};
Huang Ying64c89022023-02-13 20:34:39 +08001029static void __migrate_folio_record(struct folio *dst,
1030 unsigned long page_was_mapped,
1031 struct anon_vma *anon_vma)
Christoph Lametere24f0b82006-06-23 02:03:51 -07001032{
Linus Torvaldse77d5872023-03-04 14:03:27 -08001033 union migration_ptr ptr = { .anon_vma = anon_vma };
1034 dst->mapping = ptr.mapping;
Huang Ying64c89022023-02-13 20:34:39 +08001035 dst->private = (void *)page_was_mapped;
1036}
1037
1038static void __migrate_folio_extract(struct folio *dst,
1039 int *page_was_mappedp,
1040 struct anon_vma **anon_vmap)
1041{
Linus Torvaldse77d5872023-03-04 14:03:27 -08001042 union migration_ptr ptr = { .mapping = dst->mapping };
1043 *anon_vmap = ptr.anon_vma;
Huang Ying64c89022023-02-13 20:34:39 +08001044 *page_was_mappedp = (unsigned long)dst->private;
1045 dst->mapping = NULL;
1046 dst->private = NULL;
1047}
1048
Huang Ying5dfab102023-02-13 20:34:40 +08001049/* Restore the source folio to the original state upon failure */
1050static void migrate_folio_undo_src(struct folio *src,
1051 int page_was_mapped,
1052 struct anon_vma *anon_vma,
Huang Yingebe75e42023-02-13 20:34:42 +08001053 bool locked,
Huang Ying5dfab102023-02-13 20:34:40 +08001054 struct list_head *ret)
1055{
1056 if (page_was_mapped)
1057 remove_migration_ptes(src, src, false);
1058 /* Drop an anon_vma reference if we took one */
1059 if (anon_vma)
1060 put_anon_vma(anon_vma);
Huang Yingebe75e42023-02-13 20:34:42 +08001061 if (locked)
1062 folio_unlock(src);
1063 if (ret)
1064 list_move_tail(&src->lru, ret);
Huang Ying5dfab102023-02-13 20:34:40 +08001065}
1066
1067/* Restore the destination folio to the original state upon failure */
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001068static void migrate_folio_undo_dst(struct folio *dst, bool locked,
1069 free_folio_t put_new_folio, unsigned long private)
Huang Ying5dfab102023-02-13 20:34:40 +08001070{
Huang Yingebe75e42023-02-13 20:34:42 +08001071 if (locked)
1072 folio_unlock(dst);
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001073 if (put_new_folio)
1074 put_new_folio(dst, private);
Huang Ying5dfab102023-02-13 20:34:40 +08001075 else
1076 folio_put(dst);
1077}
1078
Huang Ying64c89022023-02-13 20:34:39 +08001079/* Cleanup src folio upon migration success */
1080static void migrate_folio_done(struct folio *src,
1081 enum migrate_reason reason)
1082{
1083 /*
1084 * Compaction can migrate also non-LRU pages which are
1085 * not accounted to NR_ISOLATED_*. They can be recognized
1086 * as __PageMovable
1087 */
1088 if (likely(!__folio_test_movable(src)))
1089 mod_node_page_state(folio_pgdat(src), NR_ISOLATED_ANON +
1090 folio_is_file_lru(src), -folio_nr_pages(src));
1091
1092 if (reason != MR_MEMORY_FAILURE)
1093 /* We release the page in page_handle_poison. */
1094 folio_put(src);
1095}
1096
Huang Yingebe75e42023-02-13 20:34:42 +08001097/* Obtain the lock on page, remove all ptes. */
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001098static int migrate_folio_unmap(new_folio_t get_new_folio,
1099 free_folio_t put_new_folio, unsigned long private,
1100 struct folio *src, struct folio **dstp, enum migrate_mode mode,
1101 enum migrate_reason reason, struct list_head *ret)
Christoph Lametere24f0b82006-06-23 02:03:51 -07001102{
Huang Yingebe75e42023-02-13 20:34:42 +08001103 struct folio *dst;
Minchan Kim0dabec92011-10-31 17:06:57 -07001104 int rc = -EAGAIN;
Huang Ying64c89022023-02-13 20:34:39 +08001105 int page_was_mapped = 0;
Mel Gorman3f6c8272010-05-24 14:32:17 -07001106 struct anon_vma *anon_vma = NULL;
Matthew Wilcox (Oracle)682a71a2022-09-02 20:46:46 +01001107 bool is_lru = !__PageMovable(&src->page);
Huang Yingebe75e42023-02-13 20:34:42 +08001108 bool locked = false;
1109 bool dst_locked = false;
1110
Huang Yingebe75e42023-02-13 20:34:42 +08001111 if (folio_ref_count(src) == 1) {
1112 /* Folio was freed from under us. So we are done. */
1113 folio_clear_active(src);
1114 folio_clear_unevictable(src);
1115 /* free_pages_prepare() will clear PG_isolated. */
1116 list_del(&src->lru);
1117 migrate_folio_done(src, reason);
1118 return MIGRATEPAGE_SUCCESS;
1119 }
1120
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001121 dst = get_new_folio(src, private);
1122 if (!dst)
Huang Yingebe75e42023-02-13 20:34:42 +08001123 return -ENOMEM;
Huang Yingebe75e42023-02-13 20:34:42 +08001124 *dstp = dst;
1125
1126 dst->private = NULL;
Christoph Lameter95a402c2006-06-23 02:03:53 -07001127
Matthew Wilcox (Oracle)682a71a2022-09-02 20:46:46 +01001128 if (!folio_trylock(src)) {
Huang Ying2ef7dbb2023-03-03 11:01:55 +08001129 if (mode == MIGRATE_ASYNC)
Minchan Kim0dabec92011-10-31 17:06:57 -07001130 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -08001131
1132 /*
1133 * It's not safe for direct compaction to call lock_page.
1134 * For example, during page readahead pages are added locked
1135 * to the LRU. Later, when the IO completes the pages are
1136 * marked uptodate and unlocked. However, the queueing
1137 * could be merging multiple pages for one bio (e.g.
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -07001138 * mpage_readahead). If an allocation happens for the
Mel Gorman3e7d3442011-01-13 15:45:56 -08001139 * second or third page, the process can end up locking
1140 * the same page twice and deadlocking. Rather than
1141 * trying to be clever about what pages can be locked,
1142 * avoid the use of lock_page for direct compaction
1143 * altogether.
1144 */
1145 if (current->flags & PF_MEMALLOC)
Minchan Kim0dabec92011-10-31 17:06:57 -07001146 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -08001147
Douglas Anderson4bb6dc72023-04-28 13:54:38 -07001148 /*
1149 * In "light" mode, we can wait for transient locks (eg
1150 * inserting a page into the page table), but it's not
1151 * worth waiting for I/O.
1152 */
1153 if (mode == MIGRATE_SYNC_LIGHT && !folio_test_uptodate(src))
1154 goto out;
1155
Matthew Wilcox (Oracle)682a71a2022-09-02 20:46:46 +01001156 folio_lock(src);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001157 }
Huang Yingebe75e42023-02-13 20:34:42 +08001158 locked = true;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001159
Matthew Wilcox (Oracle)682a71a2022-09-02 20:46:46 +01001160 if (folio_test_writeback(src)) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -07001161 /*
Jianguo Wufed5b642013-04-29 15:07:58 -07001162 * Only in the case of a full synchronous migration is it
Mel Gormana6bc32b2012-01-12 17:19:43 -08001163 * necessary to wait for PageWriteback. In the async case,
1164 * the retry loop is too short and in the sync-light case,
1165 * the overhead of stalling is too much
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -07001166 */
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07001167 switch (mode) {
1168 case MIGRATE_SYNC:
1169 case MIGRATE_SYNC_NO_COPY:
1170 break;
1171 default:
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -07001172 rc = -EBUSY;
Huang Yingebe75e42023-02-13 20:34:42 +08001173 goto out;
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -07001174 }
Matthew Wilcox (Oracle)682a71a2022-09-02 20:46:46 +01001175 folio_wait_writeback(src);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001176 }
Hugh Dickins03f15c82015-11-05 18:49:56 -08001177
Christoph Lametere24f0b82006-06-23 02:03:51 -07001178 /*
Matthew Wilcox (Oracle)682a71a2022-09-02 20:46:46 +01001179 * By try_to_migrate(), src->mapcount goes down to 0 here. In this case,
1180 * we cannot notice that anon_vma is freed while we migrate a page.
Hugh Dickins1ce82b62011-01-13 15:47:30 -08001181 * This get_anon_vma() delays freeing anon_vma pointer until the end
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001182 * of migration. File cache pages are no problem because of page_lock()
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -07001183 * File Caches may use write_page() or lock_page() in migration, then,
1184 * just care Anon page here.
Hugh Dickins03f15c82015-11-05 18:49:56 -08001185 *
Matthew Wilcox (Oracle)29eea9b2022-09-02 20:46:50 +01001186 * Only folio_get_anon_vma() understands the subtleties of
Hugh Dickins03f15c82015-11-05 18:49:56 -08001187 * getting a hold on an anon_vma from outside one of its mms.
1188 * But if we cannot get anon_vma, then we won't need it anyway,
1189 * because that implies that the anon page is no longer mapped
1190 * (and cannot be remapped so long as we hold the page lock).
Christoph Lametere24f0b82006-06-23 02:03:51 -07001191 */
Matthew Wilcox (Oracle)682a71a2022-09-02 20:46:46 +01001192 if (folio_test_anon(src) && !folio_test_ksm(src))
Matthew Wilcox (Oracle)29eea9b2022-09-02 20:46:50 +01001193 anon_vma = folio_get_anon_vma(src);
Shaohua Li62e1c552008-02-04 22:29:33 -08001194
Hugh Dickins7db76712015-11-05 18:49:49 -08001195 /*
1196 * Block others from accessing the new page when we get around to
1197 * establishing additional references. We are usually the only one
Matthew Wilcox (Oracle)682a71a2022-09-02 20:46:46 +01001198 * holding a reference to dst at this point. We used to have a BUG
1199 * here if folio_trylock(dst) fails, but would like to allow for
1200 * cases where there might be a race with the previous use of dst.
Hugh Dickins7db76712015-11-05 18:49:49 -08001201 * This is much like races on refcount of oldpage: just don't BUG().
1202 */
Matthew Wilcox (Oracle)682a71a2022-09-02 20:46:46 +01001203 if (unlikely(!folio_trylock(dst)))
Huang Yingebe75e42023-02-13 20:34:42 +08001204 goto out;
1205 dst_locked = true;
Hugh Dickins7db76712015-11-05 18:49:49 -08001206
Minchan Kimbda807d2016-07-26 15:23:05 -07001207 if (unlikely(!is_lru)) {
Huang Ying64c89022023-02-13 20:34:39 +08001208 __migrate_folio_record(dst, page_was_mapped, anon_vma);
1209 return MIGRATEPAGE_UNMAP;
Minchan Kimbda807d2016-07-26 15:23:05 -07001210 }
1211
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001212 /*
Shaohua Li62e1c552008-02-04 22:29:33 -08001213 * Corner case handling:
1214 * 1. When a new swap-cache page is read into, it is added to the LRU
1215 * and treated as swapcache but it has no rmap yet.
Matthew Wilcox (Oracle)682a71a2022-09-02 20:46:46 +01001216 * Calling try_to_unmap() against a src->mapping==NULL page will
Shaohua Li62e1c552008-02-04 22:29:33 -08001217 * trigger a BUG. So handle it here.
Yang Shid12b8952020-12-14 19:13:02 -08001218 * 2. An orphaned page (see truncate_cleanup_page) might have
Shaohua Li62e1c552008-02-04 22:29:33 -08001219 * fs-private metadata. The page can be picked up due to memory
1220 * offlining. Everywhere else except page reclaim, the page is
1221 * invisible to the vm, so the page can not be migrated. So try to
1222 * free the metadata, so the page can be freed.
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001223 */
Matthew Wilcox (Oracle)682a71a2022-09-02 20:46:46 +01001224 if (!src->mapping) {
1225 if (folio_test_private(src)) {
1226 try_to_free_buffers(src);
Huang Yingebe75e42023-02-13 20:34:42 +08001227 goto out;
Shaohua Li62e1c552008-02-04 22:29:33 -08001228 }
Matthew Wilcox (Oracle)682a71a2022-09-02 20:46:46 +01001229 } else if (folio_mapped(src)) {
Hugh Dickins7db76712015-11-05 18:49:49 -08001230 /* Establish migration ptes */
Matthew Wilcox (Oracle)682a71a2022-09-02 20:46:46 +01001231 VM_BUG_ON_FOLIO(folio_test_anon(src) &&
1232 !folio_test_ksm(src) && !anon_vma, src);
Huang Yingfb3592c2023-03-03 11:01:53 +08001233 try_to_migrate(src, mode == MIGRATE_ASYNC ? TTU_BATCH_FLUSH : 0);
Huang Ying64c89022023-02-13 20:34:39 +08001234 page_was_mapped = 1;
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001235 }
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001236
Huang Ying64c89022023-02-13 20:34:39 +08001237 if (!folio_mapped(src)) {
1238 __migrate_folio_record(dst, page_was_mapped, anon_vma);
1239 return MIGRATEPAGE_UNMAP;
1240 }
1241
Huang Ying64c89022023-02-13 20:34:39 +08001242out:
Huang Ying80562ba2023-02-13 20:34:41 +08001243 /*
1244 * A folio that has not been unmapped will be restored to
1245 * right list unless we want to retry.
1246 */
Huang Yingfb3592c2023-03-03 11:01:53 +08001247 if (rc == -EAGAIN)
Huang Yingebe75e42023-02-13 20:34:42 +08001248 ret = NULL;
Huang Ying80562ba2023-02-13 20:34:41 +08001249
Huang Yingebe75e42023-02-13 20:34:42 +08001250 migrate_folio_undo_src(src, page_was_mapped, anon_vma, locked, ret);
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001251 migrate_folio_undo_dst(dst, dst_locked, put_new_folio, private);
Huang Ying80562ba2023-02-13 20:34:41 +08001252
1253 return rc;
1254}
1255
Huang Yingebe75e42023-02-13 20:34:42 +08001256/* Migrate the folio to the newly allocated folio in dst. */
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001257static int migrate_folio_move(free_folio_t put_new_folio, unsigned long private,
Huang Yingebe75e42023-02-13 20:34:42 +08001258 struct folio *src, struct folio *dst,
1259 enum migrate_mode mode, enum migrate_reason reason,
1260 struct list_head *ret)
Huang Ying64c89022023-02-13 20:34:39 +08001261{
1262 int rc;
1263 int page_was_mapped = 0;
1264 struct anon_vma *anon_vma = NULL;
1265 bool is_lru = !__PageMovable(&src->page);
Huang Ying5dfab102023-02-13 20:34:40 +08001266 struct list_head *prev;
Huang Ying64c89022023-02-13 20:34:39 +08001267
1268 __migrate_folio_extract(dst, &page_was_mapped, &anon_vma);
Huang Ying5dfab102023-02-13 20:34:40 +08001269 prev = dst->lru.prev;
1270 list_del(&dst->lru);
Huang Ying64c89022023-02-13 20:34:39 +08001271
1272 rc = move_to_new_folio(dst, src, mode);
Huang Yingebe75e42023-02-13 20:34:42 +08001273 if (rc)
1274 goto out;
Huang Ying5dfab102023-02-13 20:34:40 +08001275
Huang Ying64c89022023-02-13 20:34:39 +08001276 if (unlikely(!is_lru))
1277 goto out_unlock_both;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001278
Hugh Dickinsc3096e62022-02-14 18:33:17 -08001279 /*
Matthew Wilcox (Oracle)682a71a2022-09-02 20:46:46 +01001280 * When successful, push dst to LRU immediately: so that if it
Hugh Dickinsc3096e62022-02-14 18:33:17 -08001281 * turns out to be an mlocked page, remove_migration_ptes() will
Matthew Wilcox (Oracle)682a71a2022-09-02 20:46:46 +01001282 * automatically build up the correct dst->mlock_count for it.
Hugh Dickinsc3096e62022-02-14 18:33:17 -08001283 *
1284 * We would like to do something similar for the old page, when
1285 * unsuccessful, and other cases when a page has been temporarily
1286 * isolated from the unevictable LRU: but this case is the easiest.
1287 */
Huang Yingebe75e42023-02-13 20:34:42 +08001288 folio_add_lru(dst);
1289 if (page_was_mapped)
1290 lru_add_drain();
Hugh Dickinsc3096e62022-02-14 18:33:17 -08001291
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001292 if (page_was_mapped)
Huang Yingebe75e42023-02-13 20:34:42 +08001293 remove_migration_ptes(src, dst, false);
Mel Gorman3f6c8272010-05-24 14:32:17 -07001294
Hugh Dickins7db76712015-11-05 18:49:49 -08001295out_unlock_both:
Matthew Wilcox (Oracle)682a71a2022-09-02 20:46:46 +01001296 folio_unlock(dst);
Huang Yingebe75e42023-02-13 20:34:42 +08001297 set_page_owner_migrate_reason(&dst->page, reason);
Minchan Kimc6c919e2016-07-26 15:23:02 -07001298 /*
Matthew Wilcox (Oracle)682a71a2022-09-02 20:46:46 +01001299 * If migration is successful, decrease refcount of dst,
Minchan Kimc6c919e2016-07-26 15:23:02 -07001300 * which will not free the page because new page owner increased
Hugh Dickinsc3096e62022-02-14 18:33:17 -08001301 * refcounter.
Minchan Kimc6c919e2016-07-26 15:23:02 -07001302 */
Huang Yingebe75e42023-02-13 20:34:42 +08001303 folio_put(dst);
David Rientjes68711a72014-06-04 16:08:25 -07001304
Christoph Lameter95a402c2006-06-23 02:03:53 -07001305 /*
Huang Yingebe75e42023-02-13 20:34:42 +08001306 * A folio that has been migrated has all references removed
1307 * and will be freed.
Christoph Lameter95a402c2006-06-23 02:03:53 -07001308 */
Huang Yingebe75e42023-02-13 20:34:42 +08001309 list_del(&src->lru);
1310 /* Drop an anon_vma reference if we took one */
1311 if (anon_vma)
1312 put_anon_vma(anon_vma);
1313 folio_unlock(src);
1314 migrate_folio_done(src, reason);
Yang Shidd4ae782020-12-14 19:13:06 -08001315
Huang Yingebe75e42023-02-13 20:34:42 +08001316 return rc;
1317out:
1318 /*
1319 * A folio that has not been migrated will be restored to
1320 * right list unless we want to retry.
1321 */
1322 if (rc == -EAGAIN) {
1323 list_add(&dst->lru, prev);
1324 __migrate_folio_record(dst, page_was_mapped, anon_vma);
1325 return rc;
Minchan Kimc6c919e2016-07-26 15:23:02 -07001326 }
David Rientjes68711a72014-06-04 16:08:25 -07001327
Huang Yingebe75e42023-02-13 20:34:42 +08001328 migrate_folio_undo_src(src, page_was_mapped, anon_vma, true, ret);
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001329 migrate_folio_undo_dst(dst, true, put_new_folio, private);
Huang Yingebe75e42023-02-13 20:34:42 +08001330
Christoph Lametere24f0b82006-06-23 02:03:51 -07001331 return rc;
1332}
1333
1334/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001335 * Counterpart of unmap_and_move_page() for hugepage migration.
1336 *
1337 * This function doesn't wait the completion of hugepage I/O
1338 * because there is no race between I/O and migration for hugepage.
1339 * Note that currently hugepage I/O occurs only in direct I/O
1340 * where no lock is held and PG_writeback is irrelevant,
1341 * and writeback status of all subpages are counted in the reference
1342 * count of the head page (i.e. if all subpages of a 2MB hugepage are
1343 * under direct I/O, the reference of the head page is 512 and a bit more.)
1344 * This means that when we try to migrate hugepage whose subpages are
1345 * doing direct I/O, some references remain after try_to_unmap() and
1346 * hugepage migration fails without data corruption.
1347 *
1348 * There is also no race when direct I/O is issued on the page under migration,
1349 * because then pte is replaced with migration swap entry and direct I/O code
1350 * will wait in the page fault for migration to complete.
1351 */
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001352static int unmap_and_move_huge_page(new_folio_t get_new_folio,
1353 free_folio_t put_new_folio, unsigned long private,
1354 struct folio *src, int force, enum migrate_mode mode,
1355 int reason, struct list_head *ret)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001356{
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001357 struct folio *dst;
Hugh Dickins2def7422015-11-05 18:49:46 -08001358 int rc = -EAGAIN;
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001359 int page_was_mapped = 0;
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001360 struct anon_vma *anon_vma = NULL;
Mike Kravetzc0d03812020-04-01 21:11:05 -07001361 struct address_space *mapping = NULL;
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001362
Matthew Wilcox (Oracle)c33db292022-09-02 20:46:47 +01001363 if (folio_ref_count(src) == 1) {
Muchun Song71a64f62021-02-04 18:32:17 -08001364 /* page was freed from under us. So we are done. */
Sidhartha Kumarea8e72f2023-01-25 09:05:32 -08001365 folio_putback_active_hugetlb(src);
Muchun Song71a64f62021-02-04 18:32:17 -08001366 return MIGRATEPAGE_SUCCESS;
1367 }
1368
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001369 dst = get_new_folio(src, private);
1370 if (!dst)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001371 return -ENOMEM;
1372
Matthew Wilcox (Oracle)c33db292022-09-02 20:46:47 +01001373 if (!folio_trylock(src)) {
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07001374 if (!force)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001375 goto out;
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07001376 switch (mode) {
1377 case MIGRATE_SYNC:
1378 case MIGRATE_SYNC_NO_COPY:
1379 break;
1380 default:
1381 goto out;
1382 }
Matthew Wilcox (Oracle)c33db292022-09-02 20:46:47 +01001383 folio_lock(src);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001384 }
1385
Mike Kravetzcb6acd02019-02-28 16:22:02 -08001386 /*
1387 * Check for pages which are in the process of being freed. Without
Matthew Wilcox (Oracle)c33db292022-09-02 20:46:47 +01001388 * folio_mapping() set, hugetlbfs specific move page routine will not
Mike Kravetzcb6acd02019-02-28 16:22:02 -08001389 * be called and we could leak usage counts for subpools.
1390 */
Sidhartha Kumar345c62d2022-11-01 15:30:59 -07001391 if (hugetlb_folio_subpool(src) && !folio_mapping(src)) {
Mike Kravetzcb6acd02019-02-28 16:22:02 -08001392 rc = -EBUSY;
1393 goto out_unlock;
1394 }
1395
Matthew Wilcox (Oracle)c33db292022-09-02 20:46:47 +01001396 if (folio_test_anon(src))
Matthew Wilcox (Oracle)29eea9b2022-09-02 20:46:50 +01001397 anon_vma = folio_get_anon_vma(src);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001398
Matthew Wilcox (Oracle)c33db292022-09-02 20:46:47 +01001399 if (unlikely(!folio_trylock(dst)))
Hugh Dickins7db76712015-11-05 18:49:49 -08001400 goto put_anon;
1401
Matthew Wilcox (Oracle)c33db292022-09-02 20:46:47 +01001402 if (folio_mapped(src)) {
Alistair Popplea98a2f02021-06-30 18:54:16 -07001403 enum ttu_flags ttu = 0;
Mike Kravetzc0d03812020-04-01 21:11:05 -07001404
Matthew Wilcox (Oracle)c33db292022-09-02 20:46:47 +01001405 if (!folio_test_anon(src)) {
Mike Kravetz336bf302020-11-13 22:52:16 -08001406 /*
1407 * In shared mappings, try_to_unmap could potentially
1408 * call huge_pmd_unshare. Because of this, take
1409 * semaphore in write mode here and set TTU_RMAP_LOCKED
1410 * to let lower levels know we have taken the lock.
1411 */
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001412 mapping = hugetlb_page_mapping_lock_write(&src->page);
Mike Kravetz336bf302020-11-13 22:52:16 -08001413 if (unlikely(!mapping))
1414 goto unlock_put_anon;
1415
Miaohe Lin52029782022-04-28 23:16:07 -07001416 ttu = TTU_RMAP_LOCKED;
Mike Kravetz336bf302020-11-13 22:52:16 -08001417 }
1418
Matthew Wilcox (Oracle)4b8554c2022-01-28 14:29:43 -05001419 try_to_migrate(src, ttu);
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001420 page_was_mapped = 1;
Mike Kravetz336bf302020-11-13 22:52:16 -08001421
Miaohe Lin52029782022-04-28 23:16:07 -07001422 if (ttu & TTU_RMAP_LOCKED)
Mike Kravetz336bf302020-11-13 22:52:16 -08001423 i_mmap_unlock_write(mapping);
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001424 }
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001425
Matthew Wilcox (Oracle)c33db292022-09-02 20:46:47 +01001426 if (!folio_mapped(src))
Matthew Wilcox (Oracle)e7e3ffe2022-05-12 20:23:05 -07001427 rc = move_to_new_folio(dst, src, mode);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001428
Mike Kravetz336bf302020-11-13 22:52:16 -08001429 if (page_was_mapped)
Matthew Wilcox (Oracle)4eecb8b2022-01-28 23:32:59 -05001430 remove_migration_ptes(src,
1431 rc == MIGRATEPAGE_SUCCESS ? dst : src, false);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001432
Mike Kravetzc0d03812020-04-01 21:11:05 -07001433unlock_put_anon:
Matthew Wilcox (Oracle)c33db292022-09-02 20:46:47 +01001434 folio_unlock(dst);
Hugh Dickins7db76712015-11-05 18:49:49 -08001435
1436put_anon:
Hugh Dickinsfd4a4662011-01-13 15:47:31 -08001437 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -07001438 put_anon_vma(anon_vma);
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -07001439
Hugh Dickins2def7422015-11-05 18:49:46 -08001440 if (rc == MIGRATEPAGE_SUCCESS) {
Sidhartha Kumar345c62d2022-11-01 15:30:59 -07001441 move_hugetlb_state(src, dst, reason);
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001442 put_new_folio = NULL;
Hugh Dickins2def7422015-11-05 18:49:46 -08001443 }
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -07001444
Mike Kravetzcb6acd02019-02-28 16:22:02 -08001445out_unlock:
Matthew Wilcox (Oracle)c33db292022-09-02 20:46:47 +01001446 folio_unlock(src);
Hillf Danton09761332011-12-08 14:34:20 -08001447out:
Yang Shidd4ae782020-12-14 19:13:06 -08001448 if (rc == MIGRATEPAGE_SUCCESS)
Sidhartha Kumarea8e72f2023-01-25 09:05:32 -08001449 folio_putback_active_hugetlb(src);
Miaohe Lina04840c2021-05-04 18:37:07 -07001450 else if (rc != -EAGAIN)
Matthew Wilcox (Oracle)c33db292022-09-02 20:46:47 +01001451 list_move_tail(&src->lru, ret);
David Rientjes68711a72014-06-04 16:08:25 -07001452
1453 /*
1454 * If migration was not successful and there's a freeing callback, use
1455 * it. Otherwise, put_page() will drop the reference grabbed during
1456 * isolation.
1457 */
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001458 if (put_new_folio)
1459 put_new_folio(dst, private);
David Rientjes68711a72014-06-04 16:08:25 -07001460 else
Sidhartha Kumarea8e72f2023-01-25 09:05:32 -08001461 folio_putback_active_hugetlb(dst);
David Rientjes68711a72014-06-04 16:08:25 -07001462
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001463 return rc;
1464}
1465
Huang Yingeaec4e62022-11-09 09:23:48 +08001466static inline int try_split_folio(struct folio *folio, struct list_head *split_folios)
Yang Shid532e2e2020-12-14 19:13:16 -08001467{
Huang Ying9c62ff02022-08-17 16:14:02 +08001468 int rc;
Yang Shid532e2e2020-12-14 19:13:16 -08001469
Huang Yingeaec4e62022-11-09 09:23:48 +08001470 folio_lock(folio);
1471 rc = split_folio_to_list(folio, split_folios);
1472 folio_unlock(folio);
Huang Yinge6fa8a72022-08-17 16:14:06 +08001473 if (!rc)
Huang Yingeaec4e62022-11-09 09:23:48 +08001474 list_move_tail(&folio->lru, split_folios);
Yang Shid532e2e2020-12-14 19:13:16 -08001475
1476 return rc;
1477}
1478
Huang Ying42012e02023-02-13 20:34:38 +08001479#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1480#define NR_MAX_BATCHED_MIGRATION HPAGE_PMD_NR
1481#else
1482#define NR_MAX_BATCHED_MIGRATION 512
1483#endif
Huang Yinge5bfff8b2023-02-13 20:34:37 +08001484#define NR_MAX_MIGRATE_PAGES_RETRY 10
Huang Ying2ef7dbb2023-03-03 11:01:55 +08001485#define NR_MAX_MIGRATE_ASYNC_RETRY 3
1486#define NR_MAX_MIGRATE_SYNC_RETRY \
1487 (NR_MAX_MIGRATE_PAGES_RETRY - NR_MAX_MIGRATE_ASYNC_RETRY)
Huang Yinge5bfff8b2023-02-13 20:34:37 +08001488
Huang Ying5b855932023-02-13 20:34:36 +08001489struct migrate_pages_stats {
1490 int nr_succeeded; /* Normal and large folios migrated successfully, in
1491 units of base pages */
1492 int nr_failed_pages; /* Normal and large folios failed to be migrated, in
1493 units of base pages. Untried folios aren't counted */
1494 int nr_thp_succeeded; /* THP migrated successfully */
1495 int nr_thp_failed; /* THP failed to be migrated */
1496 int nr_thp_split; /* THP split before migrating */
1497};
1498
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001499/*
Huang Yinge5bfff8b2023-02-13 20:34:37 +08001500 * Returns the number of hugetlb folios that were not migrated, or an error code
1501 * after NR_MAX_MIGRATE_PAGES_RETRY attempts or if no hugetlb folios are movable
1502 * any more because the list has become empty or no retryable hugetlb folios
1503 * exist any more. It is caller's responsibility to call putback_movable_pages()
1504 * only if ret != 0.
1505 */
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001506static int migrate_hugetlbs(struct list_head *from, new_folio_t get_new_folio,
1507 free_folio_t put_new_folio, unsigned long private,
Huang Yinge5bfff8b2023-02-13 20:34:37 +08001508 enum migrate_mode mode, int reason,
1509 struct migrate_pages_stats *stats,
1510 struct list_head *ret_folios)
1511{
1512 int retry = 1;
1513 int nr_failed = 0;
1514 int nr_retry_pages = 0;
1515 int pass = 0;
1516 struct folio *folio, *folio2;
1517 int rc, nr_pages;
1518
1519 for (pass = 0; pass < NR_MAX_MIGRATE_PAGES_RETRY && retry; pass++) {
1520 retry = 0;
1521 nr_retry_pages = 0;
1522
1523 list_for_each_entry_safe(folio, folio2, from, lru) {
1524 if (!folio_test_hugetlb(folio))
1525 continue;
1526
1527 nr_pages = folio_nr_pages(folio);
1528
1529 cond_resched();
1530
Huang Ying6f7d7602023-02-13 20:34:44 +08001531 /*
1532 * Migratability of hugepages depends on architectures and
1533 * their size. This check is necessary because some callers
1534 * of hugepage migration like soft offline and memory
1535 * hotremove don't walk through page tables or check whether
1536 * the hugepage is pmd-based or not before kicking migration.
1537 */
1538 if (!hugepage_migration_supported(folio_hstate(folio))) {
1539 nr_failed++;
1540 stats->nr_failed_pages += nr_pages;
1541 list_move_tail(&folio->lru, ret_folios);
1542 continue;
1543 }
1544
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001545 rc = unmap_and_move_huge_page(get_new_folio,
1546 put_new_folio, private,
1547 folio, pass > 2, mode,
Huang Yinge5bfff8b2023-02-13 20:34:37 +08001548 reason, ret_folios);
1549 /*
1550 * The rules are:
1551 * Success: hugetlb folio will be put back
1552 * -EAGAIN: stay on the from list
1553 * -ENOMEM: stay on the from list
Huang Yinge5bfff8b2023-02-13 20:34:37 +08001554 * Other errno: put on ret_folios list
1555 */
1556 switch(rc) {
Huang Yinge5bfff8b2023-02-13 20:34:37 +08001557 case -ENOMEM:
1558 /*
1559 * When memory is low, don't bother to try to migrate
1560 * other folios, just exit.
1561 */
1562 stats->nr_failed_pages += nr_pages + nr_retry_pages;
1563 return -ENOMEM;
1564 case -EAGAIN:
1565 retry++;
1566 nr_retry_pages += nr_pages;
1567 break;
1568 case MIGRATEPAGE_SUCCESS:
1569 stats->nr_succeeded += nr_pages;
1570 break;
1571 default:
1572 /*
1573 * Permanent failure (-EBUSY, etc.):
1574 * unlike -EAGAIN case, the failed folio is
1575 * removed from migration folio list and not
1576 * retried in the next outer loop.
1577 */
1578 nr_failed++;
1579 stats->nr_failed_pages += nr_pages;
1580 break;
1581 }
1582 }
1583 }
1584 /*
1585 * nr_failed is number of hugetlb folios failed to be migrated. After
1586 * NR_MAX_MIGRATE_PAGES_RETRY attempts, give up and count retried hugetlb
1587 * folios as failed.
1588 */
1589 nr_failed += retry;
1590 stats->nr_failed_pages += nr_retry_pages;
1591
1592 return nr_failed;
1593}
1594
Huang Ying5dfab102023-02-13 20:34:40 +08001595/*
1596 * migrate_pages_batch() first unmaps folios in the from list as many as
1597 * possible, then move the unmapped folios.
Huang Yingfb3592c2023-03-03 11:01:53 +08001598 *
1599 * We only batch migration if mode == MIGRATE_ASYNC to avoid to wait a
1600 * lock or bit when we have locked more than one folio. Which may cause
1601 * deadlock (e.g., for loop device). So, if mode != MIGRATE_ASYNC, the
1602 * length of the from list must be <= 1.
Huang Ying5dfab102023-02-13 20:34:40 +08001603 */
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001604static int migrate_pages_batch(struct list_head *from,
1605 new_folio_t get_new_folio, free_folio_t put_new_folio,
1606 unsigned long private, enum migrate_mode mode, int reason,
1607 struct list_head *ret_folios, struct list_head *split_folios,
1608 struct migrate_pages_stats *stats, int nr_pass)
Huang Ying42012e02023-02-13 20:34:38 +08001609{
Huang Yinga21d2132023-03-03 11:01:54 +08001610 int retry = 1;
Huang Ying42012e02023-02-13 20:34:38 +08001611 int thp_retry = 1;
1612 int nr_failed = 0;
1613 int nr_retry_pages = 0;
Huang Ying42012e02023-02-13 20:34:38 +08001614 int pass = 0;
Huang Ying42012e02023-02-13 20:34:38 +08001615 bool is_thp = false;
Huang Ying5dfab102023-02-13 20:34:40 +08001616 struct folio *folio, *folio2, *dst = NULL, *dst2;
Huang Yinga21d2132023-03-03 11:01:54 +08001617 int rc, rc_saved = 0, nr_pages;
Huang Ying5dfab102023-02-13 20:34:40 +08001618 LIST_HEAD(unmap_folios);
1619 LIST_HEAD(dst_folios);
Huang Ying42012e02023-02-13 20:34:38 +08001620 bool nosplit = (reason == MR_NUMA_MISPLACED);
Huang Ying42012e02023-02-13 20:34:38 +08001621
Huang Yingfb3592c2023-03-03 11:01:53 +08001622 VM_WARN_ON_ONCE(mode != MIGRATE_ASYNC &&
1623 !list_empty(from) && !list_is_singular(from));
Huang Yinga21d2132023-03-03 11:01:54 +08001624
Huang Ying124abce2023-05-10 11:18:29 +08001625 for (pass = 0; pass < nr_pass && retry; pass++) {
Huang Ying42012e02023-02-13 20:34:38 +08001626 retry = 0;
Huang Ying42012e02023-02-13 20:34:38 +08001627 thp_retry = 0;
1628 nr_retry_pages = 0;
1629
1630 list_for_each_entry_safe(folio, folio2, from, lru) {
Huang Ying124abce2023-05-10 11:18:29 +08001631 is_thp = folio_test_large(folio) && folio_test_pmd_mappable(folio);
Huang Ying42012e02023-02-13 20:34:38 +08001632 nr_pages = folio_nr_pages(folio);
1633
1634 cond_resched();
1635
Huang Ying6f7d7602023-02-13 20:34:44 +08001636 /*
1637 * Large folio migration might be unsupported or
1638 * the allocation might be failed so we should retry
1639 * on the same folio with the large folio split
1640 * to normal folios.
1641 *
1642 * Split folios are put in split_folios, and
1643 * we will migrate them after the rest of the
1644 * list is processed.
1645 */
1646 if (!thp_migration_supported() && is_thp) {
Huang Ying124abce2023-05-10 11:18:29 +08001647 nr_failed++;
Huang Ying6f7d7602023-02-13 20:34:44 +08001648 stats->nr_thp_failed++;
Huang Yinga21d2132023-03-03 11:01:54 +08001649 if (!try_split_folio(folio, split_folios)) {
Huang Ying6f7d7602023-02-13 20:34:44 +08001650 stats->nr_thp_split++;
1651 continue;
1652 }
1653 stats->nr_failed_pages += nr_pages;
1654 list_move_tail(&folio->lru, ret_folios);
1655 continue;
1656 }
1657
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001658 rc = migrate_folio_unmap(get_new_folio, put_new_folio,
1659 private, folio, &dst, mode, reason,
1660 ret_folios);
Huang Ying42012e02023-02-13 20:34:38 +08001661 /*
1662 * The rules are:
1663 * Success: folio will be freed
Huang Ying5dfab102023-02-13 20:34:40 +08001664 * Unmap: folio will be put on unmap_folios list,
1665 * dst folio put on dst_folios list
Huang Ying42012e02023-02-13 20:34:38 +08001666 * -EAGAIN: stay on the from list
1667 * -ENOMEM: stay on the from list
Huang Ying42012e02023-02-13 20:34:38 +08001668 * Other errno: put on ret_folios list
1669 */
1670 switch(rc) {
Huang Ying42012e02023-02-13 20:34:38 +08001671 case -ENOMEM:
1672 /*
1673 * When memory is low, don't bother to try to migrate
Huang Ying5dfab102023-02-13 20:34:40 +08001674 * other folios, move unmapped folios, then exit.
Huang Ying42012e02023-02-13 20:34:38 +08001675 */
Huang Ying124abce2023-05-10 11:18:29 +08001676 nr_failed++;
1677 stats->nr_thp_failed += is_thp;
1678 /* Large folio NUMA faulting doesn't split to retry. */
1679 if (folio_test_large(folio) && !nosplit) {
1680 int ret = try_split_folio(folio, split_folios);
Huang Ying42012e02023-02-13 20:34:38 +08001681
Huang Ying124abce2023-05-10 11:18:29 +08001682 if (!ret) {
1683 stats->nr_thp_split += is_thp;
1684 break;
1685 } else if (reason == MR_LONGTERM_PIN &&
1686 ret == -EAGAIN) {
1687 /*
1688 * Try again to split large folio to
1689 * mitigate the failure of longterm pinning.
1690 */
1691 retry++;
1692 thp_retry += is_thp;
1693 nr_retry_pages += nr_pages;
1694 /* Undo duplicated failure counting. */
1695 nr_failed--;
1696 stats->nr_thp_failed -= is_thp;
1697 break;
Huang Ying42012e02023-02-13 20:34:38 +08001698 }
Huang Ying42012e02023-02-13 20:34:38 +08001699 }
1700
1701 stats->nr_failed_pages += nr_pages + nr_retry_pages;
Huang Ying42012e02023-02-13 20:34:38 +08001702 /* nr_failed isn't updated for not used */
Huang Ying42012e02023-02-13 20:34:38 +08001703 stats->nr_thp_failed += thp_retry;
Huang Ying5dfab102023-02-13 20:34:40 +08001704 rc_saved = rc;
1705 if (list_empty(&unmap_folios))
1706 goto out;
1707 else
1708 goto move;
Huang Ying42012e02023-02-13 20:34:38 +08001709 case -EAGAIN:
Huang Ying124abce2023-05-10 11:18:29 +08001710 retry++;
1711 thp_retry += is_thp;
Huang Ying42012e02023-02-13 20:34:38 +08001712 nr_retry_pages += nr_pages;
1713 break;
1714 case MIGRATEPAGE_SUCCESS:
1715 stats->nr_succeeded += nr_pages;
1716 stats->nr_thp_succeeded += is_thp;
1717 break;
Huang Ying5dfab102023-02-13 20:34:40 +08001718 case MIGRATEPAGE_UNMAP:
Huang Ying5dfab102023-02-13 20:34:40 +08001719 list_move_tail(&folio->lru, &unmap_folios);
1720 list_add_tail(&dst->lru, &dst_folios);
1721 break;
Huang Ying42012e02023-02-13 20:34:38 +08001722 default:
1723 /*
1724 * Permanent failure (-EBUSY, etc.):
1725 * unlike -EAGAIN case, the failed folio is
1726 * removed from migration folio list and not
1727 * retried in the next outer loop.
1728 */
Huang Ying124abce2023-05-10 11:18:29 +08001729 nr_failed++;
1730 stats->nr_thp_failed += is_thp;
Huang Ying42012e02023-02-13 20:34:38 +08001731 stats->nr_failed_pages += nr_pages;
1732 break;
1733 }
1734 }
1735 }
1736 nr_failed += retry;
Huang Ying42012e02023-02-13 20:34:38 +08001737 stats->nr_thp_failed += thp_retry;
1738 stats->nr_failed_pages += nr_retry_pages;
Huang Ying5dfab102023-02-13 20:34:40 +08001739move:
Huang Ying7e12beb2023-02-13 20:34:43 +08001740 /* Flush TLBs for all unmapped folios */
1741 try_to_unmap_flush();
1742
Huang Ying5dfab102023-02-13 20:34:40 +08001743 retry = 1;
Huang Ying124abce2023-05-10 11:18:29 +08001744 for (pass = 0; pass < nr_pass && retry; pass++) {
Huang Ying5dfab102023-02-13 20:34:40 +08001745 retry = 0;
Huang Ying5dfab102023-02-13 20:34:40 +08001746 thp_retry = 0;
1747 nr_retry_pages = 0;
1748
1749 dst = list_first_entry(&dst_folios, struct folio, lru);
1750 dst2 = list_next_entry(dst, lru);
1751 list_for_each_entry_safe(folio, folio2, &unmap_folios, lru) {
Huang Ying124abce2023-05-10 11:18:29 +08001752 is_thp = folio_test_large(folio) && folio_test_pmd_mappable(folio);
Huang Ying5dfab102023-02-13 20:34:40 +08001753 nr_pages = folio_nr_pages(folio);
1754
1755 cond_resched();
1756
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001757 rc = migrate_folio_move(put_new_folio, private,
Huang Ying5dfab102023-02-13 20:34:40 +08001758 folio, dst, mode,
1759 reason, ret_folios);
1760 /*
1761 * The rules are:
1762 * Success: folio will be freed
1763 * -EAGAIN: stay on the unmap_folios list
1764 * Other errno: put on ret_folios list
1765 */
1766 switch(rc) {
1767 case -EAGAIN:
Huang Ying124abce2023-05-10 11:18:29 +08001768 retry++;
1769 thp_retry += is_thp;
Huang Ying5dfab102023-02-13 20:34:40 +08001770 nr_retry_pages += nr_pages;
1771 break;
1772 case MIGRATEPAGE_SUCCESS:
1773 stats->nr_succeeded += nr_pages;
1774 stats->nr_thp_succeeded += is_thp;
1775 break;
1776 default:
Huang Ying124abce2023-05-10 11:18:29 +08001777 nr_failed++;
1778 stats->nr_thp_failed += is_thp;
Huang Ying5dfab102023-02-13 20:34:40 +08001779 stats->nr_failed_pages += nr_pages;
1780 break;
1781 }
1782 dst = dst2;
1783 dst2 = list_next_entry(dst, lru);
1784 }
1785 }
1786 nr_failed += retry;
Huang Ying5dfab102023-02-13 20:34:40 +08001787 stats->nr_thp_failed += thp_retry;
1788 stats->nr_failed_pages += nr_retry_pages;
1789
Huang Ying124abce2023-05-10 11:18:29 +08001790 rc = rc_saved ? : nr_failed;
Huang Ying5dfab102023-02-13 20:34:40 +08001791out:
1792 /* Cleanup remaining folios */
1793 dst = list_first_entry(&dst_folios, struct folio, lru);
1794 dst2 = list_next_entry(dst, lru);
1795 list_for_each_entry_safe(folio, folio2, &unmap_folios, lru) {
1796 int page_was_mapped = 0;
1797 struct anon_vma *anon_vma = NULL;
1798
1799 __migrate_folio_extract(dst, &page_was_mapped, &anon_vma);
1800 migrate_folio_undo_src(folio, page_was_mapped, anon_vma,
Huang Yingebe75e42023-02-13 20:34:42 +08001801 true, ret_folios);
Huang Ying5dfab102023-02-13 20:34:40 +08001802 list_del(&dst->lru);
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001803 migrate_folio_undo_dst(dst, true, put_new_folio, private);
Huang Ying5dfab102023-02-13 20:34:40 +08001804 dst = dst2;
1805 dst2 = list_next_entry(dst, lru);
1806 }
1807
Huang Ying42012e02023-02-13 20:34:38 +08001808 return rc;
1809}
1810
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001811static int migrate_pages_sync(struct list_head *from, new_folio_t get_new_folio,
1812 free_folio_t put_new_folio, unsigned long private,
1813 enum migrate_mode mode, int reason,
1814 struct list_head *ret_folios, struct list_head *split_folios,
1815 struct migrate_pages_stats *stats)
Huang Ying2ef7dbb2023-03-03 11:01:55 +08001816{
1817 int rc, nr_failed = 0;
1818 LIST_HEAD(folios);
1819 struct migrate_pages_stats astats;
1820
1821 memset(&astats, 0, sizeof(astats));
1822 /* Try to migrate in batch with MIGRATE_ASYNC mode firstly */
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001823 rc = migrate_pages_batch(from, get_new_folio, put_new_folio, private, MIGRATE_ASYNC,
Huang Ying2ef7dbb2023-03-03 11:01:55 +08001824 reason, &folios, split_folios, &astats,
1825 NR_MAX_MIGRATE_ASYNC_RETRY);
1826 stats->nr_succeeded += astats.nr_succeeded;
1827 stats->nr_thp_succeeded += astats.nr_thp_succeeded;
1828 stats->nr_thp_split += astats.nr_thp_split;
1829 if (rc < 0) {
1830 stats->nr_failed_pages += astats.nr_failed_pages;
1831 stats->nr_thp_failed += astats.nr_thp_failed;
1832 list_splice_tail(&folios, ret_folios);
1833 return rc;
1834 }
1835 stats->nr_thp_failed += astats.nr_thp_split;
1836 nr_failed += astats.nr_thp_split;
1837 /*
1838 * Fall back to migrate all failed folios one by one synchronously. All
1839 * failed folios except split THPs will be retried, so their failure
1840 * isn't counted
1841 */
1842 list_splice_tail_init(&folios, from);
1843 while (!list_empty(from)) {
1844 list_move(from->next, &folios);
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001845 rc = migrate_pages_batch(&folios, get_new_folio, put_new_folio,
Huang Ying2ef7dbb2023-03-03 11:01:55 +08001846 private, mode, reason, ret_folios,
1847 split_folios, stats, NR_MAX_MIGRATE_SYNC_RETRY);
1848 list_splice_tail_init(&folios, ret_folios);
1849 if (rc < 0)
1850 return rc;
1851 nr_failed += rc;
1852 }
1853
1854 return nr_failed;
1855}
1856
Christoph Lameterb20a3502006-03-22 00:09:12 -08001857/*
Huang Yingeaec4e62022-11-09 09:23:48 +08001858 * migrate_pages - migrate the folios specified in a list, to the free folios
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001859 * supplied as the target for the page migration
Christoph Lameterb20a3502006-03-22 00:09:12 -08001860 *
Huang Yingeaec4e62022-11-09 09:23:48 +08001861 * @from: The list of folios to be migrated.
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001862 * @get_new_folio: The function used to allocate free folios to be used
Huang Yingeaec4e62022-11-09 09:23:48 +08001863 * as the target of the folio migration.
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001864 * @put_new_folio: The function used to free target folios if migration
David Rientjes68711a72014-06-04 16:08:25 -07001865 * fails, or NULL if no special handling is necessary.
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001866 * @private: Private data to be passed on to get_new_folio()
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001867 * @mode: The migration mode that specifies the constraints for
Huang Yingeaec4e62022-11-09 09:23:48 +08001868 * folio migration, if any.
1869 * @reason: The reason for folio migration.
1870 * @ret_succeeded: Set to the number of folios migrated successfully if
Yang Shi5ac95882021-09-02 14:59:13 -07001871 * the caller passes a non-NULL pointer.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001872 *
Huang Yinge5bfff8b2023-02-13 20:34:37 +08001873 * The function returns after NR_MAX_MIGRATE_PAGES_RETRY attempts or if no folios
1874 * are movable any more because the list has become empty or no retryable folios
1875 * exist any more. It is caller's responsibility to call putback_movable_pages()
1876 * only if ret != 0.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001877 *
Huang Yingeaec4e62022-11-09 09:23:48 +08001878 * Returns the number of {normal folio, large folio, hugetlb} that were not
1879 * migrated, or an error code. The number of large folio splits will be
1880 * considered as the number of non-migrated large folio, no matter how many
1881 * split folios of the large folio are migrated successfully.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001882 */
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001883int migrate_pages(struct list_head *from, new_folio_t get_new_folio,
1884 free_folio_t put_new_folio, unsigned long private,
Yang Shi5ac95882021-09-02 14:59:13 -07001885 enum migrate_mode mode, int reason, unsigned int *ret_succeeded)
Christoph Lameterb20a3502006-03-22 00:09:12 -08001886{
Huang Ying42012e02023-02-13 20:34:38 +08001887 int rc, rc_gather;
Huang Ying2ef7dbb2023-03-03 11:01:55 +08001888 int nr_pages;
Huang Yingeaec4e62022-11-09 09:23:48 +08001889 struct folio *folio, *folio2;
Huang Ying42012e02023-02-13 20:34:38 +08001890 LIST_HEAD(folios);
Huang Yingeaec4e62022-11-09 09:23:48 +08001891 LIST_HEAD(ret_folios);
Huang Yinga21d2132023-03-03 11:01:54 +08001892 LIST_HEAD(split_folios);
Huang Ying5b855932023-02-13 20:34:36 +08001893 struct migrate_pages_stats stats;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001894
Liam Mark7bc1aec2021-05-04 18:37:25 -07001895 trace_mm_migrate_pages_start(mode, reason);
1896
Huang Ying5b855932023-02-13 20:34:36 +08001897 memset(&stats, 0, sizeof(stats));
Christoph Lameterb20a3502006-03-22 00:09:12 -08001898
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001899 rc_gather = migrate_hugetlbs(from, get_new_folio, put_new_folio, private,
Huang Ying42012e02023-02-13 20:34:38 +08001900 mode, reason, &stats, &ret_folios);
1901 if (rc_gather < 0)
Huang Yinge5bfff8b2023-02-13 20:34:37 +08001902 goto out;
Huang Yingfb3592c2023-03-03 11:01:53 +08001903
Huang Ying42012e02023-02-13 20:34:38 +08001904again:
1905 nr_pages = 0;
1906 list_for_each_entry_safe(folio, folio2, from, lru) {
1907 /* Retried hugetlb folios will be kept in list */
1908 if (folio_test_hugetlb(folio)) {
1909 list_move_tail(&folio->lru, &ret_folios);
1910 continue;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001911 }
Baolin Wangb5bade92022-01-14 14:08:34 -08001912
Huang Ying42012e02023-02-13 20:34:38 +08001913 nr_pages += folio_nr_pages(folio);
Huang Ying2ef7dbb2023-03-03 11:01:55 +08001914 if (nr_pages >= NR_MAX_BATCHED_MIGRATION)
Huang Ying42012e02023-02-13 20:34:38 +08001915 break;
1916 }
Huang Ying2ef7dbb2023-03-03 11:01:55 +08001917 if (nr_pages >= NR_MAX_BATCHED_MIGRATION)
Huang Yingfb3592c2023-03-03 11:01:53 +08001918 list_cut_before(&folios, from, &folio2->lru);
Huang Ying42012e02023-02-13 20:34:38 +08001919 else
1920 list_splice_init(from, &folios);
Huang Ying2ef7dbb2023-03-03 11:01:55 +08001921 if (mode == MIGRATE_ASYNC)
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001922 rc = migrate_pages_batch(&folios, get_new_folio, put_new_folio,
1923 private, mode, reason, &ret_folios,
1924 &split_folios, &stats,
1925 NR_MAX_MIGRATE_PAGES_RETRY);
Huang Ying2ef7dbb2023-03-03 11:01:55 +08001926 else
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001927 rc = migrate_pages_sync(&folios, get_new_folio, put_new_folio,
1928 private, mode, reason, &ret_folios,
1929 &split_folios, &stats);
Huang Ying42012e02023-02-13 20:34:38 +08001930 list_splice_tail_init(&folios, &ret_folios);
1931 if (rc < 0) {
1932 rc_gather = rc;
Huang Yinga21d2132023-03-03 11:01:54 +08001933 list_splice_tail(&split_folios, &ret_folios);
Huang Ying42012e02023-02-13 20:34:38 +08001934 goto out;
1935 }
Huang Yinga21d2132023-03-03 11:01:54 +08001936 if (!list_empty(&split_folios)) {
1937 /*
1938 * Failure isn't counted since all split folios of a large folio
1939 * is counted as 1 failure already. And, we only try to migrate
1940 * with minimal effort, force MIGRATE_ASYNC mode and retry once.
1941 */
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001942 migrate_pages_batch(&split_folios, get_new_folio,
1943 put_new_folio, private, MIGRATE_ASYNC, reason,
1944 &ret_folios, NULL, &stats, 1);
Huang Yinga21d2132023-03-03 11:01:54 +08001945 list_splice_tail_init(&split_folios, &ret_folios);
1946 }
Huang Ying42012e02023-02-13 20:34:38 +08001947 rc_gather += rc;
1948 if (!list_empty(from))
1949 goto again;
Christoph Lameter95a402c2006-06-23 02:03:53 -07001950out:
Yang Shidd4ae782020-12-14 19:13:06 -08001951 /*
Huang Yingeaec4e62022-11-09 09:23:48 +08001952 * Put the permanent failure folio back to migration list, they
Yang Shidd4ae782020-12-14 19:13:06 -08001953 * will be put back to the right list by the caller.
1954 */
Huang Yingeaec4e62022-11-09 09:23:48 +08001955 list_splice(&ret_folios, from);
Yang Shidd4ae782020-12-14 19:13:06 -08001956
Baolin Wang03e5f822022-10-24 16:34:21 +08001957 /*
Huang Yingeaec4e62022-11-09 09:23:48 +08001958 * Return 0 in case all split folios of fail-to-migrate large folios
1959 * are migrated successfully.
Baolin Wang03e5f822022-10-24 16:34:21 +08001960 */
1961 if (list_empty(from))
Huang Ying42012e02023-02-13 20:34:38 +08001962 rc_gather = 0;
Baolin Wang03e5f822022-10-24 16:34:21 +08001963
Huang Ying5b855932023-02-13 20:34:36 +08001964 count_vm_events(PGMIGRATE_SUCCESS, stats.nr_succeeded);
1965 count_vm_events(PGMIGRATE_FAIL, stats.nr_failed_pages);
1966 count_vm_events(THP_MIGRATION_SUCCESS, stats.nr_thp_succeeded);
1967 count_vm_events(THP_MIGRATION_FAIL, stats.nr_thp_failed);
1968 count_vm_events(THP_MIGRATION_SPLIT, stats.nr_thp_split);
1969 trace_mm_migrate_pages(stats.nr_succeeded, stats.nr_failed_pages,
1970 stats.nr_thp_succeeded, stats.nr_thp_failed,
1971 stats.nr_thp_split, mode, reason);
Mel Gorman7b2a2d42012-10-19 14:07:31 +01001972
Yang Shi5ac95882021-09-02 14:59:13 -07001973 if (ret_succeeded)
Huang Ying5b855932023-02-13 20:34:36 +08001974 *ret_succeeded = stats.nr_succeeded;
Yang Shi5ac95882021-09-02 14:59:13 -07001975
Huang Ying42012e02023-02-13 20:34:38 +08001976 return rc_gather;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001977}
1978
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001979struct folio *alloc_migration_target(struct folio *src, unsigned long private)
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001980{
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001981 struct migration_target_control *mtc;
1982 gfp_t gfp_mask;
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001983 unsigned int order = 0;
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001984 int nid;
1985 int zidx;
1986
1987 mtc = (struct migration_target_control *)private;
1988 gfp_mask = mtc->gfp_mask;
1989 nid = mtc->nid;
1990 if (nid == NUMA_NO_NODE)
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001991 nid = folio_nid(src);
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001992
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001993 if (folio_test_hugetlb(src)) {
1994 struct hstate *h = folio_hstate(src);
Joonsoo Kimd92bbc22020-08-11 18:37:17 -07001995
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001996 gfp_mask = htlb_modify_alloc_mask(h, gfp_mask);
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01001997 return alloc_hugetlb_folio_nodemask(h, nid,
Sidhartha Kumare37d3e82023-01-13 16:30:55 -06001998 mtc->nmask, gfp_mask);
Joonsoo Kimd92bbc22020-08-11 18:37:17 -07001999 }
Joonsoo Kimb4b38222020-08-11 18:37:14 -07002000
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01002001 if (folio_test_large(src)) {
Joonsoo Kim9933a0c2020-08-11 18:37:20 -07002002 /*
2003 * clear __GFP_RECLAIM to make the migration callback
2004 * consistent with regular THP allocations.
2005 */
2006 gfp_mask &= ~__GFP_RECLAIM;
Joonsoo Kimb4b38222020-08-11 18:37:14 -07002007 gfp_mask |= GFP_TRANSHUGE;
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01002008 order = folio_order(src);
Joonsoo Kimb4b38222020-08-11 18:37:14 -07002009 }
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01002010 zidx = zone_idx(folio_zone(src));
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07002011 if (is_highmem_idx(zidx) || zidx == ZONE_MOVABLE)
Joonsoo Kimb4b38222020-08-11 18:37:14 -07002012 gfp_mask |= __GFP_HIGHMEM;
2013
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01002014 return __folio_alloc(gfp_mask, order, nid, mtc->nmask);
Joonsoo Kimb4b38222020-08-11 18:37:14 -07002015}
2016
Christoph Lameter742755a2006-06-23 02:03:55 -07002017#ifdef CONFIG_NUMA
Christoph Lameter742755a2006-06-23 02:03:55 -07002018
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002019static int store_status(int __user *status, int start, int value, int nr)
Christoph Lameter742755a2006-06-23 02:03:55 -07002020{
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002021 while (nr-- > 0) {
2022 if (put_user(value, status + start))
2023 return -EFAULT;
2024 start++;
2025 }
Christoph Lameter742755a2006-06-23 02:03:55 -07002026
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002027 return 0;
2028}
Christoph Lameter742755a2006-06-23 02:03:55 -07002029
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002030static int do_move_pages_to_node(struct mm_struct *mm,
2031 struct list_head *pagelist, int node)
2032{
2033 int err;
Joonsoo Kima0976312020-08-11 18:37:28 -07002034 struct migration_target_control mtc = {
2035 .nid = node,
2036 .gfp_mask = GFP_HIGHUSER_MOVABLE | __GFP_THISNODE,
2037 };
Christoph Lameter742755a2006-06-23 02:03:55 -07002038
Joonsoo Kima0976312020-08-11 18:37:28 -07002039 err = migrate_pages(pagelist, alloc_migration_target, NULL,
Yang Shi5ac95882021-09-02 14:59:13 -07002040 (unsigned long)&mtc, MIGRATE_SYNC, MR_SYSCALL, NULL);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002041 if (err)
2042 putback_movable_pages(pagelist);
2043 return err;
Christoph Lameter742755a2006-06-23 02:03:55 -07002044}
2045
2046/*
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002047 * Resolves the given address to a struct page, isolates it from the LRU and
2048 * puts it to the given pagelist.
Yang Shie0153fc2020-01-04 12:59:46 -08002049 * Returns:
2050 * errno - if the page cannot be found/isolated
2051 * 0 - when it doesn't have to be migrated because it is already on the
2052 * target node
2053 * 1 - when it has been queued
Christoph Lameter742755a2006-06-23 02:03:55 -07002054 */
Kirill A. Shutemov428e1062023-03-12 14:26:00 +03002055static int add_page_for_migration(struct mm_struct *mm, const void __user *p,
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002056 int node, struct list_head *pagelist, bool migrate_all)
Christoph Lameter742755a2006-06-23 02:03:55 -07002057{
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002058 struct vm_area_struct *vma;
Kirill A. Shutemov428e1062023-03-12 14:26:00 +03002059 unsigned long addr;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002060 struct page *page;
Christoph Lameter742755a2006-06-23 02:03:55 -07002061 int err;
Baolin Wang9747b9e2023-02-15 18:39:36 +08002062 bool isolated;
Christoph Lameter742755a2006-06-23 02:03:55 -07002063
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002064 mmap_read_lock(mm);
Kirill A. Shutemov428e1062023-03-12 14:26:00 +03002065 addr = (unsigned long)untagged_addr_remote(mm, p);
2066
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002067 err = -EFAULT;
Miaohe Lincb1c37b2022-04-28 23:16:07 -07002068 vma = vma_lookup(mm, addr);
2069 if (!vma || !vma_migratable(vma))
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002070 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07002071
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002072 /* FOLL_DUMP to ignore special (like zero) pages */
Miaohe Lin87d27622022-03-22 14:45:29 -07002073 page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP);
Christoph Lameter742755a2006-06-23 02:03:55 -07002074
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002075 err = PTR_ERR(page);
2076 if (IS_ERR(page))
2077 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07002078
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002079 err = -ENOENT;
Haiyue Wangf7091ed2022-08-23 21:58:41 +08002080 if (!page)
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002081 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07002082
Haiyue Wangf7091ed2022-08-23 21:58:41 +08002083 if (is_zone_device_page(page))
2084 goto out_putpage;
2085
Brice Gogline78bbfa2008-10-18 20:27:15 -07002086 err = 0;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002087 if (page_to_nid(page) == node)
2088 goto out_putpage;
Christoph Lameter742755a2006-06-23 02:03:55 -07002089
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002090 err = -EACCES;
2091 if (page_mapcount(page) > 1 && !migrate_all)
2092 goto out_putpage;
2093
2094 if (PageHuge(page)) {
2095 if (PageHead(page)) {
Baolin Wang9747b9e2023-02-15 18:39:36 +08002096 isolated = isolate_hugetlb(page_folio(page), pagelist);
2097 err = isolated ? 1 : -EBUSY;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002098 }
2099 } else {
2100 struct page *head;
2101
2102 head = compound_head(page);
Baolin Wangf7f9c002023-02-15 18:39:35 +08002103 isolated = isolate_lru_page(head);
2104 if (!isolated) {
2105 err = -EBUSY;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002106 goto out_putpage;
Baolin Wangf7f9c002023-02-15 18:39:35 +08002107 }
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002108
Yang Shie0153fc2020-01-04 12:59:46 -08002109 err = 1;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002110 list_add_tail(&head->lru, pagelist);
2111 mod_node_page_state(page_pgdat(head),
Huang Ying9de4f222020-04-06 20:04:41 -07002112 NR_ISOLATED_ANON + page_is_file_lru(head),
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -07002113 thp_nr_pages(head));
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002114 }
2115out_putpage:
2116 /*
2117 * Either remove the duplicate refcount from
2118 * isolate_lru_page() or drop the page ref if it was
2119 * not isolated.
2120 */
2121 put_page(page);
2122out:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002123 mmap_read_unlock(mm);
Christoph Lameter742755a2006-06-23 02:03:55 -07002124 return err;
2125}
2126
Wei Yang7ca87832020-04-06 20:04:12 -07002127static int move_pages_and_store_status(struct mm_struct *mm, int node,
2128 struct list_head *pagelist, int __user *status,
2129 int start, int i, unsigned long nr_pages)
2130{
2131 int err;
2132
Wei Yang5d7ae892020-04-06 20:04:15 -07002133 if (list_empty(pagelist))
2134 return 0;
2135
Wei Yang7ca87832020-04-06 20:04:12 -07002136 err = do_move_pages_to_node(mm, pagelist, node);
2137 if (err) {
2138 /*
2139 * Positive err means the number of failed
2140 * pages to migrate. Since we are going to
2141 * abort and return the number of non-migrated
Long Liab9dd4f2020-12-14 19:12:52 -08002142 * pages, so need to include the rest of the
Wei Yang7ca87832020-04-06 20:04:12 -07002143 * nr_pages that have not been attempted as
2144 * well.
2145 */
2146 if (err > 0)
Huang Yinga7504ed2022-08-17 16:14:01 +08002147 err += nr_pages - i;
Wei Yang7ca87832020-04-06 20:04:12 -07002148 return err;
2149 }
2150 return store_status(status, start, node, i - start);
2151}
2152
Christoph Lameter742755a2006-06-23 02:03:55 -07002153/*
Brice Goglin5e9a0f02008-10-18 20:27:17 -07002154 * Migrate an array of page address onto an array of nodes and fill
2155 * the corresponding array of status.
2156 */
Christoph Lameter3268c632012-03-21 16:34:06 -07002157static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
Brice Goglin5e9a0f02008-10-18 20:27:17 -07002158 unsigned long nr_pages,
2159 const void __user * __user *pages,
2160 const int __user *nodes,
2161 int __user *status, int flags)
2162{
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002163 int current_node = NUMA_NO_NODE;
2164 LIST_HEAD(pagelist);
2165 int start, i;
2166 int err = 0, err1;
Brice Goglin35282a22009-06-16 15:32:43 -07002167
Minchan Kim361a2a22021-05-04 18:36:57 -07002168 lru_cache_disable();
Brice Goglin35282a22009-06-16 15:32:43 -07002169
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002170 for (i = start = 0; i < nr_pages; i++) {
2171 const void __user *p;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002172 int node;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07002173
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002174 err = -EFAULT;
2175 if (get_user(p, pages + i))
2176 goto out_flush;
2177 if (get_user(node, nodes + i))
2178 goto out_flush;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07002179
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002180 err = -ENODEV;
2181 if (node < 0 || node >= MAX_NUMNODES)
2182 goto out_flush;
2183 if (!node_state(node, N_MEMORY))
2184 goto out_flush;
Brice Goglin3140a222009-01-06 14:38:57 -08002185
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002186 err = -EACCES;
2187 if (!node_isset(node, task_nodes))
2188 goto out_flush;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07002189
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002190 if (current_node == NUMA_NO_NODE) {
2191 current_node = node;
2192 start = i;
2193 } else if (node != current_node) {
Wei Yang7ca87832020-04-06 20:04:12 -07002194 err = move_pages_and_store_status(mm, current_node,
2195 &pagelist, status, start, i, nr_pages);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002196 if (err)
2197 goto out;
2198 start = i;
2199 current_node = node;
Brice Goglin3140a222009-01-06 14:38:57 -08002200 }
Brice Goglin5e9a0f02008-10-18 20:27:17 -07002201
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002202 /*
2203 * Errors in the page lookup or isolation are not fatal and we simply
2204 * report them via status
2205 */
Kirill A. Shutemov428e1062023-03-12 14:26:00 +03002206 err = add_page_for_migration(mm, p, current_node, &pagelist,
2207 flags & MPOL_MF_MOVE_ALL);
Yang Shie0153fc2020-01-04 12:59:46 -08002208
Wei Yangd08221a2020-04-06 20:04:18 -07002209 if (err > 0) {
Yang Shie0153fc2020-01-04 12:59:46 -08002210 /* The page is successfully queued for migration */
2211 continue;
2212 }
Brice Goglin3140a222009-01-06 14:38:57 -08002213
Wei Yangd08221a2020-04-06 20:04:18 -07002214 /*
John Hubbard65462462022-03-22 14:39:40 -07002215 * The move_pages() man page does not have an -EEXIST choice, so
2216 * use -EFAULT instead.
2217 */
2218 if (err == -EEXIST)
2219 err = -EFAULT;
2220
2221 /*
Wei Yangd08221a2020-04-06 20:04:18 -07002222 * If the page is already on the target node (!err), store the
2223 * node, otherwise, store the err.
2224 */
2225 err = store_status(status, i, err ? : current_node, 1);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002226 if (err)
2227 goto out_flush;
Brice Goglin3140a222009-01-06 14:38:57 -08002228
Wei Yang7ca87832020-04-06 20:04:12 -07002229 err = move_pages_and_store_status(mm, current_node, &pagelist,
2230 status, start, i, nr_pages);
Huang Yinga7504ed2022-08-17 16:14:01 +08002231 if (err) {
2232 /* We have accounted for page i */
2233 if (err > 0)
2234 err--;
Wei Yang4afdace2020-04-06 20:04:09 -07002235 goto out;
Huang Yinga7504ed2022-08-17 16:14:01 +08002236 }
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002237 current_node = NUMA_NO_NODE;
Brice Goglin3140a222009-01-06 14:38:57 -08002238 }
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002239out_flush:
2240 /* Make sure we do not overwrite the existing error */
Wei Yang7ca87832020-04-06 20:04:12 -07002241 err1 = move_pages_and_store_status(mm, current_node, &pagelist,
2242 status, start, i, nr_pages);
Wei Yangdfe9aa22020-01-30 22:11:14 -08002243 if (err >= 0)
Michal Hockoa49bd4d2018-04-10 16:29:59 -07002244 err = err1;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07002245out:
Minchan Kim361a2a22021-05-04 18:36:57 -07002246 lru_cache_enable();
Brice Goglin5e9a0f02008-10-18 20:27:17 -07002247 return err;
2248}
2249
2250/*
Brice Goglin2f007e72008-10-18 20:27:16 -07002251 * Determine the nodes of an array of pages and store it in an array of status.
Christoph Lameter742755a2006-06-23 02:03:55 -07002252 */
Brice Goglin80bba122008-12-09 13:14:23 -08002253static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
2254 const void __user **pages, int *status)
Christoph Lameter742755a2006-06-23 02:03:55 -07002255{
Brice Goglin2f007e72008-10-18 20:27:16 -07002256 unsigned long i;
Brice Goglin2f007e72008-10-18 20:27:16 -07002257
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002258 mmap_read_lock(mm);
Christoph Lameter742755a2006-06-23 02:03:55 -07002259
Brice Goglin2f007e72008-10-18 20:27:16 -07002260 for (i = 0; i < nr_pages; i++) {
Brice Goglin80bba122008-12-09 13:14:23 -08002261 unsigned long addr = (unsigned long)(*pages);
Christoph Lameter742755a2006-06-23 02:03:55 -07002262 struct vm_area_struct *vma;
2263 struct page *page;
KOSAKI Motohiroc095adb2008-12-16 16:06:43 +09002264 int err = -EFAULT;
Brice Goglin2f007e72008-10-18 20:27:16 -07002265
Liam Howlett059b8b42021-06-28 19:39:44 -07002266 vma = vma_lookup(mm, addr);
2267 if (!vma)
Christoph Lameter742755a2006-06-23 02:03:55 -07002268 goto set_status;
2269
Kirill A. Shutemovd8998442015-09-04 15:47:53 -07002270 /* FOLL_DUMP to ignore special (like zero) pages */
Baolin Wang16fd6b32022-11-09 16:40:27 +08002271 page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07002272
2273 err = PTR_ERR(page);
2274 if (IS_ERR(page))
2275 goto set_status;
2276
Haiyue Wangf7091ed2022-08-23 21:58:41 +08002277 err = -ENOENT;
2278 if (!page)
2279 goto set_status;
2280
2281 if (!is_zone_device_page(page))
Miaohe Lin4cd61482022-04-28 23:16:08 -07002282 err = page_to_nid(page);
Haiyue Wangf7091ed2022-08-23 21:58:41 +08002283
Baolin Wang16fd6b32022-11-09 16:40:27 +08002284 put_page(page);
Christoph Lameter742755a2006-06-23 02:03:55 -07002285set_status:
Brice Goglin80bba122008-12-09 13:14:23 -08002286 *status = err;
2287
2288 pages++;
2289 status++;
2290 }
2291
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002292 mmap_read_unlock(mm);
Brice Goglin80bba122008-12-09 13:14:23 -08002293}
2294
Arnd Bergmann5b1b5612021-09-08 15:18:17 -07002295static int get_compat_pages_array(const void __user *chunk_pages[],
2296 const void __user * __user *pages,
2297 unsigned long chunk_nr)
2298{
2299 compat_uptr_t __user *pages32 = (compat_uptr_t __user *)pages;
2300 compat_uptr_t p;
2301 int i;
2302
2303 for (i = 0; i < chunk_nr; i++) {
2304 if (get_user(p, pages32 + i))
2305 return -EFAULT;
2306 chunk_pages[i] = compat_ptr(p);
2307 }
2308
2309 return 0;
2310}
2311
Brice Goglin80bba122008-12-09 13:14:23 -08002312/*
2313 * Determine the nodes of a user array of pages and store it in
2314 * a user array of status.
2315 */
2316static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
2317 const void __user * __user *pages,
2318 int __user *status)
2319{
Miaohe Lin3eefb822022-04-28 23:16:07 -07002320#define DO_PAGES_STAT_CHUNK_NR 16UL
Brice Goglin80bba122008-12-09 13:14:23 -08002321 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
2322 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
Brice Goglin80bba122008-12-09 13:14:23 -08002323
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08002324 while (nr_pages) {
Miaohe Lin3eefb822022-04-28 23:16:07 -07002325 unsigned long chunk_nr = min(nr_pages, DO_PAGES_STAT_CHUNK_NR);
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08002326
Arnd Bergmann5b1b5612021-09-08 15:18:17 -07002327 if (in_compat_syscall()) {
2328 if (get_compat_pages_array(chunk_pages, pages,
2329 chunk_nr))
2330 break;
2331 } else {
2332 if (copy_from_user(chunk_pages, pages,
2333 chunk_nr * sizeof(*chunk_pages)))
2334 break;
2335 }
Brice Goglin80bba122008-12-09 13:14:23 -08002336
2337 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
2338
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08002339 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
2340 break;
Christoph Lameter742755a2006-06-23 02:03:55 -07002341
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08002342 pages += chunk_nr;
2343 status += chunk_nr;
2344 nr_pages -= chunk_nr;
2345 }
2346 return nr_pages ? -EFAULT : 0;
Christoph Lameter742755a2006-06-23 02:03:55 -07002347}
2348
Miaohe Lin4dc200c2020-10-17 16:14:03 -07002349static struct mm_struct *find_mm_struct(pid_t pid, nodemask_t *mem_nodes)
2350{
2351 struct task_struct *task;
2352 struct mm_struct *mm;
2353
2354 /*
2355 * There is no need to check if current process has the right to modify
2356 * the specified process when they are same.
2357 */
2358 if (!pid) {
2359 mmget(current->mm);
2360 *mem_nodes = cpuset_mems_allowed(current);
2361 return current->mm;
2362 }
2363
2364 /* Find the mm_struct */
2365 rcu_read_lock();
2366 task = find_task_by_vpid(pid);
2367 if (!task) {
2368 rcu_read_unlock();
2369 return ERR_PTR(-ESRCH);
2370 }
2371 get_task_struct(task);
2372
2373 /*
2374 * Check if this process has the right to modify the specified
2375 * process. Use the regular "ptrace_may_access()" checks.
2376 */
2377 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
2378 rcu_read_unlock();
2379 mm = ERR_PTR(-EPERM);
2380 goto out;
2381 }
2382 rcu_read_unlock();
2383
2384 mm = ERR_PTR(security_task_movememory(task));
2385 if (IS_ERR(mm))
2386 goto out;
2387 *mem_nodes = cpuset_mems_allowed(task);
2388 mm = get_task_mm(task);
2389out:
2390 put_task_struct(task);
2391 if (!mm)
2392 mm = ERR_PTR(-EINVAL);
2393 return mm;
2394}
2395
Christoph Lameter742755a2006-06-23 02:03:55 -07002396/*
2397 * Move a list of pages in the address space of the currently executing
2398 * process.
2399 */
Dominik Brodowski7addf442018-03-17 16:08:03 +01002400static int kernel_move_pages(pid_t pid, unsigned long nr_pages,
2401 const void __user * __user *pages,
2402 const int __user *nodes,
2403 int __user *status, int flags)
Christoph Lameter742755a2006-06-23 02:03:55 -07002404{
Christoph Lameter742755a2006-06-23 02:03:55 -07002405 struct mm_struct *mm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07002406 int err;
Christoph Lameter3268c632012-03-21 16:34:06 -07002407 nodemask_t task_nodes;
Christoph Lameter742755a2006-06-23 02:03:55 -07002408
2409 /* Check flags */
2410 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
2411 return -EINVAL;
2412
2413 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
2414 return -EPERM;
2415
Miaohe Lin4dc200c2020-10-17 16:14:03 -07002416 mm = find_mm_struct(pid, &task_nodes);
2417 if (IS_ERR(mm))
2418 return PTR_ERR(mm);
Sasha Levin6e8b09e2012-04-25 16:01:53 -07002419
2420 if (nodes)
2421 err = do_pages_move(mm, task_nodes, nr_pages, pages,
2422 nodes, status, flags);
2423 else
2424 err = do_pages_stat(mm, nr_pages, pages, status);
Christoph Lameter3268c632012-03-21 16:34:06 -07002425
2426 mmput(mm);
2427 return err;
Christoph Lameter742755a2006-06-23 02:03:55 -07002428}
Christoph Lameter742755a2006-06-23 02:03:55 -07002429
Dominik Brodowski7addf442018-03-17 16:08:03 +01002430SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
2431 const void __user * __user *, pages,
2432 const int __user *, nodes,
2433 int __user *, status, int, flags)
2434{
2435 return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags);
2436}
2437
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002438#ifdef CONFIG_NUMA_BALANCING
2439/*
2440 * Returns true if this is a safe migration target node for misplaced NUMA
Wei Yangbc530082022-04-28 23:16:03 -07002441 * pages. Currently it only checks the watermarks which is crude.
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002442 */
2443static bool migrate_balanced_pgdat(struct pglist_data *pgdat,
Mel Gorman3abef4e2013-02-22 16:34:27 -08002444 unsigned long nr_migrate_pages)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002445{
2446 int z;
Mel Gorman599d0c92016-07-28 15:45:31 -07002447
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002448 for (z = pgdat->nr_zones - 1; z >= 0; z--) {
2449 struct zone *zone = pgdat->node_zones + z;
2450
Wei Yangbc530082022-04-28 23:16:03 -07002451 if (!managed_zone(zone))
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002452 continue;
2453
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002454 /* Avoid waking kswapd by allocating pages_to_migrate pages. */
2455 if (!zone_watermark_ok(zone, 0,
2456 high_wmark_pages(zone) +
2457 nr_migrate_pages,
Huang Yingbfe9d002019-11-30 17:57:28 -08002458 ZONE_MOVABLE, 0))
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002459 continue;
2460 return true;
2461 }
2462 return false;
2463}
2464
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01002465static struct folio *alloc_misplaced_dst_folio(struct folio *src,
Michal Hocko666feb22018-04-10 16:30:03 -07002466 unsigned long data)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002467{
2468 int nid = (int) data;
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01002469 int order = folio_order(src);
Matthew Wilcox (Oracle)c185e492021-07-06 10:50:39 -04002470 gfp_t gfp = __GFP_THISNODE;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002471
Matthew Wilcox (Oracle)c185e492021-07-06 10:50:39 -04002472 if (order > 0)
2473 gfp |= GFP_TRANSHUGE_LIGHT;
2474 else {
2475 gfp |= GFP_HIGHUSER_MOVABLE | __GFP_NOMEMALLOC | __GFP_NORETRY |
2476 __GFP_NOWARN;
2477 gfp &= ~__GFP_RECLAIM;
2478 }
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01002479 return __folio_alloc_node(gfp, order, nid);
Yang Shic5b5a3d2021-06-30 18:51:42 -07002480}
2481
Mel Gorman1c30e012014-01-21 15:50:58 -08002482static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
Mel Gormanb32967f2012-11-19 12:35:47 +00002483{
Baolin Wang2b9b6242021-09-08 15:18:01 -07002484 int nr_pages = thp_nr_pages(page);
Huang Yingc574bbe2022-03-22 14:46:23 -07002485 int order = compound_order(page);
Mel Gormanb32967f2012-11-19 12:35:47 +00002486
Huang Yingc574bbe2022-03-22 14:46:23 -07002487 VM_BUG_ON_PAGE(order && !PageTransHuge(page), page);
Mel Gorman3abef4e2013-02-22 16:34:27 -08002488
Yang Shi662aeea2021-06-30 18:51:51 -07002489 /* Do not migrate THP mapped by multiple processes */
2490 if (PageTransHuge(page) && total_mapcount(page) > 1)
2491 return 0;
2492
Mel Gormanb32967f2012-11-19 12:35:47 +00002493 /* Avoid migrating to a node that is nearly full */
Huang Yingc574bbe2022-03-22 14:46:23 -07002494 if (!migrate_balanced_pgdat(pgdat, nr_pages)) {
2495 int z;
2496
2497 if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING))
2498 return 0;
2499 for (z = pgdat->nr_zones - 1; z >= 0; z--) {
Wei Yangbc530082022-04-28 23:16:03 -07002500 if (managed_zone(pgdat->node_zones + z))
Huang Yingc574bbe2022-03-22 14:46:23 -07002501 break;
2502 }
2503 wakeup_kswapd(pgdat->node_zones + z, 0, order, ZONE_MOVABLE);
Hugh Dickins340ef392013-02-22 16:34:33 -08002504 return 0;
Huang Yingc574bbe2022-03-22 14:46:23 -07002505 }
Mel Gormanb32967f2012-11-19 12:35:47 +00002506
Baolin Wangf7f9c002023-02-15 18:39:35 +08002507 if (!isolate_lru_page(page))
Hugh Dickins340ef392013-02-22 16:34:33 -08002508 return 0;
Mel Gormanb32967f2012-11-19 12:35:47 +00002509
Miaohe Linb75454e2022-04-28 23:16:07 -07002510 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_is_file_lru(page),
Baolin Wang2b9b6242021-09-08 15:18:01 -07002511 nr_pages);
Hugh Dickins340ef392013-02-22 16:34:33 -08002512
2513 /*
2514 * Isolating the page has taken another reference, so the
2515 * caller's reference can be safely dropped without the page
2516 * disappearing underneath us during migration.
Mel Gormanb32967f2012-11-19 12:35:47 +00002517 */
2518 put_page(page);
Hugh Dickins340ef392013-02-22 16:34:33 -08002519 return 1;
Mel Gormanb32967f2012-11-19 12:35:47 +00002520}
2521
Mel Gormana8f60772012-11-14 21:41:46 +00002522/*
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002523 * Attempt to migrate a misplaced page to the specified destination
2524 * node. Caller is expected to have an elevated reference count on
2525 * the page that will be dropped by this function before returning.
2526 */
Mel Gorman1bc115d2013-10-07 11:29:05 +01002527int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma,
2528 int node)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002529{
Mel Gormana8f60772012-11-14 21:41:46 +00002530 pg_data_t *pgdat = NODE_DATA(node);
Hugh Dickins340ef392013-02-22 16:34:33 -08002531 int isolated;
Mel Gormanb32967f2012-11-19 12:35:47 +00002532 int nr_remaining;
Huang Yinge39bb6b2022-03-22 14:46:20 -07002533 unsigned int nr_succeeded;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002534 LIST_HEAD(migratepages);
Aneesh Kumar K.Vb5916c02021-07-29 14:53:47 -07002535 int nr_pages = thp_nr_pages(page);
Yang Shic5b5a3d2021-06-30 18:51:42 -07002536
2537 /*
Mel Gorman1bc115d2013-10-07 11:29:05 +01002538 * Don't migrate file pages that are mapped in multiple processes
2539 * with execute permissions as they are probably shared libraries.
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002540 */
Miaohe Lin7ee820e2021-05-04 18:37:16 -07002541 if (page_mapcount(page) != 1 && page_is_file_lru(page) &&
2542 (vma->vm_flags & VM_EXEC))
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002543 goto out;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002544
Mel Gormana8f60772012-11-14 21:41:46 +00002545 /*
Mel Gorman09a913a2018-04-10 16:29:20 -07002546 * Also do not migrate dirty pages as not all filesystems can move
2547 * dirty pages in MIGRATE_ASYNC mode which is a waste of cycles.
2548 */
Huang Ying9de4f222020-04-06 20:04:41 -07002549 if (page_is_file_lru(page) && PageDirty(page))
Mel Gorman09a913a2018-04-10 16:29:20 -07002550 goto out;
2551
Mel Gormanb32967f2012-11-19 12:35:47 +00002552 isolated = numamigrate_isolate_page(pgdat, page);
2553 if (!isolated)
2554 goto out;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002555
Mel Gormanb32967f2012-11-19 12:35:47 +00002556 list_add(&page->lru, &migratepages);
Matthew Wilcox (Oracle)4e096ae2023-05-13 01:11:01 +01002557 nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_folio,
Matthew Wilcox (Oracle)c185e492021-07-06 10:50:39 -04002558 NULL, node, MIGRATE_ASYNC,
2559 MR_NUMA_MISPLACED, &nr_succeeded);
Mel Gormanb32967f2012-11-19 12:35:47 +00002560 if (nr_remaining) {
Joonsoo Kim59c82b72014-01-21 15:51:17 -08002561 if (!list_empty(&migratepages)) {
2562 list_del(&page->lru);
Yang Shic5fc5c32021-06-30 18:51:45 -07002563 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
2564 page_is_file_lru(page), -nr_pages);
Joonsoo Kim59c82b72014-01-21 15:51:17 -08002565 putback_lru_page(page);
2566 }
Mel Gormanb32967f2012-11-19 12:35:47 +00002567 isolated = 0;
Huang Yinge39bb6b2022-03-22 14:46:20 -07002568 }
2569 if (nr_succeeded) {
2570 count_vm_numa_events(NUMA_PAGE_MIGRATE, nr_succeeded);
2571 if (!node_is_toptier(page_to_nid(page)) && node_is_toptier(node))
2572 mod_node_page_state(pgdat, PGPROMOTE_SUCCESS,
2573 nr_succeeded);
2574 }
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002575 BUG_ON(!list_empty(&migratepages));
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002576 return isolated;
Hugh Dickins340ef392013-02-22 16:34:33 -08002577
2578out:
2579 put_page(page);
2580 return 0;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002581}
Mel Gorman220018d2012-12-05 09:32:56 +00002582#endif /* CONFIG_NUMA_BALANCING */
Oscar Salvador7d6e2d92022-04-28 23:16:09 -07002583#endif /* CONFIG_NUMA */