blob: c7d5566623add598008621e98de015f517d34e24 [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>
Christoph Lameterb20a3502006-03-22 00:09:12 -080024#include <linux/pagevec.h>
Hugh Dickinse9995ef2009-12-14 17:59:31 -080025#include <linux/ksm.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080026#include <linux/rmap.h>
27#include <linux/topology.h>
28#include <linux/cpu.h>
29#include <linux/cpuset.h>
Christoph Lameter04e62a22006-06-23 02:03:38 -070030#include <linux/writeback.h>
Christoph Lameter742755a2006-06-23 02:03:55 -070031#include <linux/mempolicy.h>
32#include <linux/vmalloc.h>
David Quigley86c3a762006-06-23 02:04:02 -070033#include <linux/security.h>
Hugh Dickins42cb14b2015-11-05 18:50:05 -080034#include <linux/backing-dev.h>
Minchan Kimbda807d2016-07-26 15:23:05 -070035#include <linux/compaction.h>
Adrian Bunk4f5ca262008-07-23 21:27:02 -070036#include <linux/syscalls.h>
Dominik Brodowski7addf442018-03-17 16:08:03 +010037#include <linux/compat.h>
Naoya Horiguchi290408d2010-09-08 10:19:35 +090038#include <linux/hugetlb.h>
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -070039#include <linux/hugetlb_cgroup.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/gfp.h>
Christoph Hellwiga5201102019-08-28 16:19:53 +020041#include <linux/pagewalk.h>
Jérôme Glissedf6ad692017-09-08 16:12:24 -070042#include <linux/pfn_t.h>
Jérôme Glissea5430dd2017-09-08 16:12:17 -070043#include <linux/memremap.h>
Jérôme Glisse8315ada2017-09-08 16:12:21 -070044#include <linux/userfaultfd_k.h>
Rafael Aquinibf6bddf12012-12-11 16:02:42 -080045#include <linux/balloon_compaction.h>
Mel Gormanf714f4f2013-12-18 17:08:33 -080046#include <linux/mmu_notifier.h>
Vladimir Davydov33c3fc72015-09-09 15:35:45 -070047#include <linux/page_idle.h>
Vlastimil Babkad435edc2016-03-15 14:56:15 -070048#include <linux/page_owner.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010049#include <linux/sched/mm.h>
Linus Torvalds197e7e52017-08-20 13:26:27 -070050#include <linux/ptrace.h>
Ralph Campbell34290e22020-01-30 22:14:44 -080051#include <linux/oom.h>
Dave Hansen884a6e52021-09-02 14:59:09 -070052#include <linux/memory.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#define CREATE_TRACE_POINTS
57#include <trace/events/migrate.h>
58
Christoph Lameterb20a3502006-03-22 00:09:12 -080059#include "internal.h"
60
Yisheng Xie9e5bcd62017-02-24 14:57:29 -080061int isolate_movable_page(struct page *page, isolate_mode_t mode)
Minchan Kimbda807d2016-07-26 15:23:05 -070062{
63 struct address_space *mapping;
64
65 /*
66 * Avoid burning cycles with pages that are yet under __free_pages(),
67 * or just got freed under us.
68 *
69 * In case we 'win' a race for a movable page being freed under us and
70 * raise its refcount preventing __free_pages() from doing its job
71 * the put_page() at the end of this block will take care of
72 * release this page, thus avoiding a nasty leakage.
73 */
74 if (unlikely(!get_page_unless_zero(page)))
75 goto out;
76
77 /*
78 * Check PageMovable before holding a PG_lock because page's owner
79 * assumes anybody doesn't touch PG_lock of newly allocated page
Wei Yang8bb4e7a2019-03-05 15:46:22 -080080 * so unconditionally grabbing the lock ruins page's owner side.
Minchan Kimbda807d2016-07-26 15:23:05 -070081 */
82 if (unlikely(!__PageMovable(page)))
83 goto out_putpage;
84 /*
85 * As movable pages are not isolated from LRU lists, concurrent
86 * compaction threads can race against page migration functions
87 * as well as race against the releasing a page.
88 *
89 * In order to avoid having an already isolated movable page
90 * being (wrongly) re-isolated while it is under migration,
91 * or to avoid attempting to isolate pages being released,
92 * lets be sure we have the page lock
93 * before proceeding with the movable page isolation steps.
94 */
95 if (unlikely(!trylock_page(page)))
96 goto out_putpage;
97
98 if (!PageMovable(page) || PageIsolated(page))
99 goto out_no_isolated;
100
101 mapping = page_mapping(page);
102 VM_BUG_ON_PAGE(!mapping, page);
103
104 if (!mapping->a_ops->isolate_page(page, mode))
105 goto out_no_isolated;
106
107 /* Driver shouldn't use PG_isolated bit of page->flags */
108 WARN_ON_ONCE(PageIsolated(page));
109 __SetPageIsolated(page);
110 unlock_page(page);
111
Yisheng Xie9e5bcd62017-02-24 14:57:29 -0800112 return 0;
Minchan Kimbda807d2016-07-26 15:23:05 -0700113
114out_no_isolated:
115 unlock_page(page);
116out_putpage:
117 put_page(page);
118out:
Yisheng Xie9e5bcd62017-02-24 14:57:29 -0800119 return -EBUSY;
Minchan Kimbda807d2016-07-26 15:23:05 -0700120}
121
Miaohe Lin606a6f72021-05-04 18:37:04 -0700122static void putback_movable_page(struct page *page)
Minchan Kimbda807d2016-07-26 15:23:05 -0700123{
124 struct address_space *mapping;
125
Minchan Kimbda807d2016-07-26 15:23:05 -0700126 mapping = page_mapping(page);
127 mapping->a_ops->putback_page(page);
128 __ClearPageIsolated(page);
129}
130
Christoph Lameterb20a3502006-03-22 00:09:12 -0800131/*
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800132 * Put previously isolated pages back onto the appropriate lists
133 * from where they were once taken off for compaction/migration.
134 *
Joonsoo Kim59c82b72014-01-21 15:51:17 -0800135 * This function shall be used whenever the isolated pageset has been
136 * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range()
Miaohe Lin072e7412022-05-30 19:30:15 +0800137 * and isolate_hugetlb().
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800138 */
139void putback_movable_pages(struct list_head *l)
140{
141 struct page *page;
142 struct page *page2;
143
144 list_for_each_entry_safe(page, page2, l, lru) {
Naoya Horiguchi31caf662013-09-11 14:21:59 -0700145 if (unlikely(PageHuge(page))) {
146 putback_active_hugepage(page);
147 continue;
148 }
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800149 list_del(&page->lru);
Minchan Kimbda807d2016-07-26 15:23:05 -0700150 /*
151 * We isolated non-lru movable page so here we can use
152 * __PageMovable because LRU page's mapping cannot have
153 * PAGE_MAPPING_MOVABLE.
154 */
Minchan Kimb1123ea62016-07-26 15:23:09 -0700155 if (unlikely(__PageMovable(page))) {
Minchan Kimbda807d2016-07-26 15:23:05 -0700156 VM_BUG_ON_PAGE(!PageIsolated(page), page);
157 lock_page(page);
158 if (PageMovable(page))
159 putback_movable_page(page);
160 else
161 __ClearPageIsolated(page);
162 unlock_page(page);
163 put_page(page);
164 } else {
Naoya Horiguchie8db67e2017-09-08 16:11:12 -0700165 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -0700166 page_is_file_lru(page), -thp_nr_pages(page));
Rabin Vincentfc280fe2017-04-20 14:37:46 -0700167 putback_lru_page(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700168 }
Christoph Lameterb20a3502006-03-22 00:09:12 -0800169 }
Christoph Lameterb20a3502006-03-22 00:09:12 -0800170}
171
Christoph Lameter06972122006-06-23 02:03:35 -0700172/*
173 * Restore a potential migration pte to a working pte entry
174 */
Minchan Kime4b82222017-05-03 14:54:27 -0700175static bool remove_migration_pte(struct page *page, struct vm_area_struct *vma,
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800176 unsigned long addr, void *old)
Christoph Lameter06972122006-06-23 02:03:35 -0700177{
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800178 struct page_vma_mapped_walk pvmw = {
179 .page = old,
180 .vma = vma,
181 .address = addr,
182 .flags = PVMW_SYNC | PVMW_MIGRATION,
183 };
184 struct page *new;
185 pte_t pte;
Christoph Lameter06972122006-06-23 02:03:35 -0700186 swp_entry_t entry;
Christoph Lameter06972122006-06-23 02:03:35 -0700187
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800188 VM_BUG_ON_PAGE(PageTail(page), page);
189 while (page_vma_mapped_walk(&pvmw)) {
Naoya Horiguchi4b0ece62017-03-31 15:11:44 -0700190 if (PageKsm(page))
191 new = page;
192 else
193 new = page - pvmw.page->index +
194 linear_page_index(vma, pvmw.address);
Christoph Lameter06972122006-06-23 02:03:35 -0700195
Zi Yan616b8372017-09-08 16:10:57 -0700196#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
197 /* PMD-mapped THP migration entry */
198 if (!pvmw.pte) {
199 VM_BUG_ON_PAGE(PageHuge(page) || !PageTransCompound(page), page);
200 remove_migration_pmd(&pvmw, new);
201 continue;
202 }
203#endif
204
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800205 get_page(new);
206 pte = pte_mkold(mk_pte(new, READ_ONCE(vma->vm_page_prot)));
207 if (pte_swp_soft_dirty(*pvmw.pte))
208 pte = pte_mksoft_dirty(pte);
Christoph Lameter06972122006-06-23 02:03:35 -0700209
Hugh Dickins486cf462011-10-19 12:50:35 -0700210 /*
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800211 * Recheck VMA as permissions can change since migration started
Hugh Dickins486cf462011-10-19 12:50:35 -0700212 */
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800213 entry = pte_to_swp_entry(*pvmw.pte);
Alistair Popple4dd845b2021-06-30 18:54:09 -0700214 if (is_writable_migration_entry(entry))
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800215 pte = maybe_mkwrite(pte, vma);
Peter Xuf45ec5f2020-04-06 20:06:01 -0700216 else if (pte_swp_uffd_wp(*pvmw.pte))
217 pte = pte_mkuffd_wp(pte);
Mel Gormand3cb8bf2014-10-02 19:47:41 +0100218
Ralph Campbell61287632020-09-04 16:36:04 -0700219 if (unlikely(is_device_private_page(new))) {
Alistair Popple4dd845b2021-06-30 18:54:09 -0700220 if (pte_write(pte))
221 entry = make_writable_device_private_entry(
222 page_to_pfn(new));
223 else
224 entry = make_readable_device_private_entry(
225 page_to_pfn(new));
Ralph Campbell61287632020-09-04 16:36:04 -0700226 pte = swp_entry_to_pte(entry);
Ralph Campbell3d321bf82020-09-04 16:36:07 -0700227 if (pte_swp_soft_dirty(*pvmw.pte))
228 pte = pte_swp_mksoft_dirty(pte);
Ralph Campbell61287632020-09-04 16:36:04 -0700229 if (pte_swp_uffd_wp(*pvmw.pte))
230 pte = pte_swp_mkuffd_wp(pte);
Lars Perssond2b2c6dd2019-03-28 20:44:28 -0700231 }
Jérôme Glissea5430dd2017-09-08 16:12:17 -0700232
Andi Kleen3ef8fd72010-10-11 16:03:21 +0200233#ifdef CONFIG_HUGETLB_PAGE
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800234 if (PageHuge(new)) {
Christophe Leroy79c1c592021-06-30 18:48:00 -0700235 unsigned int shift = huge_page_shift(hstate_vma(vma));
236
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800237 pte = pte_mkhuge(pte);
Christophe Leroy79c1c592021-06-30 18:48:00 -0700238 pte = arch_make_huge_pte(pte, shift, vma->vm_flags);
Aneesh Kumar K.V383321a2017-07-06 15:38:41 -0700239 set_huge_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800240 if (PageAnon(new))
241 hugepage_add_anon_rmap(new, vma, pvmw.address);
242 else
243 page_dup_rmap(new, true);
Aneesh Kumar K.V383321a2017-07-06 15:38:41 -0700244 } else
245#endif
246 {
247 set_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700248
Aneesh Kumar K.V383321a2017-07-06 15:38:41 -0700249 if (PageAnon(new))
250 page_add_anon_rmap(new, vma, pvmw.address, false);
251 else
252 page_add_file_rmap(new, false);
253 }
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800254 if (vma->vm_flags & VM_LOCKED && !PageTransCompound(new))
255 mlock_vma_page(new);
Hugh Dickins51afb122015-11-05 18:49:37 -0800256
Kirill A. Shutemove125fe42018-10-05 15:51:41 -0700257 if (PageTransHuge(page) && PageMlocked(page))
258 clear_page_mlock(page);
259
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800260 /* No need to invalidate - it was non-present before */
261 update_mmu_cache(vma, pvmw.address, pvmw.pte);
262 }
263
Minchan Kime4b82222017-05-03 14:54:27 -0700264 return true;
Christoph Lameter06972122006-06-23 02:03:35 -0700265}
266
267/*
Christoph Lameter04e62a22006-06-23 02:03:38 -0700268 * Get rid of all migration entries and replace them by
269 * references to the indicated page.
270 */
Kirill A. Shutemove3884662016-03-17 14:20:07 -0700271void remove_migration_ptes(struct page *old, struct page *new, bool locked)
Christoph Lameter04e62a22006-06-23 02:03:38 -0700272{
Joonsoo Kim051ac832014-01-21 15:49:48 -0800273 struct rmap_walk_control rwc = {
274 .rmap_one = remove_migration_pte,
275 .arg = old,
276 };
277
Kirill A. Shutemove3884662016-03-17 14:20:07 -0700278 if (locked)
279 rmap_walk_locked(new, &rwc);
280 else
281 rmap_walk(new, &rwc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700282}
283
284/*
Christoph Lameter06972122006-06-23 02:03:35 -0700285 * Something used the pte of a page under migration. We need to
286 * get to the page and wait until migration is finished.
287 * When we return from this function the fault will be retried.
Christoph Lameter06972122006-06-23 02:03:35 -0700288 */
Naoya Horiguchie66f17f2015-02-11 15:25:22 -0800289void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700290 spinlock_t *ptl)
Christoph Lameter06972122006-06-23 02:03:35 -0700291{
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700292 pte_t pte;
Christoph Lameter06972122006-06-23 02:03:35 -0700293 swp_entry_t entry;
294 struct page *page;
295
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700296 spin_lock(ptl);
Christoph Lameter06972122006-06-23 02:03:35 -0700297 pte = *ptep;
298 if (!is_swap_pte(pte))
299 goto out;
300
301 entry = pte_to_swp_entry(pte);
302 if (!is_migration_entry(entry))
303 goto out;
304
Alistair Poppleaf5cdaf2021-06-30 18:54:06 -0700305 page = pfn_swap_entry_to_page(entry);
Xu Yuffc90cb2021-06-15 18:23:42 -0700306 page = compound_head(page);
Christoph Lameter06972122006-06-23 02:03:35 -0700307
Nick Piggine2867812008-07-25 19:45:30 -0700308 /*
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500309 * Once page cache replacement of page migration started, page_count
Hugh Dickins9a1ea432018-12-28 00:36:14 -0800310 * is zero; but we must not call put_and_wait_on_page_locked() without
311 * a ref. Use get_page_unless_zero(), and just fault again if it fails.
Nick Piggine2867812008-07-25 19:45:30 -0700312 */
313 if (!get_page_unless_zero(page))
314 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700315 pte_unmap_unlock(ptep, ptl);
Matthew Wilcox (Oracle)48054622021-02-24 12:02:02 -0800316 put_and_wait_on_page_locked(page, TASK_UNINTERRUPTIBLE);
Christoph Lameter06972122006-06-23 02:03:35 -0700317 return;
318out:
319 pte_unmap_unlock(ptep, ptl);
320}
321
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700322void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
323 unsigned long address)
324{
325 spinlock_t *ptl = pte_lockptr(mm, pmd);
326 pte_t *ptep = pte_offset_map(pmd, address);
327 __migration_entry_wait(mm, ptep, ptl);
328}
329
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -0800330void migration_entry_wait_huge(struct vm_area_struct *vma,
331 struct mm_struct *mm, pte_t *pte)
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700332{
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -0800333 spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte);
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700334 __migration_entry_wait(mm, pte, ptl);
335}
336
Zi Yan616b8372017-09-08 16:10:57 -0700337#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
338void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd)
339{
340 spinlock_t *ptl;
341 struct page *page;
342
343 ptl = pmd_lock(mm, pmd);
344 if (!is_pmd_migration_entry(*pmd))
345 goto unlock;
Alistair Poppleaf5cdaf2021-06-30 18:54:06 -0700346 page = pfn_swap_entry_to_page(pmd_to_swp_entry(*pmd));
Zi Yan616b8372017-09-08 16:10:57 -0700347 if (!get_page_unless_zero(page))
348 goto unlock;
349 spin_unlock(ptl);
Matthew Wilcox (Oracle)48054622021-02-24 12:02:02 -0800350 put_and_wait_on_page_locked(page, TASK_UNINTERRUPTIBLE);
Zi Yan616b8372017-09-08 16:10:57 -0700351 return;
352unlock:
353 spin_unlock(ptl);
354}
355#endif
356
Jan Karaf9004822019-03-05 15:48:46 -0800357static int expected_page_refs(struct address_space *mapping, struct page *page)
Jan Kara0b3901b2018-12-28 00:39:01 -0800358{
359 int expected_count = 1;
360
361 /*
Ralph Campbellf1f4f3a2020-10-13 16:58:42 -0700362 * Device private pages have an extra refcount as they are
Jan Kara0b3901b2018-12-28 00:39:01 -0800363 * ZONE_DEVICE pages.
364 */
365 expected_count += is_device_private_page(page);
Jan Karaf9004822019-03-05 15:48:46 -0800366 if (mapping)
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -0700367 expected_count += thp_nr_pages(page) + page_has_private(page);
Jan Kara0b3901b2018-12-28 00:39:01 -0800368
369 return expected_count;
370}
371
Christoph Lameterb20a3502006-03-22 00:09:12 -0800372/*
Christoph Lameterc3fcf8a2006-06-23 02:03:32 -0700373 * Replace the page in the mapping.
Christoph Lameter5b5c7122006-06-23 02:03:29 -0700374 *
375 * The number of remaining references must be:
376 * 1 for anonymous pages without a mapping
377 * 2 for pages with a mapping
David Howells266cf652009-04-03 16:42:36 +0100378 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800379 */
Gu Zheng36bc08c2013-07-16 17:56:16 +0800380int migrate_page_move_mapping(struct address_space *mapping,
Keith Busch37109692019-07-18 15:58:46 -0700381 struct page *newpage, struct page *page, int extra_count)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800382{
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500383 XA_STATE(xas, &mapping->i_pages, page_index(page));
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800384 struct zone *oldzone, *newzone;
385 int dirty;
Jan Karaf9004822019-03-05 15:48:46 -0800386 int expected_count = expected_page_refs(mapping, page) + extra_count;
Shakeel Butt5c447d22021-01-23 21:01:15 -0800387 int nr = thp_nr_pages(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -0700388
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700389 if (!mapping) {
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700390 /* Anonymous page without mapping */
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500391 if (page_count(page) != expected_count)
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700392 return -EAGAIN;
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800393
394 /* No turning back from here */
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800395 newpage->index = page->index;
396 newpage->mapping = page->mapping;
397 if (PageSwapBacked(page))
Hugh Dickinsfa9949d2016-05-19 17:12:41 -0700398 __SetPageSwapBacked(newpage);
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800399
Rafael Aquini78bd5202012-12-11 16:02:31 -0800400 return MIGRATEPAGE_SUCCESS;
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700401 }
402
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800403 oldzone = page_zone(page);
404 newzone = page_zone(newpage);
405
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500406 xas_lock_irq(&xas);
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500407 if (page_count(page) != expected_count || xas_load(&xas) != page) {
408 xas_unlock_irq(&xas);
Christoph Lametere23ca002006-04-10 22:52:57 -0700409 return -EAGAIN;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800410 }
411
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700412 if (!page_ref_freeze(page, expected_count)) {
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500413 xas_unlock_irq(&xas);
Nick Piggine2867812008-07-25 19:45:30 -0700414 return -EAGAIN;
415 }
416
Christoph Lameterb20a3502006-03-22 00:09:12 -0800417 /*
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800418 * Now we know that no one else is looking at the page:
419 * no turning back from here.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800420 */
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800421 newpage->index = page->index;
422 newpage->mapping = page->mapping;
Shakeel Butt5c447d22021-01-23 21:01:15 -0800423 page_ref_add(newpage, nr); /* add cache reference */
Nicholas Piggin6326fec2016-12-25 13:00:29 +1000424 if (PageSwapBacked(page)) {
425 __SetPageSwapBacked(newpage);
426 if (PageSwapCache(page)) {
427 SetPageSwapCache(newpage);
428 set_page_private(newpage, page_private(page));
429 }
430 } else {
431 VM_BUG_ON_PAGE(PageSwapCache(page), page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800432 }
433
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800434 /* Move dirty while page refs frozen and newpage not yet exposed */
435 dirty = PageDirty(page);
436 if (dirty) {
437 ClearPageDirty(page);
438 SetPageDirty(newpage);
439 }
440
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500441 xas_store(&xas, newpage);
Naoya Horiguchie71769a2018-04-20 14:55:45 -0700442 if (PageTransHuge(page)) {
443 int i;
Naoya Horiguchie71769a2018-04-20 14:55:45 -0700444
Shakeel Butt5c447d22021-01-23 21:01:15 -0800445 for (i = 1; i < nr; i++) {
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500446 xas_next(&xas);
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700447 xas_store(&xas, newpage);
Naoya Horiguchie71769a2018-04-20 14:55:45 -0700448 }
Naoya Horiguchie71769a2018-04-20 14:55:45 -0700449 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800450
451 /*
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800452 * Drop cache reference from old page by unfreezing
453 * to one less reference.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800454 * We know this isn't the last reference.
455 */
Shakeel Butt5c447d22021-01-23 21:01:15 -0800456 page_ref_unfreeze(page, expected_count - nr);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800457
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500458 xas_unlock(&xas);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800459 /* Leave irq disabled to prevent preemption while updating stats */
460
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700461 /*
462 * If moved to a different zone then also account
463 * the page for that zone. Other VM counters will be
464 * taken care of when we establish references to the
465 * new page and drop references to the old page.
466 *
467 * Note that anonymous pages are accounted for
Mel Gorman4b9d0fa2016-07-28 15:46:17 -0700468 * via NR_FILE_PAGES and NR_ANON_MAPPED if they
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700469 * are mapped to swap space.
470 */
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800471 if (newzone != oldzone) {
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700472 struct lruvec *old_lruvec, *new_lruvec;
473 struct mem_cgroup *memcg;
474
475 memcg = page_memcg(page);
476 old_lruvec = mem_cgroup_lruvec(memcg, oldzone->zone_pgdat);
477 new_lruvec = mem_cgroup_lruvec(memcg, newzone->zone_pgdat);
478
Shakeel Butt5c447d22021-01-23 21:01:15 -0800479 __mod_lruvec_state(old_lruvec, NR_FILE_PAGES, -nr);
480 __mod_lruvec_state(new_lruvec, NR_FILE_PAGES, nr);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800481 if (PageSwapBacked(page) && !PageSwapCache(page)) {
Shakeel Butt5c447d22021-01-23 21:01:15 -0800482 __mod_lruvec_state(old_lruvec, NR_SHMEM, -nr);
483 __mod_lruvec_state(new_lruvec, NR_SHMEM, nr);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800484 }
Shakeel Buttb6038942021-02-24 12:03:55 -0800485#ifdef CONFIG_SWAP
486 if (PageSwapCache(page)) {
487 __mod_lruvec_state(old_lruvec, NR_SWAPCACHE, -nr);
488 __mod_lruvec_state(new_lruvec, NR_SWAPCACHE, nr);
489 }
490#endif
Christoph Hellwigf56753a2020-09-24 08:51:40 +0200491 if (dirty && mapping_can_writeback(mapping)) {
Shakeel Butt5c447d22021-01-23 21:01:15 -0800492 __mod_lruvec_state(old_lruvec, NR_FILE_DIRTY, -nr);
493 __mod_zone_page_state(oldzone, NR_ZONE_WRITE_PENDING, -nr);
494 __mod_lruvec_state(new_lruvec, NR_FILE_DIRTY, nr);
495 __mod_zone_page_state(newzone, NR_ZONE_WRITE_PENDING, nr);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800496 }
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700497 }
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800498 local_irq_enable();
Christoph Lameterb20a3502006-03-22 00:09:12 -0800499
Rafael Aquini78bd5202012-12-11 16:02:31 -0800500 return MIGRATEPAGE_SUCCESS;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800501}
Richard Weinberger1118dce2016-06-16 23:26:14 +0200502EXPORT_SYMBOL(migrate_page_move_mapping);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800503
504/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900505 * The expected number of remaining references is the same as that
506 * of migrate_page_move_mapping().
507 */
508int migrate_huge_page_move_mapping(struct address_space *mapping,
509 struct page *newpage, struct page *page)
510{
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500511 XA_STATE(xas, &mapping->i_pages, page_index(page));
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900512 int expected_count;
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900513
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500514 xas_lock_irq(&xas);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900515 expected_count = 2 + page_has_private(page);
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500516 if (page_count(page) != expected_count || xas_load(&xas) != page) {
517 xas_unlock_irq(&xas);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900518 return -EAGAIN;
519 }
520
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700521 if (!page_ref_freeze(page, expected_count)) {
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500522 xas_unlock_irq(&xas);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900523 return -EAGAIN;
524 }
525
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800526 newpage->index = page->index;
527 newpage->mapping = page->mapping;
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700528
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900529 get_page(newpage);
530
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500531 xas_store(&xas, newpage);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900532
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700533 page_ref_unfreeze(page, expected_count - 1);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900534
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500535 xas_unlock_irq(&xas);
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700536
Rafael Aquini78bd5202012-12-11 16:02:31 -0800537 return MIGRATEPAGE_SUCCESS;
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900538}
539
540/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800541 * Copy the page to its new location
542 */
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700543void migrate_page_states(struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800544{
Rik van Riel7851a452013-10-07 11:29:23 +0100545 int cpupid;
546
Christoph Lameterb20a3502006-03-22 00:09:12 -0800547 if (PageError(page))
548 SetPageError(newpage);
549 if (PageReferenced(page))
550 SetPageReferenced(newpage);
551 if (PageUptodate(page))
552 SetPageUptodate(newpage);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700553 if (TestClearPageActive(page)) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800554 VM_BUG_ON_PAGE(PageUnevictable(page), page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800555 SetPageActive(newpage);
Lee Schermerhorn418b27e2009-12-14 17:59:54 -0800556 } else if (TestClearPageUnevictable(page))
557 SetPageUnevictable(newpage);
Johannes Weiner1899ad12018-10-26 15:06:04 -0700558 if (PageWorkingset(page))
559 SetPageWorkingset(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800560 if (PageChecked(page))
561 SetPageChecked(newpage);
562 if (PageMappedToDisk(page))
563 SetPageMappedToDisk(newpage);
564
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800565 /* Move dirty on pages not done by migrate_page_move_mapping() */
566 if (PageDirty(page))
567 SetPageDirty(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800568
Vladimir Davydov33c3fc72015-09-09 15:35:45 -0700569 if (page_is_young(page))
570 set_page_young(newpage);
571 if (page_is_idle(page))
572 set_page_idle(newpage);
573
Rik van Riel7851a452013-10-07 11:29:23 +0100574 /*
575 * Copy NUMA information to the new page, to prevent over-eager
576 * future migrations of this same page.
577 */
578 cpupid = page_cpupid_xchg_last(page, -1);
579 page_cpupid_xchg_last(newpage, cpupid);
580
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800581 ksm_migrate_page(newpage, page);
Hugh Dickinsc8d65532013-02-22 16:35:10 -0800582 /*
583 * Please do not reorder this without considering how mm/ksm.c's
584 * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache().
585 */
Naoya Horiguchib3b3a992015-04-15 16:13:15 -0700586 if (PageSwapCache(page))
587 ClearPageSwapCache(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800588 ClearPagePrivate(page);
Muchun Songad2fa372021-06-30 18:47:21 -0700589
590 /* page->private contains hugetlb specific flags */
591 if (!PageHuge(page))
592 set_page_private(page, 0);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800593
594 /*
595 * If any waiters have accumulated on the new page then
596 * wake them up.
597 */
598 if (PageWriteback(newpage))
599 end_page_writeback(newpage);
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700600
Yang Shi6aeff242020-04-06 20:04:21 -0700601 /*
602 * PG_readahead shares the same bit with PG_reclaim. The above
603 * end_page_writeback() may clear PG_readahead mistakenly, so set the
604 * bit after that.
605 */
606 if (PageReadahead(page))
607 SetPageReadahead(newpage);
608
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700609 copy_page_owner(page, newpage);
Johannes Weiner74485cf2016-03-15 14:57:54 -0700610
Hugh Dickinsa333e3e2020-09-18 21:20:06 -0700611 if (!PageHuge(page))
612 mem_cgroup_migrate(page, newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800613}
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700614EXPORT_SYMBOL(migrate_page_states);
615
616void migrate_page_copy(struct page *newpage, struct page *page)
617{
618 if (PageHuge(page) || PageTransHuge(page))
619 copy_huge_page(newpage, page);
620 else
621 copy_highpage(newpage, page);
622
623 migrate_page_states(newpage, page);
624}
Richard Weinberger1118dce2016-06-16 23:26:14 +0200625EXPORT_SYMBOL(migrate_page_copy);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800626
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700627/************************************************************
628 * Migration functions
629 ***********************************************************/
630
Christoph Lameterb20a3502006-03-22 00:09:12 -0800631/*
Minchan Kimbda807d2016-07-26 15:23:05 -0700632 * Common logic to directly migrate a single LRU page suitable for
David Howells266cf652009-04-03 16:42:36 +0100633 * pages that do not use PagePrivate/PagePrivate2.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800634 *
635 * Pages are locked upon entry and exit.
636 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700637int migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800638 struct page *newpage, struct page *page,
639 enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800640{
641 int rc;
642
643 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
644
Keith Busch37109692019-07-18 15:58:46 -0700645 rc = migrate_page_move_mapping(mapping, newpage, page, 0);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800646
Rafael Aquini78bd5202012-12-11 16:02:31 -0800647 if (rc != MIGRATEPAGE_SUCCESS)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800648 return rc;
649
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700650 if (mode != MIGRATE_SYNC_NO_COPY)
651 migrate_page_copy(newpage, page);
652 else
653 migrate_page_states(newpage, page);
Rafael Aquini78bd5202012-12-11 16:02:31 -0800654 return MIGRATEPAGE_SUCCESS;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800655}
656EXPORT_SYMBOL(migrate_page);
657
David Howells93614012006-09-30 20:45:40 +0200658#ifdef CONFIG_BLOCK
Jan Kara84ade7c2018-12-28 00:39:09 -0800659/* Returns true if all buffers are successfully locked */
660static bool buffer_migrate_lock_buffers(struct buffer_head *head,
661 enum migrate_mode mode)
662{
663 struct buffer_head *bh = head;
664
665 /* Simple case, sync compaction */
666 if (mode != MIGRATE_ASYNC) {
667 do {
Jan Kara84ade7c2018-12-28 00:39:09 -0800668 lock_buffer(bh);
669 bh = bh->b_this_page;
670
671 } while (bh != head);
672
673 return true;
674 }
675
676 /* async case, we cannot block on lock_buffer so use trylock_buffer */
677 do {
Jan Kara84ade7c2018-12-28 00:39:09 -0800678 if (!trylock_buffer(bh)) {
679 /*
680 * We failed to lock the buffer and cannot stall in
681 * async migration. Release the taken locks
682 */
683 struct buffer_head *failed_bh = bh;
Jan Kara84ade7c2018-12-28 00:39:09 -0800684 bh = head;
685 while (bh != failed_bh) {
686 unlock_buffer(bh);
Jan Kara84ade7c2018-12-28 00:39:09 -0800687 bh = bh->b_this_page;
688 }
689 return false;
690 }
691
692 bh = bh->b_this_page;
693 } while (bh != head);
694 return true;
695}
696
Jan Kara89cb0882018-12-28 00:39:12 -0800697static int __buffer_migrate_page(struct address_space *mapping,
698 struct page *newpage, struct page *page, enum migrate_mode mode,
699 bool check_refs)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700700{
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700701 struct buffer_head *bh, *head;
702 int rc;
Jan Karacc4f11e2018-12-28 00:39:05 -0800703 int expected_count;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700704
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700705 if (!page_has_buffers(page))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800706 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700707
Jan Karacc4f11e2018-12-28 00:39:05 -0800708 /* Check whether page does not have extra refs before we do more work */
Jan Karaf9004822019-03-05 15:48:46 -0800709 expected_count = expected_page_refs(mapping, page);
Jan Karacc4f11e2018-12-28 00:39:05 -0800710 if (page_count(page) != expected_count)
711 return -EAGAIN;
712
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700713 head = page_buffers(page);
Jan Karacc4f11e2018-12-28 00:39:05 -0800714 if (!buffer_migrate_lock_buffers(head, mode))
715 return -EAGAIN;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700716
Jan Kara89cb0882018-12-28 00:39:12 -0800717 if (check_refs) {
718 bool busy;
719 bool invalidated = false;
720
721recheck_buffers:
722 busy = false;
723 spin_lock(&mapping->private_lock);
724 bh = head;
725 do {
726 if (atomic_read(&bh->b_count)) {
727 busy = true;
728 break;
729 }
730 bh = bh->b_this_page;
731 } while (bh != head);
Jan Kara89cb0882018-12-28 00:39:12 -0800732 if (busy) {
733 if (invalidated) {
734 rc = -EAGAIN;
735 goto unlock_buffers;
736 }
Jan Karaebdf4de2019-08-02 21:48:47 -0700737 spin_unlock(&mapping->private_lock);
Jan Kara89cb0882018-12-28 00:39:12 -0800738 invalidate_bh_lrus();
739 invalidated = true;
740 goto recheck_buffers;
741 }
742 }
743
Keith Busch37109692019-07-18 15:58:46 -0700744 rc = migrate_page_move_mapping(mapping, newpage, page, 0);
Rafael Aquini78bd5202012-12-11 16:02:31 -0800745 if (rc != MIGRATEPAGE_SUCCESS)
Jan Karacc4f11e2018-12-28 00:39:05 -0800746 goto unlock_buffers;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700747
Guoqing Jiangcd0f3712020-06-01 21:48:06 -0700748 attach_page_private(newpage, detach_page_private(page));
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700749
750 bh = head;
751 do {
752 set_bh_page(bh, newpage, bh_offset(bh));
753 bh = bh->b_this_page;
754
755 } while (bh != head);
756
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700757 if (mode != MIGRATE_SYNC_NO_COPY)
758 migrate_page_copy(newpage, page);
759 else
760 migrate_page_states(newpage, page);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700761
Jan Karacc4f11e2018-12-28 00:39:05 -0800762 rc = MIGRATEPAGE_SUCCESS;
763unlock_buffers:
Jan Karaebdf4de2019-08-02 21:48:47 -0700764 if (check_refs)
765 spin_unlock(&mapping->private_lock);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700766 bh = head;
767 do {
768 unlock_buffer(bh);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700769 bh = bh->b_this_page;
770
771 } while (bh != head);
772
Jan Karacc4f11e2018-12-28 00:39:05 -0800773 return rc;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700774}
Jan Kara89cb0882018-12-28 00:39:12 -0800775
776/*
777 * Migration function for pages with buffers. This function can only be used
778 * if the underlying filesystem guarantees that no other references to "page"
779 * exist. For example attached buffer heads are accessed only under page lock.
780 */
781int buffer_migrate_page(struct address_space *mapping,
782 struct page *newpage, struct page *page, enum migrate_mode mode)
783{
784 return __buffer_migrate_page(mapping, newpage, page, mode, false);
785}
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700786EXPORT_SYMBOL(buffer_migrate_page);
Jan Kara89cb0882018-12-28 00:39:12 -0800787
788/*
789 * Same as above except that this variant is more careful and checks that there
790 * are also no buffer head references. This function is the right one for
791 * mappings where buffer heads are directly looked up and referenced (such as
792 * block device mappings).
793 */
794int buffer_migrate_page_norefs(struct address_space *mapping,
795 struct page *newpage, struct page *page, enum migrate_mode mode)
796{
797 return __buffer_migrate_page(mapping, newpage, page, mode, true);
798}
David Howells93614012006-09-30 20:45:40 +0200799#endif
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700800
Christoph Lameter04e62a22006-06-23 02:03:38 -0700801/*
802 * Writeback a page to clean the dirty state
803 */
804static int writeout(struct address_space *mapping, struct page *page)
805{
806 struct writeback_control wbc = {
807 .sync_mode = WB_SYNC_NONE,
808 .nr_to_write = 1,
809 .range_start = 0,
810 .range_end = LLONG_MAX,
Christoph Lameter04e62a22006-06-23 02:03:38 -0700811 .for_reclaim = 1
812 };
813 int rc;
814
815 if (!mapping->a_ops->writepage)
816 /* No write method for the address space */
817 return -EINVAL;
818
819 if (!clear_page_dirty_for_io(page))
820 /* Someone else already triggered a write */
821 return -EAGAIN;
822
823 /*
824 * A dirty page may imply that the underlying filesystem has
825 * the page on some queue. So the page must be clean for
826 * migration. Writeout may mean we loose the lock and the
827 * page state is no longer what we checked for earlier.
828 * At this point we know that the migration attempt cannot
829 * be successful.
830 */
Kirill A. Shutemove3884662016-03-17 14:20:07 -0700831 remove_migration_ptes(page, page, false);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700832
833 rc = mapping->a_ops->writepage(page, &wbc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700834
835 if (rc != AOP_WRITEPAGE_ACTIVATE)
836 /* unlocked. Relock */
837 lock_page(page);
838
Hugh Dickinsbda85502008-11-19 15:36:36 -0800839 return (rc < 0) ? -EIO : -EAGAIN;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700840}
841
842/*
843 * Default handling if a filesystem does not provide a migration function.
844 */
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700845static int fallback_migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800846 struct page *newpage, struct page *page, enum migrate_mode mode)
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700847{
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800848 if (PageDirty(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800849 /* Only writeback pages in full synchronous migration */
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700850 switch (mode) {
851 case MIGRATE_SYNC:
852 case MIGRATE_SYNC_NO_COPY:
853 break;
854 default:
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800855 return -EBUSY;
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700856 }
Christoph Lameter04e62a22006-06-23 02:03:38 -0700857 return writeout(mapping, page);
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800858 }
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700859
860 /*
861 * Buffers may be managed in a filesystem specific way.
862 * We must have no buffers or drop them.
863 */
David Howells266cf652009-04-03 16:42:36 +0100864 if (page_has_private(page) &&
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700865 !try_to_release_page(page, GFP_KERNEL))
Mel Gorman806031b2019-03-05 15:44:43 -0800866 return mode == MIGRATE_SYNC ? -EAGAIN : -EBUSY;
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700867
Mel Gormana6bc32b2012-01-12 17:19:43 -0800868 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700869}
870
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700871/*
Christoph Lametere24f0b82006-06-23 02:03:51 -0700872 * Move a page to a newly allocated page
873 * The page is locked and all ptes have been successfully removed.
874 *
875 * The new page will have replaced the old page if this function
876 * is successful.
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700877 *
878 * Return value:
879 * < 0 - error code
Rafael Aquini78bd5202012-12-11 16:02:31 -0800880 * MIGRATEPAGE_SUCCESS - success
Christoph Lametere24f0b82006-06-23 02:03:51 -0700881 */
Mel Gorman3fe20112010-05-24 14:32:20 -0700882static int move_to_new_page(struct page *newpage, struct page *page,
Hugh Dickins5c3f9a62015-11-05 18:49:53 -0800883 enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700884{
885 struct address_space *mapping;
Minchan Kimbda807d2016-07-26 15:23:05 -0700886 int rc = -EAGAIN;
887 bool is_lru = !__PageMovable(page);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700888
Hugh Dickins7db76712015-11-05 18:49:49 -0800889 VM_BUG_ON_PAGE(!PageLocked(page), page);
890 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700891
Christoph Lametere24f0b82006-06-23 02:03:51 -0700892 mapping = page_mapping(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700893
894 if (likely(is_lru)) {
895 if (!mapping)
896 rc = migrate_page(mapping, newpage, page, mode);
897 else if (mapping->a_ops->migratepage)
898 /*
899 * Most pages have a mapping and most filesystems
900 * provide a migratepage callback. Anonymous pages
901 * are part of swap space which also has its own
902 * migratepage callback. This is the most common path
903 * for page migration.
904 */
905 rc = mapping->a_ops->migratepage(mapping, newpage,
906 page, mode);
907 else
908 rc = fallback_migrate_page(mapping, newpage,
909 page, mode);
910 } else {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700911 /*
Minchan Kimbda807d2016-07-26 15:23:05 -0700912 * In case of non-lru page, it could be released after
913 * isolation step. In that case, we shouldn't try migration.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700914 */
Minchan Kimbda807d2016-07-26 15:23:05 -0700915 VM_BUG_ON_PAGE(!PageIsolated(page), page);
916 if (!PageMovable(page)) {
917 rc = MIGRATEPAGE_SUCCESS;
918 __ClearPageIsolated(page);
919 goto out;
920 }
921
922 rc = mapping->a_ops->migratepage(mapping, newpage,
923 page, mode);
924 WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS &&
925 !PageIsolated(page));
926 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700927
Hugh Dickins5c3f9a62015-11-05 18:49:53 -0800928 /*
929 * When successful, old pagecache page->mapping must be cleared before
930 * page is freed; but stats require that PageAnon be left as PageAnon.
931 */
932 if (rc == MIGRATEPAGE_SUCCESS) {
Minchan Kimbda807d2016-07-26 15:23:05 -0700933 if (__PageMovable(page)) {
934 VM_BUG_ON_PAGE(!PageIsolated(page), page);
935
936 /*
937 * We clear PG_movable under page_lock so any compactor
938 * cannot try to migrate this page.
939 */
940 __ClearPageIsolated(page);
941 }
942
943 /*
Ralph Campbellc23a0c92020-01-30 22:14:41 -0800944 * Anonymous and movable page->mapping will be cleared by
Minchan Kimbda807d2016-07-26 15:23:05 -0700945 * free_pages_prepare so don't reset it here for keeping
946 * the type to work PageAnon, for example.
947 */
948 if (!PageMappingFlags(page))
Hugh Dickins5c3f9a62015-11-05 18:49:53 -0800949 page->mapping = NULL;
Lars Perssond2b2c6dd2019-03-28 20:44:28 -0700950
Muchun Song97a9f802022-03-22 14:41:56 -0700951 if (likely(!is_zone_device_page(newpage))) {
952 int i, nr = compound_nr(newpage);
Lars Perssond2b2c6dd2019-03-28 20:44:28 -0700953
Muchun Song97a9f802022-03-22 14:41:56 -0700954 for (i = 0; i < nr; i++)
955 flush_dcache_page(newpage + i);
956 }
Mel Gorman3fe20112010-05-24 14:32:20 -0700957 }
Minchan Kimbda807d2016-07-26 15:23:05 -0700958out:
Christoph Lametere24f0b82006-06-23 02:03:51 -0700959 return rc;
960}
961
Minchan Kim0dabec92011-10-31 17:06:57 -0700962static int __unmap_and_move(struct page *page, struct page *newpage,
Hugh Dickins9c620e22013-02-22 16:35:14 -0800963 int force, enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700964{
Minchan Kim0dabec92011-10-31 17:06:57 -0700965 int rc = -EAGAIN;
Baolin Wang213ecb32021-09-08 15:18:06 -0700966 bool page_was_mapped = false;
Mel Gorman3f6c8272010-05-24 14:32:17 -0700967 struct anon_vma *anon_vma = NULL;
Minchan Kimbda807d2016-07-26 15:23:05 -0700968 bool is_lru = !__PageMovable(page);
Christoph Lameter95a402c2006-06-23 02:03:53 -0700969
Nick Piggin529ae9a2008-08-02 12:01:03 +0200970 if (!trylock_page(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800971 if (!force || mode == MIGRATE_ASYNC)
Minchan Kim0dabec92011-10-31 17:06:57 -0700972 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800973
974 /*
975 * It's not safe for direct compaction to call lock_page.
976 * For example, during page readahead pages are added locked
977 * to the LRU. Later, when the IO completes the pages are
978 * marked uptodate and unlocked. However, the queueing
979 * could be merging multiple pages for one bio (e.g.
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -0700980 * mpage_readahead). If an allocation happens for the
Mel Gorman3e7d3442011-01-13 15:45:56 -0800981 * second or third page, the process can end up locking
982 * the same page twice and deadlocking. Rather than
983 * trying to be clever about what pages can be locked,
984 * avoid the use of lock_page for direct compaction
985 * altogether.
986 */
987 if (current->flags & PF_MEMALLOC)
Minchan Kim0dabec92011-10-31 17:06:57 -0700988 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800989
Christoph Lametere24f0b82006-06-23 02:03:51 -0700990 lock_page(page);
991 }
992
993 if (PageWriteback(page)) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700994 /*
Jianguo Wufed5b642013-04-29 15:07:58 -0700995 * Only in the case of a full synchronous migration is it
Mel Gormana6bc32b2012-01-12 17:19:43 -0800996 * necessary to wait for PageWriteback. In the async case,
997 * the retry loop is too short and in the sync-light case,
998 * the overhead of stalling is too much
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700999 */
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07001000 switch (mode) {
1001 case MIGRATE_SYNC:
1002 case MIGRATE_SYNC_NO_COPY:
1003 break;
1004 default:
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -07001005 rc = -EBUSY;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07001006 goto out_unlock;
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -07001007 }
1008 if (!force)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07001009 goto out_unlock;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001010 wait_on_page_writeback(page);
1011 }
Hugh Dickins03f15c82015-11-05 18:49:56 -08001012
Christoph Lametere24f0b82006-06-23 02:03:51 -07001013 /*
Baolin Wang68a98432021-09-08 15:18:03 -07001014 * By try_to_migrate(), page->mapcount goes down to 0 here. In this case,
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001015 * we cannot notice that anon_vma is freed while we migrates a page.
Hugh Dickins1ce82b62011-01-13 15:47:30 -08001016 * This get_anon_vma() delays freeing anon_vma pointer until the end
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001017 * of migration. File cache pages are no problem because of page_lock()
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -07001018 * File Caches may use write_page() or lock_page() in migration, then,
1019 * just care Anon page here.
Hugh Dickins03f15c82015-11-05 18:49:56 -08001020 *
1021 * Only page_get_anon_vma() understands the subtleties of
1022 * getting a hold on an anon_vma from outside one of its mms.
1023 * But if we cannot get anon_vma, then we won't need it anyway,
1024 * because that implies that the anon page is no longer mapped
1025 * (and cannot be remapped so long as we hold the page lock).
Christoph Lametere24f0b82006-06-23 02:03:51 -07001026 */
Hugh Dickins03f15c82015-11-05 18:49:56 -08001027 if (PageAnon(page) && !PageKsm(page))
Peter Zijlstra746b18d2011-05-24 17:12:10 -07001028 anon_vma = page_get_anon_vma(page);
Shaohua Li62e1c552008-02-04 22:29:33 -08001029
Hugh Dickins7db76712015-11-05 18:49:49 -08001030 /*
1031 * Block others from accessing the new page when we get around to
1032 * establishing additional references. We are usually the only one
1033 * holding a reference to newpage at this point. We used to have a BUG
1034 * here if trylock_page(newpage) fails, but would like to allow for
1035 * cases where there might be a race with the previous use of newpage.
1036 * This is much like races on refcount of oldpage: just don't BUG().
1037 */
1038 if (unlikely(!trylock_page(newpage)))
1039 goto out_unlock;
1040
Minchan Kimbda807d2016-07-26 15:23:05 -07001041 if (unlikely(!is_lru)) {
1042 rc = move_to_new_page(newpage, page, mode);
1043 goto out_unlock_both;
1044 }
1045
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001046 /*
Shaohua Li62e1c552008-02-04 22:29:33 -08001047 * Corner case handling:
1048 * 1. When a new swap-cache page is read into, it is added to the LRU
1049 * and treated as swapcache but it has no rmap yet.
1050 * Calling try_to_unmap() against a page->mapping==NULL page will
1051 * trigger a BUG. So handle it here.
Yang Shid12b8952020-12-14 19:13:02 -08001052 * 2. An orphaned page (see truncate_cleanup_page) might have
Shaohua Li62e1c552008-02-04 22:29:33 -08001053 * fs-private metadata. The page can be picked up due to memory
1054 * offlining. Everywhere else except page reclaim, the page is
1055 * invisible to the vm, so the page can not be migrated. So try to
1056 * free the metadata, so the page can be freed.
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001057 */
Shaohua Li62e1c552008-02-04 22:29:33 -08001058 if (!page->mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -08001059 VM_BUG_ON_PAGE(PageAnon(page), page);
Hugh Dickins1ce82b62011-01-13 15:47:30 -08001060 if (page_has_private(page)) {
Shaohua Li62e1c552008-02-04 22:29:33 -08001061 try_to_free_buffers(page);
Hugh Dickins7db76712015-11-05 18:49:49 -08001062 goto out_unlock_both;
Shaohua Li62e1c552008-02-04 22:29:33 -08001063 }
Hugh Dickins7db76712015-11-05 18:49:49 -08001064 } else if (page_mapped(page)) {
1065 /* Establish migration ptes */
Hugh Dickins03f15c82015-11-05 18:49:56 -08001066 VM_BUG_ON_PAGE(PageAnon(page) && !PageKsm(page) && !anon_vma,
1067 page);
Alistair Popplea98a2f02021-06-30 18:54:16 -07001068 try_to_migrate(page, 0);
Baolin Wang213ecb32021-09-08 15:18:06 -07001069 page_was_mapped = true;
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001070 }
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001071
Christoph Lametere6a15302006-06-25 05:46:49 -07001072 if (!page_mapped(page))
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001073 rc = move_to_new_page(newpage, page, mode);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001074
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001075 if (page_was_mapped)
1076 remove_migration_ptes(page,
Kirill A. Shutemove3884662016-03-17 14:20:07 -07001077 rc == MIGRATEPAGE_SUCCESS ? newpage : page, false);
Mel Gorman3f6c8272010-05-24 14:32:17 -07001078
Hugh Dickins7db76712015-11-05 18:49:49 -08001079out_unlock_both:
1080 unlock_page(newpage);
1081out_unlock:
Mel Gorman3f6c8272010-05-24 14:32:17 -07001082 /* Drop an anon_vma reference if we took one */
Rik van Riel76545062010-08-09 17:18:41 -07001083 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -07001084 put_anon_vma(anon_vma);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001085 unlock_page(page);
Minchan Kim0dabec92011-10-31 17:06:57 -07001086out:
Minchan Kimc6c919e2016-07-26 15:23:02 -07001087 /*
1088 * If migration is successful, decrease refcount of the newpage
1089 * which will not free the page because new page owner increased
1090 * refcounter. As well, if it is LRU page, add the page to LRU
David Hildenbrande0a352f2019-02-01 14:21:19 -08001091 * list in here. Use the old state of the isolated source page to
1092 * determine if we migrated a LRU page. newpage was already unlocked
1093 * and possibly modified by its owner - don't rely on the page
1094 * state.
Minchan Kimc6c919e2016-07-26 15:23:02 -07001095 */
1096 if (rc == MIGRATEPAGE_SUCCESS) {
David Hildenbrande0a352f2019-02-01 14:21:19 -08001097 if (unlikely(!is_lru))
Minchan Kimc6c919e2016-07-26 15:23:02 -07001098 put_page(newpage);
1099 else
1100 putback_lru_page(newpage);
1101 }
1102
Minchan Kim0dabec92011-10-31 17:06:57 -07001103 return rc;
1104}
Christoph Lameter95a402c2006-06-23 02:03:53 -07001105
Dave Hansen79c28a42021-09-02 14:59:06 -07001106
1107/*
1108 * node_demotion[] example:
1109 *
1110 * Consider a system with two sockets. Each socket has
1111 * three classes of memory attached: fast, medium and slow.
1112 * Each memory class is placed in its own NUMA node. The
1113 * CPUs are placed in the node with the "fast" memory. The
1114 * 6 NUMA nodes (0-5) might be split among the sockets like
1115 * this:
1116 *
1117 * Socket A: 0, 1, 2
1118 * Socket B: 3, 4, 5
1119 *
1120 * When Node 0 fills up, its memory should be migrated to
1121 * Node 1. When Node 1 fills up, it should be migrated to
1122 * Node 2. The migration path start on the nodes with the
1123 * processors (since allocations default to this node) and
1124 * fast memory, progress through medium and end with the
1125 * slow memory:
1126 *
1127 * 0 -> 1 -> 2 -> stop
1128 * 3 -> 4 -> 5 -> stop
1129 *
1130 * This is represented in the node_demotion[] like this:
1131 *
1132 * { 1, // Node 0 migrates to 1
1133 * 2, // Node 1 migrates to 2
1134 * -1, // Node 2 does not migrate
1135 * 4, // Node 3 migrates to 4
1136 * 5, // Node 4 migrates to 5
1137 * -1} // Node 5 does not migrate
1138 */
1139
1140/*
1141 * Writes to this array occur without locking. Cycles are
1142 * not allowed: Node X demotes to Y which demotes to X...
1143 *
1144 * If multiple reads are performed, a single rcu_read_lock()
1145 * must be held over all reads to ensure that no cycles are
1146 * observed.
1147 */
1148static int node_demotion[MAX_NUMNODES] __read_mostly =
1149 {[0 ... MAX_NUMNODES - 1] = NUMA_NO_NODE};
1150
1151/**
1152 * next_demotion_node() - Get the next node in the demotion path
1153 * @node: The starting node to lookup the next node
1154 *
Randy Dunlapc9bd7d12021-09-02 15:00:36 -07001155 * Return: node id for next memory node in the demotion path hierarchy
Dave Hansen79c28a42021-09-02 14:59:06 -07001156 * from @node; NUMA_NO_NODE if @node is terminal. This does not keep
1157 * @node online or guarantee that it *continues* to be the next demotion
1158 * target.
1159 */
1160int next_demotion_node(int node)
1161{
1162 int target;
1163
1164 /*
1165 * node_demotion[] is updated without excluding this
1166 * function from running. RCU doesn't provide any
1167 * compiler barriers, so the READ_ONCE() is required
1168 * to avoid compiler reordering or read merging.
1169 *
1170 * Make sure to use RCU over entire code blocks if
1171 * node_demotion[] reads need to be consistent.
1172 */
1173 rcu_read_lock();
1174 target = READ_ONCE(node_demotion[node]);
1175 rcu_read_unlock();
1176
1177 return target;
1178}
1179
Minchan Kim0dabec92011-10-31 17:06:57 -07001180/*
1181 * Obtain the lock on page, remove all ptes and migrate the page
1182 * to the newly allocated page in newpage.
1183 */
Linus Torvalds6ec44762020-07-08 10:48:35 -07001184static int unmap_and_move(new_page_t get_new_page,
Geert Uytterhoevenef2a5152015-04-14 15:44:22 -07001185 free_page_t put_new_page,
1186 unsigned long private, struct page *page,
Naoya Horiguchiadd05ce2015-06-24 16:56:50 -07001187 int force, enum migrate_mode mode,
Yang Shidd4ae782020-12-14 19:13:06 -08001188 enum migrate_reason reason,
1189 struct list_head *ret)
Minchan Kim0dabec92011-10-31 17:06:57 -07001190{
Hugh Dickins2def7422015-11-05 18:49:46 -08001191 int rc = MIGRATEPAGE_SUCCESS;
Yang Shi74d4a572019-11-30 17:57:12 -08001192 struct page *newpage = NULL;
Minchan Kim0dabec92011-10-31 17:06:57 -07001193
Michal Hocko94723aa2018-04-10 16:30:07 -07001194 if (!thp_migration_supported() && PageTransHuge(page))
Yang Shid532e2e2020-12-14 19:13:16 -08001195 return -ENOSYS;
Michal Hocko94723aa2018-04-10 16:30:07 -07001196
Minchan Kim0dabec92011-10-31 17:06:57 -07001197 if (page_count(page) == 1) {
1198 /* page was freed from under us. So we are done. */
Minchan Kimc6c919e2016-07-26 15:23:02 -07001199 ClearPageActive(page);
1200 ClearPageUnevictable(page);
Minchan Kimbda807d2016-07-26 15:23:05 -07001201 if (unlikely(__PageMovable(page))) {
1202 lock_page(page);
1203 if (!PageMovable(page))
1204 __ClearPageIsolated(page);
1205 unlock_page(page);
1206 }
Minchan Kim0dabec92011-10-31 17:06:57 -07001207 goto out;
1208 }
1209
Yang Shi74d4a572019-11-30 17:57:12 -08001210 newpage = get_new_page(page, private);
1211 if (!newpage)
1212 return -ENOMEM;
1213
Hugh Dickins9c620e22013-02-22 16:35:14 -08001214 rc = __unmap_and_move(page, newpage, force, mode);
Minchan Kimc6c919e2016-07-26 15:23:02 -07001215 if (rc == MIGRATEPAGE_SUCCESS)
Vlastimil Babka7cd12b42016-03-15 14:56:18 -07001216 set_page_owner_migrate_reason(newpage, reason);
Rafael Aquinibf6bddf12012-12-11 16:02:42 -08001217
Minchan Kim0dabec92011-10-31 17:06:57 -07001218out:
Christoph Lametere24f0b82006-06-23 02:03:51 -07001219 if (rc != -EAGAIN) {
Minchan Kim0dabec92011-10-31 17:06:57 -07001220 /*
1221 * A page that has been migrated has all references
1222 * removed and will be freed. A page that has not been
Ralph Campbellc23a0c92020-01-30 22:14:41 -08001223 * migrated will have kept its references and be restored.
Minchan Kim0dabec92011-10-31 17:06:57 -07001224 */
1225 list_del(&page->lru);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001226 }
David Rientjes68711a72014-06-04 16:08:25 -07001227
Christoph Lameter95a402c2006-06-23 02:03:53 -07001228 /*
Minchan Kimc6c919e2016-07-26 15:23:02 -07001229 * If migration is successful, releases reference grabbed during
1230 * isolation. Otherwise, restore the page to right list unless
1231 * we want to retry.
Christoph Lameter95a402c2006-06-23 02:03:53 -07001232 */
Minchan Kimc6c919e2016-07-26 15:23:02 -07001233 if (rc == MIGRATEPAGE_SUCCESS) {
Yang Shidd4ae782020-12-14 19:13:06 -08001234 /*
1235 * Compaction can migrate also non-LRU pages which are
1236 * not accounted to NR_ISOLATED_*. They can be recognized
1237 * as __PageMovable
1238 */
1239 if (likely(!__PageMovable(page)))
1240 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
1241 page_is_file_lru(page), -thp_nr_pages(page));
1242
Oscar Salvador79f5f8f2020-10-15 20:07:09 -07001243 if (reason != MR_MEMORY_FAILURE)
Minchan Kimc6c919e2016-07-26 15:23:02 -07001244 /*
Oscar Salvador79f5f8f2020-10-15 20:07:09 -07001245 * We release the page in page_handle_poison.
Minchan Kimc6c919e2016-07-26 15:23:02 -07001246 */
Oscar Salvador79f5f8f2020-10-15 20:07:09 -07001247 put_page(page);
Minchan Kimc6c919e2016-07-26 15:23:02 -07001248 } else {
Yang Shidd4ae782020-12-14 19:13:06 -08001249 if (rc != -EAGAIN)
1250 list_add_tail(&page->lru, ret);
Minchan Kimbda807d2016-07-26 15:23:05 -07001251
Minchan Kimc6c919e2016-07-26 15:23:02 -07001252 if (put_new_page)
1253 put_new_page(newpage, private);
1254 else
1255 put_page(newpage);
1256 }
David Rientjes68711a72014-06-04 16:08:25 -07001257
Christoph Lametere24f0b82006-06-23 02:03:51 -07001258 return rc;
1259}
1260
1261/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001262 * Counterpart of unmap_and_move_page() for hugepage migration.
1263 *
1264 * This function doesn't wait the completion of hugepage I/O
1265 * because there is no race between I/O and migration for hugepage.
1266 * Note that currently hugepage I/O occurs only in direct I/O
1267 * where no lock is held and PG_writeback is irrelevant,
1268 * and writeback status of all subpages are counted in the reference
1269 * count of the head page (i.e. if all subpages of a 2MB hugepage are
1270 * under direct I/O, the reference of the head page is 512 and a bit more.)
1271 * This means that when we try to migrate hugepage whose subpages are
1272 * doing direct I/O, some references remain after try_to_unmap() and
1273 * hugepage migration fails without data corruption.
1274 *
1275 * There is also no race when direct I/O is issued on the page under migration,
1276 * because then pte is replaced with migration swap entry and direct I/O code
1277 * will wait in the page fault for migration to complete.
1278 */
1279static int unmap_and_move_huge_page(new_page_t get_new_page,
David Rientjes68711a72014-06-04 16:08:25 -07001280 free_page_t put_new_page, unsigned long private,
1281 struct page *hpage, int force,
Yang Shidd4ae782020-12-14 19:13:06 -08001282 enum migrate_mode mode, int reason,
1283 struct list_head *ret)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001284{
Hugh Dickins2def7422015-11-05 18:49:46 -08001285 int rc = -EAGAIN;
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001286 int page_was_mapped = 0;
Joonsoo Kim32665f22014-01-21 15:51:15 -08001287 struct page *new_hpage;
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001288 struct anon_vma *anon_vma = NULL;
Mike Kravetzc0d03812020-04-01 21:11:05 -07001289 struct address_space *mapping = NULL;
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001290
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001291 /*
Anshuman Khandual7ed2c312019-03-05 15:43:44 -08001292 * Migratability of hugepages depends on architectures and their size.
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001293 * This check is necessary because some callers of hugepage migration
1294 * like soft offline and memory hotremove don't walk through page
1295 * tables or check whether the hugepage is pmd-based or not before
1296 * kicking migration.
1297 */
Naoya Horiguchi100873d2014-06-04 16:10:56 -07001298 if (!hugepage_migration_supported(page_hstate(hpage))) {
Yang Shidd4ae782020-12-14 19:13:06 -08001299 list_move_tail(&hpage->lru, ret);
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001300 return -ENOSYS;
Joonsoo Kim32665f22014-01-21 15:51:15 -08001301 }
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001302
Muchun Song71a64f62021-02-04 18:32:17 -08001303 if (page_count(hpage) == 1) {
1304 /* page was freed from under us. So we are done. */
1305 putback_active_hugepage(hpage);
1306 return MIGRATEPAGE_SUCCESS;
1307 }
1308
Michal Hocko666feb22018-04-10 16:30:03 -07001309 new_hpage = get_new_page(hpage, private);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001310 if (!new_hpage)
1311 return -ENOMEM;
1312
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001313 if (!trylock_page(hpage)) {
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07001314 if (!force)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001315 goto out;
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07001316 switch (mode) {
1317 case MIGRATE_SYNC:
1318 case MIGRATE_SYNC_NO_COPY:
1319 break;
1320 default:
1321 goto out;
1322 }
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001323 lock_page(hpage);
1324 }
1325
Mike Kravetzcb6acd02019-02-28 16:22:02 -08001326 /*
1327 * Check for pages which are in the process of being freed. Without
1328 * page_mapping() set, hugetlbfs specific move page routine will not
1329 * be called and we could leak usage counts for subpools.
1330 */
Muchun Song6acfb5b2021-06-30 18:51:29 -07001331 if (hugetlb_page_subpool(hpage) && !page_mapping(hpage)) {
Mike Kravetzcb6acd02019-02-28 16:22:02 -08001332 rc = -EBUSY;
1333 goto out_unlock;
1334 }
1335
Peter Zijlstra746b18d2011-05-24 17:12:10 -07001336 if (PageAnon(hpage))
1337 anon_vma = page_get_anon_vma(hpage);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001338
Hugh Dickins7db76712015-11-05 18:49:49 -08001339 if (unlikely(!trylock_page(new_hpage)))
1340 goto put_anon;
1341
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001342 if (page_mapped(hpage)) {
Mike Kravetz336bf302020-11-13 22:52:16 -08001343 bool mapping_locked = false;
Alistair Popplea98a2f02021-06-30 18:54:16 -07001344 enum ttu_flags ttu = 0;
Mike Kravetzc0d03812020-04-01 21:11:05 -07001345
Mike Kravetz336bf302020-11-13 22:52:16 -08001346 if (!PageAnon(hpage)) {
1347 /*
1348 * In shared mappings, try_to_unmap could potentially
1349 * call huge_pmd_unshare. Because of this, take
1350 * semaphore in write mode here and set TTU_RMAP_LOCKED
1351 * to let lower levels know we have taken the lock.
1352 */
1353 mapping = hugetlb_page_mapping_lock_write(hpage);
1354 if (unlikely(!mapping))
1355 goto unlock_put_anon;
1356
1357 mapping_locked = true;
1358 ttu |= TTU_RMAP_LOCKED;
1359 }
1360
Alistair Popplea98a2f02021-06-30 18:54:16 -07001361 try_to_migrate(hpage, ttu);
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001362 page_was_mapped = 1;
Mike Kravetz336bf302020-11-13 22:52:16 -08001363
1364 if (mapping_locked)
1365 i_mmap_unlock_write(mapping);
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001366 }
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001367
1368 if (!page_mapped(hpage))
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001369 rc = move_to_new_page(new_hpage, hpage, mode);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001370
Mike Kravetz336bf302020-11-13 22:52:16 -08001371 if (page_was_mapped)
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001372 remove_migration_ptes(hpage,
Mike Kravetz336bf302020-11-13 22:52:16 -08001373 rc == MIGRATEPAGE_SUCCESS ? new_hpage : hpage, false);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001374
Mike Kravetzc0d03812020-04-01 21:11:05 -07001375unlock_put_anon:
Hugh Dickins7db76712015-11-05 18:49:49 -08001376 unlock_page(new_hpage);
1377
1378put_anon:
Hugh Dickinsfd4a4662011-01-13 15:47:31 -08001379 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -07001380 put_anon_vma(anon_vma);
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -07001381
Hugh Dickins2def7422015-11-05 18:49:46 -08001382 if (rc == MIGRATEPAGE_SUCCESS) {
Michal Hockoab5ac902018-01-31 16:20:48 -08001383 move_hugetlb_state(hpage, new_hpage, reason);
Hugh Dickins2def7422015-11-05 18:49:46 -08001384 put_new_page = NULL;
1385 }
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -07001386
Mike Kravetzcb6acd02019-02-28 16:22:02 -08001387out_unlock:
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001388 unlock_page(hpage);
Hillf Danton09761332011-12-08 14:34:20 -08001389out:
Yang Shidd4ae782020-12-14 19:13:06 -08001390 if (rc == MIGRATEPAGE_SUCCESS)
Naoya Horiguchib8ec1ce2013-09-11 14:22:01 -07001391 putback_active_hugepage(hpage);
Miaohe Lina04840c2021-05-04 18:37:07 -07001392 else if (rc != -EAGAIN)
Yang Shidd4ae782020-12-14 19:13:06 -08001393 list_move_tail(&hpage->lru, ret);
David Rientjes68711a72014-06-04 16:08:25 -07001394
1395 /*
1396 * If migration was not successful and there's a freeing callback, use
1397 * it. Otherwise, put_page() will drop the reference grabbed during
1398 * isolation.
1399 */
Hugh Dickins2def7422015-11-05 18:49:46 -08001400 if (put_new_page)
David Rientjes68711a72014-06-04 16:08:25 -07001401 put_new_page(new_hpage, private);
1402 else
Naoya Horiguchi3aaa76e2015-09-22 14:59:14 -07001403 putback_active_hugepage(new_hpage);
David Rientjes68711a72014-06-04 16:08:25 -07001404
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001405 return rc;
1406}
1407
Yang Shid532e2e2020-12-14 19:13:16 -08001408static inline int try_split_thp(struct page *page, struct page **page2,
1409 struct list_head *from)
1410{
1411 int rc = 0;
1412
1413 lock_page(page);
1414 rc = split_huge_page_to_list(page, from);
1415 unlock_page(page);
1416 if (!rc)
1417 list_safe_reset_next(page, *page2, lru);
1418
1419 return rc;
1420}
1421
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001422/*
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001423 * migrate_pages - migrate the pages specified in a list, to the free pages
1424 * supplied as the target for the page migration
Christoph Lameterb20a3502006-03-22 00:09:12 -08001425 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001426 * @from: The list of pages to be migrated.
1427 * @get_new_page: The function used to allocate free pages to be used
1428 * as the target of the page migration.
David Rientjes68711a72014-06-04 16:08:25 -07001429 * @put_new_page: The function used to free target pages if migration
1430 * fails, or NULL if no special handling is necessary.
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001431 * @private: Private data to be passed on to get_new_page()
1432 * @mode: The migration mode that specifies the constraints for
1433 * page migration, if any.
1434 * @reason: The reason for page migration.
Yang Shi5ac95882021-09-02 14:59:13 -07001435 * @ret_succeeded: Set to the number of pages migrated successfully if
1436 * the caller passes a non-NULL pointer.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001437 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001438 * The function returns after 10 attempts or if no pages are movable any more
1439 * because the list has become empty or no retryable pages exist any more.
Yang Shidd4ae782020-12-14 19:13:06 -08001440 * It is caller's responsibility to call putback_movable_pages() to return pages
1441 * to the LRU or free list only if ret != 0.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001442 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001443 * Returns the number of pages that were not migrated, or an error code.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001444 */
Hugh Dickins9c620e22013-02-22 16:35:14 -08001445int migrate_pages(struct list_head *from, new_page_t get_new_page,
David Rientjes68711a72014-06-04 16:08:25 -07001446 free_page_t put_new_page, unsigned long private,
Yang Shi5ac95882021-09-02 14:59:13 -07001447 enum migrate_mode mode, int reason, unsigned int *ret_succeeded)
Christoph Lameterb20a3502006-03-22 00:09:12 -08001448{
Christoph Lametere24f0b82006-06-23 02:03:51 -07001449 int retry = 1;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001450 int thp_retry = 1;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001451 int nr_failed = 0;
Mel Gorman5647bc22012-10-19 10:46:20 +01001452 int nr_succeeded = 0;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001453 int nr_thp_succeeded = 0;
1454 int nr_thp_failed = 0;
1455 int nr_thp_split = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001456 int pass = 0;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001457 bool is_thp = false;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001458 struct page *page;
1459 struct page *page2;
1460 int swapwrite = current->flags & PF_SWAPWRITE;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001461 int rc, nr_subpages;
Yang Shidd4ae782020-12-14 19:13:06 -08001462 LIST_HEAD(ret_pages);
Yang Shib0b515b2021-06-30 18:51:48 -07001463 bool nosplit = (reason == MR_NUMA_MISPLACED);
Christoph Lameterb20a3502006-03-22 00:09:12 -08001464
Liam Mark7bc1aec2021-05-04 18:37:25 -07001465 trace_mm_migrate_pages_start(mode, reason);
1466
Christoph Lameterb20a3502006-03-22 00:09:12 -08001467 if (!swapwrite)
1468 current->flags |= PF_SWAPWRITE;
1469
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001470 for (pass = 0; pass < 10 && (retry || thp_retry); pass++) {
Christoph Lametere24f0b82006-06-23 02:03:51 -07001471 retry = 0;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001472 thp_retry = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001473
Christoph Lametere24f0b82006-06-23 02:03:51 -07001474 list_for_each_entry_safe(page, page2, from, lru) {
Michal Hocko94723aa2018-04-10 16:30:07 -07001475retry:
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001476 /*
1477 * THP statistics is based on the source huge page.
1478 * Capture required information that might get lost
1479 * during migration.
1480 */
Zi Yan6c5c7b92020-09-25 21:19:14 -07001481 is_thp = PageTransHuge(page) && !PageHuge(page);
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -07001482 nr_subpages = thp_nr_pages(page);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001483 cond_resched();
Christoph Lameterb20a3502006-03-22 00:09:12 -08001484
Naoya Horiguchi31caf662013-09-11 14:21:59 -07001485 if (PageHuge(page))
1486 rc = unmap_and_move_huge_page(get_new_page,
David Rientjes68711a72014-06-04 16:08:25 -07001487 put_new_page, private, page,
Yang Shidd4ae782020-12-14 19:13:06 -08001488 pass > 2, mode, reason,
1489 &ret_pages);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07001490 else
David Rientjes68711a72014-06-04 16:08:25 -07001491 rc = unmap_and_move(get_new_page, put_new_page,
Naoya Horiguchiadd05ce2015-06-24 16:56:50 -07001492 private, page, pass > 2, mode,
Yang Shidd4ae782020-12-14 19:13:06 -08001493 reason, &ret_pages);
1494 /*
1495 * The rules are:
1496 * Success: non hugetlb page will be freed, hugetlb
1497 * page will be put back
1498 * -EAGAIN: stay on the from list
1499 * -ENOMEM: stay on the from list
1500 * Other errno: put on ret_pages list then splice to
1501 * from list
1502 */
Christoph Lametere24f0b82006-06-23 02:03:51 -07001503 switch(rc) {
Yang Shid532e2e2020-12-14 19:13:16 -08001504 /*
1505 * THP migration might be unsupported or the
1506 * allocation could've failed so we should
1507 * retry on the same page with the THP split
1508 * to base pages.
1509 *
1510 * Head page is retried immediately and tail
1511 * pages are added to the tail of the list so
1512 * we encounter them after the rest of the list
1513 * is processed.
1514 */
1515 case -ENOSYS:
1516 /* THP migration is unsupported */
1517 if (is_thp) {
1518 if (!try_split_thp(page, &page2, from)) {
1519 nr_thp_split++;
1520 goto retry;
1521 }
1522
1523 nr_thp_failed++;
1524 nr_failed += nr_subpages;
1525 break;
1526 }
1527
1528 /* Hugetlb migration is unsupported */
1529 nr_failed++;
1530 break;
Christoph Lameter95a402c2006-06-23 02:03:53 -07001531 case -ENOMEM:
Michal Hocko94723aa2018-04-10 16:30:07 -07001532 /*
Yang Shid532e2e2020-12-14 19:13:16 -08001533 * When memory is low, don't bother to try to migrate
1534 * other pages, just exit.
Yang Shib0b515b2021-06-30 18:51:48 -07001535 * THP NUMA faulting doesn't split THP to retry.
Michal Hocko94723aa2018-04-10 16:30:07 -07001536 */
Yang Shib0b515b2021-06-30 18:51:48 -07001537 if (is_thp && !nosplit) {
Yang Shid532e2e2020-12-14 19:13:16 -08001538 if (!try_split_thp(page, &page2, from)) {
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001539 nr_thp_split++;
Michal Hocko94723aa2018-04-10 16:30:07 -07001540 goto retry;
1541 }
Zi Yan6c5c7b92020-09-25 21:19:14 -07001542
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001543 nr_thp_failed++;
1544 nr_failed += nr_subpages;
1545 goto out;
1546 }
David Rientjesdfef2ef2016-05-20 16:59:05 -07001547 nr_failed++;
Christoph Lameter95a402c2006-06-23 02:03:53 -07001548 goto out;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001549 case -EAGAIN:
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001550 if (is_thp) {
1551 thp_retry++;
1552 break;
1553 }
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001554 retry++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001555 break;
Rafael Aquini78bd5202012-12-11 16:02:31 -08001556 case MIGRATEPAGE_SUCCESS:
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001557 if (is_thp) {
1558 nr_thp_succeeded++;
1559 nr_succeeded += nr_subpages;
1560 break;
1561 }
Mel Gorman5647bc22012-10-19 10:46:20 +01001562 nr_succeeded++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001563 break;
1564 default:
Naoya Horiguchi354a3362014-01-21 15:51:14 -08001565 /*
Yang Shid532e2e2020-12-14 19:13:16 -08001566 * Permanent failure (-EBUSY, etc.):
Naoya Horiguchi354a3362014-01-21 15:51:14 -08001567 * unlike -EAGAIN case, the failed page is
1568 * removed from migration page list and not
1569 * retried in the next outer loop.
1570 */
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001571 if (is_thp) {
1572 nr_thp_failed++;
1573 nr_failed += nr_subpages;
1574 break;
1575 }
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001576 nr_failed++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001577 break;
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001578 }
Christoph Lameterb20a3502006-03-22 00:09:12 -08001579 }
1580 }
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001581 nr_failed += retry + thp_retry;
1582 nr_thp_failed += thp_retry;
Vlastimil Babkaf2f81fb2015-11-05 18:47:03 -08001583 rc = nr_failed;
Christoph Lameter95a402c2006-06-23 02:03:53 -07001584out:
Yang Shidd4ae782020-12-14 19:13:06 -08001585 /*
1586 * Put the permanent failure page back to migration list, they
1587 * will be put back to the right list by the caller.
1588 */
1589 list_splice(&ret_pages, from);
1590
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001591 count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
1592 count_vm_events(PGMIGRATE_FAIL, nr_failed);
1593 count_vm_events(THP_MIGRATION_SUCCESS, nr_thp_succeeded);
1594 count_vm_events(THP_MIGRATION_FAIL, nr_thp_failed);
1595 count_vm_events(THP_MIGRATION_SPLIT, nr_thp_split);
1596 trace_mm_migrate_pages(nr_succeeded, nr_failed, nr_thp_succeeded,
1597 nr_thp_failed, nr_thp_split, mode, reason);
Mel Gorman7b2a2d42012-10-19 14:07:31 +01001598
Christoph Lameterb20a3502006-03-22 00:09:12 -08001599 if (!swapwrite)
1600 current->flags &= ~PF_SWAPWRITE;
1601
Yang Shi5ac95882021-09-02 14:59:13 -07001602 if (ret_succeeded)
1603 *ret_succeeded = nr_succeeded;
1604
Rafael Aquini78bd5202012-12-11 16:02:31 -08001605 return rc;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001606}
1607
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001608struct page *alloc_migration_target(struct page *page, unsigned long private)
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001609{
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001610 struct migration_target_control *mtc;
1611 gfp_t gfp_mask;
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001612 unsigned int order = 0;
1613 struct page *new_page = NULL;
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001614 int nid;
1615 int zidx;
1616
1617 mtc = (struct migration_target_control *)private;
1618 gfp_mask = mtc->gfp_mask;
1619 nid = mtc->nid;
1620 if (nid == NUMA_NO_NODE)
1621 nid = page_to_nid(page);
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001622
Joonsoo Kimd92bbc22020-08-11 18:37:17 -07001623 if (PageHuge(page)) {
1624 struct hstate *h = page_hstate(compound_head(page));
1625
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001626 gfp_mask = htlb_modify_alloc_mask(h, gfp_mask);
1627 return alloc_huge_page_nodemask(h, nid, mtc->nmask, gfp_mask);
Joonsoo Kimd92bbc22020-08-11 18:37:17 -07001628 }
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001629
1630 if (PageTransHuge(page)) {
Joonsoo Kim9933a0c2020-08-11 18:37:20 -07001631 /*
1632 * clear __GFP_RECLAIM to make the migration callback
1633 * consistent with regular THP allocations.
1634 */
1635 gfp_mask &= ~__GFP_RECLAIM;
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001636 gfp_mask |= GFP_TRANSHUGE;
1637 order = HPAGE_PMD_ORDER;
1638 }
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001639 zidx = zone_idx(page_zone(page));
1640 if (is_highmem_idx(zidx) || zidx == ZONE_MOVABLE)
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001641 gfp_mask |= __GFP_HIGHMEM;
1642
Matthew Wilcox (Oracle)84172f42021-04-29 23:01:15 -07001643 new_page = __alloc_pages(gfp_mask, order, nid, mtc->nmask);
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001644
1645 if (new_page && PageTransHuge(new_page))
1646 prep_transhuge_page(new_page);
1647
1648 return new_page;
1649}
1650
Christoph Lameter742755a2006-06-23 02:03:55 -07001651#ifdef CONFIG_NUMA
Christoph Lameter742755a2006-06-23 02:03:55 -07001652
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001653static int store_status(int __user *status, int start, int value, int nr)
Christoph Lameter742755a2006-06-23 02:03:55 -07001654{
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001655 while (nr-- > 0) {
1656 if (put_user(value, status + start))
1657 return -EFAULT;
1658 start++;
1659 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001660
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001661 return 0;
1662}
Christoph Lameter742755a2006-06-23 02:03:55 -07001663
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001664static int do_move_pages_to_node(struct mm_struct *mm,
1665 struct list_head *pagelist, int node)
1666{
1667 int err;
Joonsoo Kima0976312020-08-11 18:37:28 -07001668 struct migration_target_control mtc = {
1669 .nid = node,
1670 .gfp_mask = GFP_HIGHUSER_MOVABLE | __GFP_THISNODE,
1671 };
Christoph Lameter742755a2006-06-23 02:03:55 -07001672
Joonsoo Kima0976312020-08-11 18:37:28 -07001673 err = migrate_pages(pagelist, alloc_migration_target, NULL,
Yang Shi5ac95882021-09-02 14:59:13 -07001674 (unsigned long)&mtc, MIGRATE_SYNC, MR_SYSCALL, NULL);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001675 if (err)
1676 putback_movable_pages(pagelist);
1677 return err;
Christoph Lameter742755a2006-06-23 02:03:55 -07001678}
1679
1680/*
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001681 * Resolves the given address to a struct page, isolates it from the LRU and
1682 * puts it to the given pagelist.
Yang Shie0153fc2020-01-04 12:59:46 -08001683 * Returns:
1684 * errno - if the page cannot be found/isolated
1685 * 0 - when it doesn't have to be migrated because it is already on the
1686 * target node
1687 * 1 - when it has been queued
Christoph Lameter742755a2006-06-23 02:03:55 -07001688 */
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001689static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
1690 int node, struct list_head *pagelist, bool migrate_all)
Christoph Lameter742755a2006-06-23 02:03:55 -07001691{
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001692 struct vm_area_struct *vma;
1693 struct page *page;
1694 unsigned int follflags;
Christoph Lameter742755a2006-06-23 02:03:55 -07001695 int err;
Christoph Lameter742755a2006-06-23 02:03:55 -07001696
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001697 mmap_read_lock(mm);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001698 err = -EFAULT;
1699 vma = find_vma(mm, addr);
1700 if (!vma || addr < vma->vm_start || !vma_migratable(vma))
1701 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001702
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001703 /* FOLL_DUMP to ignore special (like zero) pages */
1704 follflags = FOLL_GET | FOLL_DUMP;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001705 page = follow_page(vma, addr, follflags);
Christoph Lameter742755a2006-06-23 02:03:55 -07001706
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001707 err = PTR_ERR(page);
1708 if (IS_ERR(page))
1709 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001710
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001711 err = -ENOENT;
1712 if (!page)
1713 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001714
Brice Gogline78bbfa2008-10-18 20:27:15 -07001715 err = 0;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001716 if (page_to_nid(page) == node)
1717 goto out_putpage;
Christoph Lameter742755a2006-06-23 02:03:55 -07001718
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001719 err = -EACCES;
1720 if (page_mapcount(page) > 1 && !migrate_all)
1721 goto out_putpage;
1722
1723 if (PageHuge(page)) {
1724 if (PageHead(page)) {
Miaohe Lin072e7412022-05-30 19:30:15 +08001725 err = isolate_hugetlb(page, pagelist);
1726 if (!err)
1727 err = 1;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001728 }
1729 } else {
1730 struct page *head;
1731
1732 head = compound_head(page);
1733 err = isolate_lru_page(head);
1734 if (err)
1735 goto out_putpage;
1736
Yang Shie0153fc2020-01-04 12:59:46 -08001737 err = 1;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001738 list_add_tail(&head->lru, pagelist);
1739 mod_node_page_state(page_pgdat(head),
Huang Ying9de4f222020-04-06 20:04:41 -07001740 NR_ISOLATED_ANON + page_is_file_lru(head),
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -07001741 thp_nr_pages(head));
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001742 }
1743out_putpage:
1744 /*
1745 * Either remove the duplicate refcount from
1746 * isolate_lru_page() or drop the page ref if it was
1747 * not isolated.
1748 */
1749 put_page(page);
1750out:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001751 mmap_read_unlock(mm);
Christoph Lameter742755a2006-06-23 02:03:55 -07001752 return err;
1753}
1754
Wei Yang7ca87832020-04-06 20:04:12 -07001755static int move_pages_and_store_status(struct mm_struct *mm, int node,
1756 struct list_head *pagelist, int __user *status,
1757 int start, int i, unsigned long nr_pages)
1758{
1759 int err;
1760
Wei Yang5d7ae892020-04-06 20:04:15 -07001761 if (list_empty(pagelist))
1762 return 0;
1763
Wei Yang7ca87832020-04-06 20:04:12 -07001764 err = do_move_pages_to_node(mm, pagelist, node);
1765 if (err) {
1766 /*
1767 * Positive err means the number of failed
1768 * pages to migrate. Since we are going to
1769 * abort and return the number of non-migrated
Long Liab9dd4f2020-12-14 19:12:52 -08001770 * pages, so need to include the rest of the
Wei Yang7ca87832020-04-06 20:04:12 -07001771 * nr_pages that have not been attempted as
1772 * well.
1773 */
1774 if (err > 0)
1775 err += nr_pages - i - 1;
1776 return err;
1777 }
1778 return store_status(status, start, node, i - start);
1779}
1780
Christoph Lameter742755a2006-06-23 02:03:55 -07001781/*
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001782 * Migrate an array of page address onto an array of nodes and fill
1783 * the corresponding array of status.
1784 */
Christoph Lameter3268c632012-03-21 16:34:06 -07001785static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001786 unsigned long nr_pages,
1787 const void __user * __user *pages,
1788 const int __user *nodes,
1789 int __user *status, int flags)
1790{
Gregory Price556b68d2023-10-03 10:48:56 -04001791 compat_uptr_t __user *compat_pages = (void __user *)pages;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001792 int current_node = NUMA_NO_NODE;
1793 LIST_HEAD(pagelist);
1794 int start, i;
1795 int err = 0, err1;
Brice Goglin35282a22009-06-16 15:32:43 -07001796
Minchan Kim361a2a22021-05-04 18:36:57 -07001797 lru_cache_disable();
Brice Goglin35282a22009-06-16 15:32:43 -07001798
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001799 for (i = start = 0; i < nr_pages; i++) {
1800 const void __user *p;
1801 unsigned long addr;
1802 int node;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001803
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001804 err = -EFAULT;
Gregory Price556b68d2023-10-03 10:48:56 -04001805 if (in_compat_syscall()) {
1806 compat_uptr_t cp;
1807
1808 if (get_user(cp, compat_pages + i))
1809 goto out_flush;
1810
1811 p = compat_ptr(cp);
1812 } else {
1813 if (get_user(p, pages + i))
1814 goto out_flush;
1815 }
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001816 if (get_user(node, nodes + i))
1817 goto out_flush;
Andrey Konovalov057d33892019-09-25 16:48:30 -07001818 addr = (unsigned long)untagged_addr(p);
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001819
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001820 err = -ENODEV;
1821 if (node < 0 || node >= MAX_NUMNODES)
1822 goto out_flush;
1823 if (!node_state(node, N_MEMORY))
1824 goto out_flush;
Brice Goglin3140a222009-01-06 14:38:57 -08001825
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001826 err = -EACCES;
1827 if (!node_isset(node, task_nodes))
1828 goto out_flush;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001829
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001830 if (current_node == NUMA_NO_NODE) {
1831 current_node = node;
1832 start = i;
1833 } else if (node != current_node) {
Wei Yang7ca87832020-04-06 20:04:12 -07001834 err = move_pages_and_store_status(mm, current_node,
1835 &pagelist, status, start, i, nr_pages);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001836 if (err)
1837 goto out;
1838 start = i;
1839 current_node = node;
Brice Goglin3140a222009-01-06 14:38:57 -08001840 }
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001841
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001842 /*
1843 * Errors in the page lookup or isolation are not fatal and we simply
1844 * report them via status
1845 */
1846 err = add_page_for_migration(mm, addr, current_node,
1847 &pagelist, flags & MPOL_MF_MOVE_ALL);
Yang Shie0153fc2020-01-04 12:59:46 -08001848
Wei Yangd08221a2020-04-06 20:04:18 -07001849 if (err > 0) {
Yang Shie0153fc2020-01-04 12:59:46 -08001850 /* The page is successfully queued for migration */
1851 continue;
1852 }
Brice Goglin3140a222009-01-06 14:38:57 -08001853
Wei Yangd08221a2020-04-06 20:04:18 -07001854 /*
1855 * If the page is already on the target node (!err), store the
1856 * node, otherwise, store the err.
1857 */
1858 err = store_status(status, i, err ? : current_node, 1);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001859 if (err)
1860 goto out_flush;
Brice Goglin3140a222009-01-06 14:38:57 -08001861
Wei Yang7ca87832020-04-06 20:04:12 -07001862 err = move_pages_and_store_status(mm, current_node, &pagelist,
1863 status, start, i, nr_pages);
Wei Yang4afdace2020-04-06 20:04:09 -07001864 if (err)
1865 goto out;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001866 current_node = NUMA_NO_NODE;
Brice Goglin3140a222009-01-06 14:38:57 -08001867 }
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001868out_flush:
1869 /* Make sure we do not overwrite the existing error */
Wei Yang7ca87832020-04-06 20:04:12 -07001870 err1 = move_pages_and_store_status(mm, current_node, &pagelist,
1871 status, start, i, nr_pages);
Wei Yangdfe9aa22020-01-30 22:11:14 -08001872 if (err >= 0)
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001873 err = err1;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001874out:
Minchan Kim361a2a22021-05-04 18:36:57 -07001875 lru_cache_enable();
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001876 return err;
1877}
1878
1879/*
Brice Goglin2f007e72008-10-18 20:27:16 -07001880 * Determine the nodes of an array of pages and store it in an array of status.
Christoph Lameter742755a2006-06-23 02:03:55 -07001881 */
Brice Goglin80bba122008-12-09 13:14:23 -08001882static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1883 const void __user **pages, int *status)
Christoph Lameter742755a2006-06-23 02:03:55 -07001884{
Brice Goglin2f007e72008-10-18 20:27:16 -07001885 unsigned long i;
Brice Goglin2f007e72008-10-18 20:27:16 -07001886
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001887 mmap_read_lock(mm);
Christoph Lameter742755a2006-06-23 02:03:55 -07001888
Brice Goglin2f007e72008-10-18 20:27:16 -07001889 for (i = 0; i < nr_pages; i++) {
Brice Goglin80bba122008-12-09 13:14:23 -08001890 unsigned long addr = (unsigned long)(*pages);
Christoph Lameter742755a2006-06-23 02:03:55 -07001891 struct vm_area_struct *vma;
1892 struct page *page;
KOSAKI Motohiroc095adb2008-12-16 16:06:43 +09001893 int err = -EFAULT;
Brice Goglin2f007e72008-10-18 20:27:16 -07001894
Liam Howlett059b8b42021-06-28 19:39:44 -07001895 vma = vma_lookup(mm, addr);
1896 if (!vma)
Christoph Lameter742755a2006-06-23 02:03:55 -07001897 goto set_status;
1898
Kirill A. Shutemovd8998442015-09-04 15:47:53 -07001899 /* FOLL_DUMP to ignore special (like zero) pages */
1900 page = follow_page(vma, addr, FOLL_DUMP);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001901
1902 err = PTR_ERR(page);
1903 if (IS_ERR(page))
1904 goto set_status;
1905
Kirill A. Shutemovd8998442015-09-04 15:47:53 -07001906 err = page ? page_to_nid(page) : -ENOENT;
Christoph Lameter742755a2006-06-23 02:03:55 -07001907set_status:
Brice Goglin80bba122008-12-09 13:14:23 -08001908 *status = err;
1909
1910 pages++;
1911 status++;
1912 }
1913
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001914 mmap_read_unlock(mm);
Brice Goglin80bba122008-12-09 13:14:23 -08001915}
1916
Arnd Bergmann5b1b5612021-09-08 15:18:17 -07001917static int get_compat_pages_array(const void __user *chunk_pages[],
1918 const void __user * __user *pages,
1919 unsigned long chunk_nr)
1920{
1921 compat_uptr_t __user *pages32 = (compat_uptr_t __user *)pages;
1922 compat_uptr_t p;
1923 int i;
1924
1925 for (i = 0; i < chunk_nr; i++) {
1926 if (get_user(p, pages32 + i))
1927 return -EFAULT;
1928 chunk_pages[i] = compat_ptr(p);
1929 }
1930
1931 return 0;
1932}
1933
Brice Goglin80bba122008-12-09 13:14:23 -08001934/*
1935 * Determine the nodes of a user array of pages and store it in
1936 * a user array of status.
1937 */
1938static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1939 const void __user * __user *pages,
1940 int __user *status)
1941{
1942#define DO_PAGES_STAT_CHUNK_NR 16
1943 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1944 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
Brice Goglin80bba122008-12-09 13:14:23 -08001945
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001946 while (nr_pages) {
1947 unsigned long chunk_nr;
Brice Goglin80bba122008-12-09 13:14:23 -08001948
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001949 chunk_nr = nr_pages;
1950 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1951 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1952
Arnd Bergmann5b1b5612021-09-08 15:18:17 -07001953 if (in_compat_syscall()) {
1954 if (get_compat_pages_array(chunk_pages, pages,
1955 chunk_nr))
1956 break;
1957 } else {
1958 if (copy_from_user(chunk_pages, pages,
1959 chunk_nr * sizeof(*chunk_pages)))
1960 break;
1961 }
Brice Goglin80bba122008-12-09 13:14:23 -08001962
1963 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1964
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001965 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1966 break;
Christoph Lameter742755a2006-06-23 02:03:55 -07001967
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001968 pages += chunk_nr;
1969 status += chunk_nr;
1970 nr_pages -= chunk_nr;
1971 }
1972 return nr_pages ? -EFAULT : 0;
Christoph Lameter742755a2006-06-23 02:03:55 -07001973}
1974
Miaohe Lin4dc200c2020-10-17 16:14:03 -07001975static struct mm_struct *find_mm_struct(pid_t pid, nodemask_t *mem_nodes)
1976{
1977 struct task_struct *task;
1978 struct mm_struct *mm;
1979
1980 /*
1981 * There is no need to check if current process has the right to modify
1982 * the specified process when they are same.
1983 */
1984 if (!pid) {
1985 mmget(current->mm);
1986 *mem_nodes = cpuset_mems_allowed(current);
1987 return current->mm;
1988 }
1989
1990 /* Find the mm_struct */
1991 rcu_read_lock();
1992 task = find_task_by_vpid(pid);
1993 if (!task) {
1994 rcu_read_unlock();
1995 return ERR_PTR(-ESRCH);
1996 }
1997 get_task_struct(task);
1998
1999 /*
2000 * Check if this process has the right to modify the specified
2001 * process. Use the regular "ptrace_may_access()" checks.
2002 */
2003 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
2004 rcu_read_unlock();
2005 mm = ERR_PTR(-EPERM);
2006 goto out;
2007 }
2008 rcu_read_unlock();
2009
2010 mm = ERR_PTR(security_task_movememory(task));
2011 if (IS_ERR(mm))
2012 goto out;
2013 *mem_nodes = cpuset_mems_allowed(task);
2014 mm = get_task_mm(task);
2015out:
2016 put_task_struct(task);
2017 if (!mm)
2018 mm = ERR_PTR(-EINVAL);
2019 return mm;
2020}
2021
Christoph Lameter742755a2006-06-23 02:03:55 -07002022/*
2023 * Move a list of pages in the address space of the currently executing
2024 * process.
2025 */
Dominik Brodowski7addf442018-03-17 16:08:03 +01002026static int kernel_move_pages(pid_t pid, unsigned long nr_pages,
2027 const void __user * __user *pages,
2028 const int __user *nodes,
2029 int __user *status, int flags)
Christoph Lameter742755a2006-06-23 02:03:55 -07002030{
Christoph Lameter742755a2006-06-23 02:03:55 -07002031 struct mm_struct *mm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07002032 int err;
Christoph Lameter3268c632012-03-21 16:34:06 -07002033 nodemask_t task_nodes;
Christoph Lameter742755a2006-06-23 02:03:55 -07002034
2035 /* Check flags */
2036 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
2037 return -EINVAL;
2038
2039 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
2040 return -EPERM;
2041
Miaohe Lin4dc200c2020-10-17 16:14:03 -07002042 mm = find_mm_struct(pid, &task_nodes);
2043 if (IS_ERR(mm))
2044 return PTR_ERR(mm);
Sasha Levin6e8b09e2012-04-25 16:01:53 -07002045
2046 if (nodes)
2047 err = do_pages_move(mm, task_nodes, nr_pages, pages,
2048 nodes, status, flags);
2049 else
2050 err = do_pages_stat(mm, nr_pages, pages, status);
Christoph Lameter3268c632012-03-21 16:34:06 -07002051
2052 mmput(mm);
2053 return err;
Christoph Lameter742755a2006-06-23 02:03:55 -07002054}
Christoph Lameter742755a2006-06-23 02:03:55 -07002055
Dominik Brodowski7addf442018-03-17 16:08:03 +01002056SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
2057 const void __user * __user *, pages,
2058 const int __user *, nodes,
2059 int __user *, status, int, flags)
2060{
2061 return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags);
2062}
2063
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002064#ifdef CONFIG_NUMA_BALANCING
2065/*
2066 * Returns true if this is a safe migration target node for misplaced NUMA
2067 * pages. Currently it only checks the watermarks which crude
2068 */
2069static bool migrate_balanced_pgdat(struct pglist_data *pgdat,
Mel Gorman3abef4e2013-02-22 16:34:27 -08002070 unsigned long nr_migrate_pages)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002071{
2072 int z;
Mel Gorman599d0c92016-07-28 15:45:31 -07002073
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002074 for (z = pgdat->nr_zones - 1; z >= 0; z--) {
2075 struct zone *zone = pgdat->node_zones + z;
2076
2077 if (!populated_zone(zone))
2078 continue;
2079
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002080 /* Avoid waking kswapd by allocating pages_to_migrate pages. */
2081 if (!zone_watermark_ok(zone, 0,
2082 high_wmark_pages(zone) +
2083 nr_migrate_pages,
Huang Yingbfe9d002019-11-30 17:57:28 -08002084 ZONE_MOVABLE, 0))
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002085 continue;
2086 return true;
2087 }
2088 return false;
2089}
2090
2091static struct page *alloc_misplaced_dst_page(struct page *page,
Michal Hocko666feb22018-04-10 16:30:03 -07002092 unsigned long data)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002093{
2094 int nid = (int) data;
2095 struct page *newpage;
2096
Vlastimil Babka96db8002015-09-08 15:03:50 -07002097 newpage = __alloc_pages_node(nid,
Johannes Weinere97ca8e52014-03-10 15:49:43 -07002098 (GFP_HIGHUSER_MOVABLE |
2099 __GFP_THISNODE | __GFP_NOMEMALLOC |
2100 __GFP_NORETRY | __GFP_NOWARN) &
Mel Gorman8479eba2016-02-26 15:19:31 -08002101 ~__GFP_RECLAIM, 0);
Hillf Dantonbac03822012-11-27 14:46:24 +00002102
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002103 return newpage;
2104}
2105
Yang Shic5b5a3d2021-06-30 18:51:42 -07002106static struct page *alloc_misplaced_dst_page_thp(struct page *page,
2107 unsigned long data)
2108{
2109 int nid = (int) data;
2110 struct page *newpage;
2111
2112 newpage = alloc_pages_node(nid, (GFP_TRANSHUGE_LIGHT | __GFP_THISNODE),
2113 HPAGE_PMD_ORDER);
2114 if (!newpage)
2115 goto out;
2116
2117 prep_transhuge_page(newpage);
2118
2119out:
2120 return newpage;
2121}
2122
Mel Gorman1c30e012014-01-21 15:50:58 -08002123static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
Mel Gormanb32967f2012-11-19 12:35:47 +00002124{
Hugh Dickins340ef392013-02-22 16:34:33 -08002125 int page_lru;
Baolin Wang2b9b6242021-09-08 15:18:01 -07002126 int nr_pages = thp_nr_pages(page);
Mel Gormanb32967f2012-11-19 12:35:47 +00002127
Sasha Levin309381fea2014-01-23 15:52:54 -08002128 VM_BUG_ON_PAGE(compound_order(page) && !PageTransHuge(page), page);
Mel Gorman3abef4e2013-02-22 16:34:27 -08002129
Yang Shi662aeea2021-06-30 18:51:51 -07002130 /* Do not migrate THP mapped by multiple processes */
2131 if (PageTransHuge(page) && total_mapcount(page) > 1)
2132 return 0;
2133
Mel Gormanb32967f2012-11-19 12:35:47 +00002134 /* Avoid migrating to a node that is nearly full */
Baolin Wang2b9b6242021-09-08 15:18:01 -07002135 if (!migrate_balanced_pgdat(pgdat, nr_pages))
Hugh Dickins340ef392013-02-22 16:34:33 -08002136 return 0;
Mel Gormanb32967f2012-11-19 12:35:47 +00002137
Hugh Dickins340ef392013-02-22 16:34:33 -08002138 if (isolate_lru_page(page))
2139 return 0;
Mel Gormanb32967f2012-11-19 12:35:47 +00002140
Huang Ying9de4f222020-04-06 20:04:41 -07002141 page_lru = page_is_file_lru(page);
Mel Gorman599d0c92016-07-28 15:45:31 -07002142 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_lru,
Baolin Wang2b9b6242021-09-08 15:18:01 -07002143 nr_pages);
Hugh Dickins340ef392013-02-22 16:34:33 -08002144
2145 /*
2146 * Isolating the page has taken another reference, so the
2147 * caller's reference can be safely dropped without the page
2148 * disappearing underneath us during migration.
Mel Gormanb32967f2012-11-19 12:35:47 +00002149 */
2150 put_page(page);
Hugh Dickins340ef392013-02-22 16:34:33 -08002151 return 1;
Mel Gormanb32967f2012-11-19 12:35:47 +00002152}
2153
Mel Gormana8f60772012-11-14 21:41:46 +00002154/*
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002155 * Attempt to migrate a misplaced page to the specified destination
2156 * node. Caller is expected to have an elevated reference count on
2157 * the page that will be dropped by this function before returning.
2158 */
Mel Gorman1bc115d2013-10-07 11:29:05 +01002159int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma,
2160 int node)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002161{
Mel Gormana8f60772012-11-14 21:41:46 +00002162 pg_data_t *pgdat = NODE_DATA(node);
Hugh Dickins340ef392013-02-22 16:34:33 -08002163 int isolated;
Mel Gormanb32967f2012-11-19 12:35:47 +00002164 int nr_remaining;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002165 LIST_HEAD(migratepages);
Yang Shic5b5a3d2021-06-30 18:51:42 -07002166 new_page_t *new;
2167 bool compound;
Aneesh Kumar K.Vb5916c02021-07-29 14:53:47 -07002168 int nr_pages = thp_nr_pages(page);
Yang Shic5b5a3d2021-06-30 18:51:42 -07002169
2170 /*
2171 * PTE mapped THP or HugeTLB page can't reach here so the page could
2172 * be either base page or THP. And it must be head page if it is
2173 * THP.
2174 */
2175 compound = PageTransHuge(page);
2176
2177 if (compound)
2178 new = alloc_misplaced_dst_page_thp;
2179 else
2180 new = alloc_misplaced_dst_page;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002181
2182 /*
Mel Gorman1bc115d2013-10-07 11:29:05 +01002183 * Don't migrate file pages that are mapped in multiple processes
2184 * with execute permissions as they are probably shared libraries.
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002185 */
Miaohe Lin7ee820e2021-05-04 18:37:16 -07002186 if (page_mapcount(page) != 1 && page_is_file_lru(page) &&
2187 (vma->vm_flags & VM_EXEC))
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002188 goto out;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002189
Mel Gormana8f60772012-11-14 21:41:46 +00002190 /*
Mel Gorman09a913a2018-04-10 16:29:20 -07002191 * Also do not migrate dirty pages as not all filesystems can move
2192 * dirty pages in MIGRATE_ASYNC mode which is a waste of cycles.
2193 */
Huang Ying9de4f222020-04-06 20:04:41 -07002194 if (page_is_file_lru(page) && PageDirty(page))
Mel Gorman09a913a2018-04-10 16:29:20 -07002195 goto out;
2196
Mel Gormanb32967f2012-11-19 12:35:47 +00002197 isolated = numamigrate_isolate_page(pgdat, page);
2198 if (!isolated)
2199 goto out;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002200
Mel Gormanb32967f2012-11-19 12:35:47 +00002201 list_add(&page->lru, &migratepages);
Yang Shic5b5a3d2021-06-30 18:51:42 -07002202 nr_remaining = migrate_pages(&migratepages, *new, NULL, node,
Yang Shi5ac95882021-09-02 14:59:13 -07002203 MIGRATE_ASYNC, MR_NUMA_MISPLACED, NULL);
Mel Gormanb32967f2012-11-19 12:35:47 +00002204 if (nr_remaining) {
Joonsoo Kim59c82b72014-01-21 15:51:17 -08002205 if (!list_empty(&migratepages)) {
2206 list_del(&page->lru);
Yang Shic5fc5c32021-06-30 18:51:45 -07002207 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
2208 page_is_file_lru(page), -nr_pages);
Joonsoo Kim59c82b72014-01-21 15:51:17 -08002209 putback_lru_page(page);
2210 }
Mel Gormanb32967f2012-11-19 12:35:47 +00002211 isolated = 0;
2212 } else
Yang Shic5fc5c32021-06-30 18:51:45 -07002213 count_vm_numa_events(NUMA_PAGE_MIGRATE, nr_pages);
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002214 BUG_ON(!list_empty(&migratepages));
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002215 return isolated;
Hugh Dickins340ef392013-02-22 16:34:33 -08002216
2217out:
2218 put_page(page);
2219 return 0;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002220}
Mel Gorman220018d2012-12-05 09:32:56 +00002221#endif /* CONFIG_NUMA_BALANCING */
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002222#endif /* CONFIG_NUMA */
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002223
Christoph Hellwig9b2ed9c2019-08-14 09:59:28 +02002224#ifdef CONFIG_DEVICE_PRIVATE
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002225static int migrate_vma_collect_skip(unsigned long start,
2226 unsigned long end,
2227 struct mm_walk *walk)
2228{
2229 struct migrate_vma *migrate = walk->private;
2230 unsigned long addr;
2231
Ralph Campbell872ea702020-01-30 22:14:38 -08002232 for (addr = start; addr < end; addr += PAGE_SIZE) {
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002233 migrate->dst[migrate->npages] = 0;
2234 migrate->src[migrate->npages++] = 0;
2235 }
2236
2237 return 0;
2238}
2239
Miaohe Lin843e1be2021-05-04 18:37:13 -07002240static int migrate_vma_collect_hole(unsigned long start,
2241 unsigned long end,
2242 __always_unused int depth,
2243 struct mm_walk *walk)
2244{
2245 struct migrate_vma *migrate = walk->private;
2246 unsigned long addr;
2247
2248 /* Only allow populating anonymous memory. */
2249 if (!vma_is_anonymous(walk->vma))
2250 return migrate_vma_collect_skip(start, end, walk);
2251
2252 for (addr = start; addr < end; addr += PAGE_SIZE) {
2253 migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE;
2254 migrate->dst[migrate->npages] = 0;
2255 migrate->npages++;
2256 migrate->cpages++;
2257 }
2258
2259 return 0;
2260}
2261
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002262static int migrate_vma_collect_pmd(pmd_t *pmdp,
2263 unsigned long start,
2264 unsigned long end,
2265 struct mm_walk *walk)
2266{
2267 struct migrate_vma *migrate = walk->private;
2268 struct vm_area_struct *vma = walk->vma;
2269 struct mm_struct *mm = vma->vm_mm;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002270 unsigned long addr = start, unmapped = 0;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002271 spinlock_t *ptl;
2272 pte_t *ptep;
2273
2274again:
2275 if (pmd_none(*pmdp))
Steven Priceb7a16c72020-02-03 17:36:03 -08002276 return migrate_vma_collect_hole(start, end, -1, walk);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002277
2278 if (pmd_trans_huge(*pmdp)) {
2279 struct page *page;
2280
2281 ptl = pmd_lock(mm, pmdp);
2282 if (unlikely(!pmd_trans_huge(*pmdp))) {
2283 spin_unlock(ptl);
2284 goto again;
2285 }
2286
2287 page = pmd_page(*pmdp);
2288 if (is_huge_zero_page(page)) {
2289 spin_unlock(ptl);
2290 split_huge_pmd(vma, pmdp, addr);
2291 if (pmd_trans_unstable(pmdp))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002292 return migrate_vma_collect_skip(start, end,
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002293 walk);
2294 } else {
2295 int ret;
2296
2297 get_page(page);
2298 spin_unlock(ptl);
2299 if (unlikely(!trylock_page(page)))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002300 return migrate_vma_collect_skip(start, end,
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002301 walk);
2302 ret = split_huge_page(page);
2303 unlock_page(page);
2304 put_page(page);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002305 if (ret)
2306 return migrate_vma_collect_skip(start, end,
2307 walk);
2308 if (pmd_none(*pmdp))
Steven Priceb7a16c72020-02-03 17:36:03 -08002309 return migrate_vma_collect_hole(start, end, -1,
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002310 walk);
2311 }
2312 }
2313
2314 if (unlikely(pmd_bad(*pmdp)))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002315 return migrate_vma_collect_skip(start, end, walk);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002316
2317 ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002318 arch_enter_lazy_mmu_mode();
2319
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002320 for (; addr < end; addr += PAGE_SIZE, ptep++) {
Christoph Hellwig800bb1c2020-03-16 20:32:14 +01002321 unsigned long mpfn = 0, pfn;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002322 struct page *page;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002323 swp_entry_t entry;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002324 pte_t pte;
2325
2326 pte = *ptep;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002327
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002328 if (pte_none(pte)) {
Ralph Campbell0744f282020-08-11 18:31:41 -07002329 if (vma_is_anonymous(vma)) {
2330 mpfn = MIGRATE_PFN_MIGRATE;
2331 migrate->cpages++;
2332 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002333 goto next;
2334 }
2335
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002336 if (!pte_present(pte)) {
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002337 /*
2338 * Only care about unaddressable device page special
2339 * page table entry. Other special swap entries are not
2340 * migratable, and we ignore regular swapped page.
2341 */
2342 entry = pte_to_swp_entry(pte);
2343 if (!is_device_private_entry(entry))
2344 goto next;
2345
Alistair Poppleaf5cdaf2021-06-30 18:54:06 -07002346 page = pfn_swap_entry_to_page(entry);
Ralph Campbell51431922020-07-23 15:30:00 -07002347 if (!(migrate->flags &
2348 MIGRATE_VMA_SELECT_DEVICE_PRIVATE) ||
2349 page->pgmap->owner != migrate->pgmap_owner)
Christoph Hellwig800bb1c2020-03-16 20:32:14 +01002350 goto next;
2351
Christoph Hellwig06d462b2019-08-14 09:59:27 +02002352 mpfn = migrate_pfn(page_to_pfn(page)) |
2353 MIGRATE_PFN_MIGRATE;
Alistair Popple4dd845b2021-06-30 18:54:09 -07002354 if (is_writable_device_private_entry(entry))
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002355 mpfn |= MIGRATE_PFN_WRITE;
2356 } else {
Ralph Campbell51431922020-07-23 15:30:00 -07002357 if (!(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM))
Christoph Hellwig800bb1c2020-03-16 20:32:14 +01002358 goto next;
Pingfan Liu276f7562019-09-23 15:37:38 -07002359 pfn = pte_pfn(pte);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002360 if (is_zero_pfn(pfn)) {
2361 mpfn = MIGRATE_PFN_MIGRATE;
2362 migrate->cpages++;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002363 goto next;
2364 }
Christoph Hellwig25b29952019-06-13 22:50:49 +02002365 page = vm_normal_page(migrate->vma, addr, pte);
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002366 mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
2367 mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0;
2368 }
2369
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002370 /* FIXME support THP */
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002371 if (!page || !page->mapping || PageTransCompound(page)) {
Pingfan Liu276f7562019-09-23 15:37:38 -07002372 mpfn = 0;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002373 goto next;
2374 }
2375
2376 /*
2377 * By getting a reference on the page we pin it and that blocks
2378 * any kind of migration. Side effect is that it "freezes" the
2379 * pte.
2380 *
2381 * We drop this reference after isolating the page from the lru
2382 * for non device page (device page are not on the lru and thus
2383 * can't be dropped from it).
2384 */
2385 get_page(page);
2386 migrate->cpages++;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002387
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002388 /*
2389 * Optimize for the common case where page is only mapped once
2390 * in one process. If we can lock the page, then we can safely
2391 * set up a special migration page table entry now.
2392 */
2393 if (trylock_page(page)) {
2394 pte_t swp_pte;
2395
2396 mpfn |= MIGRATE_PFN_LOCKED;
2397 ptep_get_and_clear(mm, addr, ptep);
2398
2399 /* Setup special migration page table entry */
Alistair Popple4dd845b2021-06-30 18:54:09 -07002400 if (mpfn & MIGRATE_PFN_WRITE)
2401 entry = make_writable_migration_entry(
2402 page_to_pfn(page));
2403 else
2404 entry = make_readable_migration_entry(
2405 page_to_pfn(page));
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002406 swp_pte = swp_entry_to_pte(entry);
Alistair Popplead7df762020-09-04 16:36:01 -07002407 if (pte_present(pte)) {
2408 if (pte_soft_dirty(pte))
2409 swp_pte = pte_swp_mksoft_dirty(swp_pte);
2410 if (pte_uffd_wp(pte))
2411 swp_pte = pte_swp_mkuffd_wp(swp_pte);
2412 } else {
2413 if (pte_swp_soft_dirty(pte))
2414 swp_pte = pte_swp_mksoft_dirty(swp_pte);
2415 if (pte_swp_uffd_wp(pte))
2416 swp_pte = pte_swp_mkuffd_wp(swp_pte);
2417 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002418 set_pte_at(mm, addr, ptep, swp_pte);
2419
2420 /*
2421 * This is like regular unmap: we remove the rmap and
2422 * drop page refcount. Page won't be freed, as we took
2423 * a reference just above.
2424 */
2425 page_remove_rmap(page, false);
2426 put_page(page);
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002427
2428 if (pte_present(pte))
2429 unmapped++;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002430 }
2431
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002432next:
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002433 migrate->dst[migrate->npages] = 0;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002434 migrate->src[migrate->npages++] = mpfn;
2435 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002436
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002437 /* Only flush the TLB if we actually modified any entries */
2438 if (unmapped)
2439 flush_tlb_range(walk->vma, start, end);
2440
Alistair Popple1299c112022-09-02 10:35:51 +10002441 arch_leave_lazy_mmu_mode();
2442 pte_unmap_unlock(ptep - 1, ptl);
2443
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002444 return 0;
2445}
2446
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02002447static const struct mm_walk_ops migrate_vma_walk_ops = {
2448 .pmd_entry = migrate_vma_collect_pmd,
2449 .pte_hole = migrate_vma_collect_hole,
2450};
2451
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002452/*
2453 * migrate_vma_collect() - collect pages over a range of virtual addresses
2454 * @migrate: migrate struct containing all migration information
2455 *
2456 * This will walk the CPU page table. For each virtual address backed by a
2457 * valid page, it updates the src array and takes a reference on the page, in
2458 * order to pin the page until we lock it and unmap it.
2459 */
2460static void migrate_vma_collect(struct migrate_vma *migrate)
2461{
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002462 struct mmu_notifier_range range;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002463
Ralph Campbell998427b2020-07-23 15:30:01 -07002464 /*
2465 * Note that the pgmap_owner is passed to the mmu notifier callback so
2466 * that the registered device driver can skip invalidating device
2467 * private page mappings that won't be migrated.
2468 */
Alistair Popple6b49bf62021-06-30 18:54:19 -07002469 mmu_notifier_range_init_owner(&range, MMU_NOTIFY_MIGRATE, 0,
2470 migrate->vma, migrate->vma->vm_mm, migrate->start, migrate->end,
Ralph Campbellc1a06df2020-08-06 23:17:09 -07002471 migrate->pgmap_owner);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002472 mmu_notifier_invalidate_range_start(&range);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002473
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02002474 walk_page_range(migrate->vma->vm_mm, migrate->start, migrate->end,
2475 &migrate_vma_walk_ops, migrate);
2476
2477 mmu_notifier_invalidate_range_end(&range);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002478 migrate->end = migrate->start + (migrate->npages << PAGE_SHIFT);
2479}
2480
2481/*
2482 * migrate_vma_check_page() - check if page is pinned or not
2483 * @page: struct page to check
2484 *
2485 * Pinned pages cannot be migrated. This is the same test as in
2486 * migrate_page_move_mapping(), except that here we allow migration of a
2487 * ZONE_DEVICE page.
2488 */
2489static bool migrate_vma_check_page(struct page *page)
2490{
2491 /*
2492 * One extra ref because caller holds an extra reference, either from
2493 * isolate_lru_page() for a regular page, or migrate_vma_collect() for
2494 * a device page.
2495 */
2496 int extra = 1;
2497
2498 /*
2499 * FIXME support THP (transparent huge page), it is bit more complex to
2500 * check them than regular pages, because they can be mapped with a pmd
2501 * or with a pte (split pte mapping).
2502 */
2503 if (PageCompound(page))
2504 return false;
2505
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002506 /* Page from ZONE_DEVICE have one extra reference */
2507 if (is_zone_device_page(page)) {
2508 /*
2509 * Private page can never be pin as they have no valid pte and
2510 * GUP will fail for those. Yet if there is a pending migration
2511 * a thread might try to wait on the pte migration entry and
2512 * will bump the page reference count. Sadly there is no way to
2513 * differentiate a regular pin from migration wait. Hence to
2514 * avoid 2 racing thread trying to migrate back to CPU to enter
Haitao Shi8958b242020-12-15 20:47:26 -08002515 * infinite loop (one stopping migration because the other is
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002516 * waiting on pte migration entry). We always return true here.
2517 *
2518 * FIXME proper solution is to rework migration_entry_wait() so
2519 * it does not need to take a reference on page.
2520 */
Christoph Hellwig25b29952019-06-13 22:50:49 +02002521 return is_device_private_page(page);
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002522 }
2523
Jérôme Glissedf6ad692017-09-08 16:12:24 -07002524 /* For file back page */
2525 if (page_mapping(page))
2526 extra += 1 + page_has_private(page);
2527
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002528 if ((page_count(page) - extra) > page_mapcount(page))
2529 return false;
2530
2531 return true;
2532}
2533
2534/*
2535 * migrate_vma_prepare() - lock pages and isolate them from the lru
2536 * @migrate: migrate struct containing all migration information
2537 *
2538 * This locks pages that have been collected by migrate_vma_collect(). Once each
2539 * page is locked it is isolated from the lru (for non-device pages). Finally,
2540 * the ref taken by migrate_vma_collect() is dropped, as locked pages cannot be
2541 * migrated by concurrent kernel threads.
2542 */
2543static void migrate_vma_prepare(struct migrate_vma *migrate)
2544{
2545 const unsigned long npages = migrate->npages;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002546 const unsigned long start = migrate->start;
2547 unsigned long addr, i, restore = 0;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002548 bool allow_drain = true;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002549
2550 lru_add_drain();
2551
2552 for (i = 0; (i < npages) && migrate->cpages; i++) {
2553 struct page *page = migrate_pfn_to_page(migrate->src[i]);
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002554 bool remap = true;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002555
2556 if (!page)
2557 continue;
2558
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002559 if (!(migrate->src[i] & MIGRATE_PFN_LOCKED)) {
2560 /*
2561 * Because we are migrating several pages there can be
2562 * a deadlock between 2 concurrent migration where each
2563 * are waiting on each other page lock.
2564 *
2565 * Make migrate_vma() a best effort thing and backoff
2566 * for any page we can not lock right away.
2567 */
2568 if (!trylock_page(page)) {
2569 migrate->src[i] = 0;
2570 migrate->cpages--;
2571 put_page(page);
2572 continue;
2573 }
2574 remap = false;
2575 migrate->src[i] |= MIGRATE_PFN_LOCKED;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002576 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002577
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002578 /* ZONE_DEVICE pages are not on LRU */
2579 if (!is_zone_device_page(page)) {
2580 if (!PageLRU(page) && allow_drain) {
2581 /* Drain CPU's pagevec */
2582 lru_add_drain_all();
2583 allow_drain = false;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002584 }
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002585
2586 if (isolate_lru_page(page)) {
2587 if (remap) {
2588 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2589 migrate->cpages--;
2590 restore++;
2591 } else {
2592 migrate->src[i] = 0;
2593 unlock_page(page);
2594 migrate->cpages--;
2595 put_page(page);
2596 }
2597 continue;
2598 }
2599
2600 /* Drop the reference we took in collect */
2601 put_page(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002602 }
2603
2604 if (!migrate_vma_check_page(page)) {
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002605 if (remap) {
2606 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2607 migrate->cpages--;
2608 restore++;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002609
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002610 if (!is_zone_device_page(page)) {
2611 get_page(page);
2612 putback_lru_page(page);
2613 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002614 } else {
2615 migrate->src[i] = 0;
2616 unlock_page(page);
2617 migrate->cpages--;
2618
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002619 if (!is_zone_device_page(page))
2620 putback_lru_page(page);
2621 else
2622 put_page(page);
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002623 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002624 }
2625 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002626
2627 for (i = 0, addr = start; i < npages && restore; i++, addr += PAGE_SIZE) {
2628 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2629
2630 if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE))
2631 continue;
2632
2633 remove_migration_pte(page, migrate->vma, addr, page);
2634
2635 migrate->src[i] = 0;
2636 unlock_page(page);
2637 put_page(page);
2638 restore--;
2639 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002640}
2641
2642/*
2643 * migrate_vma_unmap() - replace page mapping with special migration pte entry
2644 * @migrate: migrate struct containing all migration information
2645 *
2646 * Replace page mapping (CPU page table pte) with a special migration pte entry
2647 * and check again if it has been pinned. Pinned pages are restored because we
2648 * cannot migrate them.
2649 *
2650 * This is the last step before we call the device driver callback to allocate
2651 * destination memory and copy contents of original page over to new page.
2652 */
2653static void migrate_vma_unmap(struct migrate_vma *migrate)
2654{
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002655 const unsigned long npages = migrate->npages;
2656 const unsigned long start = migrate->start;
2657 unsigned long addr, i, restore = 0;
2658
2659 for (i = 0; i < npages; i++) {
2660 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2661
2662 if (!page || !(migrate->src[i] & MIGRATE_PFN_MIGRATE))
2663 continue;
2664
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002665 if (page_mapped(page)) {
Alistair Popplea98a2f02021-06-30 18:54:16 -07002666 try_to_migrate(page, 0);
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002667 if (page_mapped(page))
2668 goto restore;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002669 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002670
2671 if (migrate_vma_check_page(page))
2672 continue;
2673
2674restore:
2675 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2676 migrate->cpages--;
2677 restore++;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002678 }
2679
2680 for (addr = start, i = 0; i < npages && restore; addr += PAGE_SIZE, i++) {
2681 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2682
2683 if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE))
2684 continue;
2685
2686 remove_migration_ptes(page, page, false);
2687
2688 migrate->src[i] = 0;
2689 unlock_page(page);
2690 restore--;
2691
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002692 if (is_zone_device_page(page))
2693 put_page(page);
2694 else
2695 putback_lru_page(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002696 }
2697}
2698
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002699/**
2700 * migrate_vma_setup() - prepare to migrate a range of memory
Randy Dunlapeaf444d2020-08-11 18:33:08 -07002701 * @args: contains the vma, start, and pfns arrays for the migration
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002702 *
2703 * Returns: negative errno on failures, 0 when 0 or more pages were migrated
2704 * without an error.
2705 *
2706 * Prepare to migrate a range of memory virtual address range by collecting all
2707 * the pages backing each virtual address in the range, saving them inside the
2708 * src array. Then lock those pages and unmap them. Once the pages are locked
2709 * and unmapped, check whether each page is pinned or not. Pages that aren't
2710 * pinned have the MIGRATE_PFN_MIGRATE flag set (by this function) in the
2711 * corresponding src array entry. Then restores any pages that are pinned, by
2712 * remapping and unlocking those pages.
2713 *
2714 * The caller should then allocate destination memory and copy source memory to
2715 * it for all those entries (ie with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE
2716 * flag set). Once these are allocated and copied, the caller must update each
2717 * corresponding entry in the dst array with the pfn value of the destination
2718 * page and with the MIGRATE_PFN_VALID and MIGRATE_PFN_LOCKED flags set
2719 * (destination pages must have their struct pages locked, via lock_page()).
2720 *
2721 * Note that the caller does not have to migrate all the pages that are marked
2722 * with MIGRATE_PFN_MIGRATE flag in src array unless this is a migration from
2723 * device memory to system memory. If the caller cannot migrate a device page
2724 * back to system memory, then it must return VM_FAULT_SIGBUS, which has severe
2725 * consequences for the userspace process, so it must be avoided if at all
2726 * possible.
2727 *
2728 * For empty entries inside CPU page table (pte_none() or pmd_none() is true) we
2729 * do set MIGRATE_PFN_MIGRATE flag inside the corresponding source array thus
Ingo Molnarf0953a12021-05-06 18:06:47 -07002730 * allowing the caller to allocate device memory for those unbacked virtual
2731 * addresses. For this the caller simply has to allocate device memory and
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002732 * properly set the destination entry like for regular migration. Note that
Ingo Molnarf0953a12021-05-06 18:06:47 -07002733 * this can still fail, and thus inside the device driver you must check if the
2734 * migration was successful for those entries after calling migrate_vma_pages(),
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002735 * just like for regular migration.
2736 *
2737 * After that, the callers must call migrate_vma_pages() to go over each entry
2738 * in the src array that has the MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag
2739 * set. If the corresponding entry in dst array has MIGRATE_PFN_VALID flag set,
2740 * then migrate_vma_pages() to migrate struct page information from the source
2741 * struct page to the destination struct page. If it fails to migrate the
2742 * struct page information, then it clears the MIGRATE_PFN_MIGRATE flag in the
2743 * src array.
2744 *
2745 * At this point all successfully migrated pages have an entry in the src
2746 * array with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag set and the dst
2747 * array entry with MIGRATE_PFN_VALID flag set.
2748 *
2749 * Once migrate_vma_pages() returns the caller may inspect which pages were
2750 * successfully migrated, and which were not. Successfully migrated pages will
2751 * have the MIGRATE_PFN_MIGRATE flag set for their src array entry.
2752 *
2753 * It is safe to update device page table after migrate_vma_pages() because
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002754 * both destination and source page are still locked, and the mmap_lock is held
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002755 * in read mode (hence no one can unmap the range being migrated).
2756 *
2757 * Once the caller is done cleaning up things and updating its page table (if it
2758 * chose to do so, this is not an obligation) it finally calls
2759 * migrate_vma_finalize() to update the CPU page table to point to new pages
2760 * for successfully migrated pages or otherwise restore the CPU page table to
2761 * point to the original source pages.
2762 */
2763int migrate_vma_setup(struct migrate_vma *args)
2764{
2765 long nr_pages = (args->end - args->start) >> PAGE_SHIFT;
2766
2767 args->start &= PAGE_MASK;
2768 args->end &= PAGE_MASK;
2769 if (!args->vma || is_vm_hugetlb_page(args->vma) ||
2770 (args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma))
2771 return -EINVAL;
2772 if (nr_pages <= 0)
2773 return -EINVAL;
2774 if (args->start < args->vma->vm_start ||
2775 args->start >= args->vma->vm_end)
2776 return -EINVAL;
2777 if (args->end <= args->vma->vm_start || args->end > args->vma->vm_end)
2778 return -EINVAL;
2779 if (!args->src || !args->dst)
2780 return -EINVAL;
2781
2782 memset(args->src, 0, sizeof(*args->src) * nr_pages);
2783 args->cpages = 0;
2784 args->npages = 0;
2785
2786 migrate_vma_collect(args);
2787
2788 if (args->cpages)
2789 migrate_vma_prepare(args);
2790 if (args->cpages)
2791 migrate_vma_unmap(args);
2792
2793 /*
2794 * At this point pages are locked and unmapped, and thus they have
2795 * stable content and can safely be copied to destination memory that
2796 * is allocated by the drivers.
2797 */
2798 return 0;
2799
2800}
2801EXPORT_SYMBOL(migrate_vma_setup);
2802
Ralph Campbell34290e22020-01-30 22:14:44 -08002803/*
2804 * This code closely matches the code in:
2805 * __handle_mm_fault()
2806 * handle_pte_fault()
2807 * do_anonymous_page()
2808 * to map in an anonymous zero page but the struct page will be a ZONE_DEVICE
2809 * private page.
2810 */
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002811static void migrate_vma_insert_page(struct migrate_vma *migrate,
2812 unsigned long addr,
2813 struct page *page,
Stephen Zhangd85c6db2020-12-14 19:13:20 -08002814 unsigned long *src)
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002815{
2816 struct vm_area_struct *vma = migrate->vma;
2817 struct mm_struct *mm = vma->vm_mm;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002818 bool flush = false;
2819 spinlock_t *ptl;
2820 pte_t entry;
2821 pgd_t *pgdp;
2822 p4d_t *p4dp;
2823 pud_t *pudp;
2824 pmd_t *pmdp;
2825 pte_t *ptep;
2826
2827 /* Only allow populating anonymous memory */
2828 if (!vma_is_anonymous(vma))
2829 goto abort;
2830
2831 pgdp = pgd_offset(mm, addr);
2832 p4dp = p4d_alloc(mm, pgdp, addr);
2833 if (!p4dp)
2834 goto abort;
2835 pudp = pud_alloc(mm, p4dp, addr);
2836 if (!pudp)
2837 goto abort;
2838 pmdp = pmd_alloc(mm, pudp, addr);
2839 if (!pmdp)
2840 goto abort;
2841
2842 if (pmd_trans_huge(*pmdp) || pmd_devmap(*pmdp))
2843 goto abort;
2844
2845 /*
2846 * Use pte_alloc() instead of pte_alloc_map(). We can't run
2847 * pte_offset_map() on pmds where a huge pmd might be created
2848 * from a different thread.
2849 *
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002850 * pte_alloc_map() is safe to use under mmap_write_lock(mm) or when
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002851 * parallel threads are excluded by other means.
2852 *
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002853 * Here we only have mmap_read_lock(mm).
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002854 */
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -08002855 if (pte_alloc(mm, pmdp))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002856 goto abort;
2857
2858 /* See the comment in pte_alloc_one_map() */
2859 if (unlikely(pmd_trans_unstable(pmdp)))
2860 goto abort;
2861
2862 if (unlikely(anon_vma_prepare(vma)))
2863 goto abort;
Johannes Weinerd9eb1ea2020-06-03 16:02:24 -07002864 if (mem_cgroup_charge(page, vma->vm_mm, GFP_KERNEL))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002865 goto abort;
2866
2867 /*
2868 * The memory barrier inside __SetPageUptodate makes sure that
2869 * preceding stores to the page contents become visible before
2870 * the set_pte_at() write.
2871 */
2872 __SetPageUptodate(page);
2873
Jérôme Glissedf6ad692017-09-08 16:12:24 -07002874 if (is_zone_device_page(page)) {
2875 if (is_device_private_page(page)) {
2876 swp_entry_t swp_entry;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002877
Alistair Popple4dd845b2021-06-30 18:54:09 -07002878 if (vma->vm_flags & VM_WRITE)
2879 swp_entry = make_writable_device_private_entry(
2880 page_to_pfn(page));
2881 else
2882 swp_entry = make_readable_device_private_entry(
2883 page_to_pfn(page));
Jérôme Glissedf6ad692017-09-08 16:12:24 -07002884 entry = swp_entry_to_pte(swp_entry);
Miaohe Lin34f5e9b2021-05-04 18:37:10 -07002885 } else {
2886 /*
2887 * For now we only support migrating to un-addressable
2888 * device memory.
2889 */
2890 pr_warn_once("Unsupported ZONE_DEVICE page type.\n");
2891 goto abort;
Jérôme Glissedf6ad692017-09-08 16:12:24 -07002892 }
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002893 } else {
2894 entry = mk_pte(page, vma->vm_page_prot);
2895 if (vma->vm_flags & VM_WRITE)
2896 entry = pte_mkwrite(pte_mkdirty(entry));
2897 }
2898
2899 ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
2900
Ralph Campbell34290e22020-01-30 22:14:44 -08002901 if (check_stable_address_space(mm))
2902 goto unlock_abort;
2903
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002904 if (pte_present(*ptep)) {
2905 unsigned long pfn = pte_pfn(*ptep);
2906
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002907 if (!is_zero_pfn(pfn))
2908 goto unlock_abort;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002909 flush = true;
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002910 } else if (!pte_none(*ptep))
2911 goto unlock_abort;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002912
2913 /*
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002914 * Check for userfaultfd but do not deliver the fault. Instead,
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002915 * just back off.
2916 */
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002917 if (userfaultfd_missing(vma))
2918 goto unlock_abort;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002919
2920 inc_mm_counter(mm, MM_ANONPAGES);
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07002921 page_add_new_anon_rmap(page, vma, addr, false);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002922 if (!is_zone_device_page(page))
Joonsoo Kimb5181542020-08-11 18:30:40 -07002923 lru_cache_add_inactive_or_unevictable(page, vma);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002924 get_page(page);
2925
2926 if (flush) {
2927 flush_cache_page(vma, addr, pte_pfn(*ptep));
2928 ptep_clear_flush_notify(vma, addr, ptep);
2929 set_pte_at_notify(mm, addr, ptep, entry);
2930 update_mmu_cache(vma, addr, ptep);
2931 } else {
2932 /* No need to invalidate - it was non-present before */
2933 set_pte_at(mm, addr, ptep, entry);
2934 update_mmu_cache(vma, addr, ptep);
2935 }
2936
2937 pte_unmap_unlock(ptep, ptl);
2938 *src = MIGRATE_PFN_MIGRATE;
2939 return;
2940
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002941unlock_abort:
2942 pte_unmap_unlock(ptep, ptl);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002943abort:
2944 *src &= ~MIGRATE_PFN_MIGRATE;
2945}
2946
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002947/**
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002948 * migrate_vma_pages() - migrate meta-data from src page to dst page
2949 * @migrate: migrate struct containing all migration information
2950 *
2951 * This migrates struct page meta-data from source struct page to destination
2952 * struct page. This effectively finishes the migration from source page to the
2953 * destination page.
2954 */
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002955void migrate_vma_pages(struct migrate_vma *migrate)
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002956{
2957 const unsigned long npages = migrate->npages;
2958 const unsigned long start = migrate->start;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002959 struct mmu_notifier_range range;
2960 unsigned long addr, i;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002961 bool notified = false;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002962
2963 for (i = 0, addr = start; i < npages; addr += PAGE_SIZE, i++) {
2964 struct page *newpage = migrate_pfn_to_page(migrate->dst[i]);
2965 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2966 struct address_space *mapping;
2967 int r;
2968
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002969 if (!newpage) {
2970 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002971 continue;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002972 }
2973
2974 if (!page) {
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002975 if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002976 continue;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002977 if (!notified) {
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002978 notified = true;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002979
Alistair Popple6b49bf62021-06-30 18:54:19 -07002980 mmu_notifier_range_init_owner(&range,
2981 MMU_NOTIFY_MIGRATE, 0, migrate->vma,
2982 migrate->vma->vm_mm, addr, migrate->end,
Ralph Campbell5e5dda82020-12-14 19:12:55 -08002983 migrate->pgmap_owner);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002984 mmu_notifier_invalidate_range_start(&range);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002985 }
2986 migrate_vma_insert_page(migrate, addr, newpage,
Stephen Zhangd85c6db2020-12-14 19:13:20 -08002987 &migrate->src[i]);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002988 continue;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002989 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002990
2991 mapping = page_mapping(page);
2992
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002993 if (is_zone_device_page(newpage)) {
2994 if (is_device_private_page(newpage)) {
2995 /*
2996 * For now only support private anonymous when
2997 * migrating to un-addressable device memory.
2998 */
2999 if (mapping) {
3000 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
3001 continue;
3002 }
Christoph Hellwig25b29952019-06-13 22:50:49 +02003003 } else {
Jérôme Glissea5430dd2017-09-08 16:12:17 -07003004 /*
3005 * Other types of ZONE_DEVICE page are not
3006 * supported.
3007 */
3008 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
3009 continue;
3010 }
3011 }
3012
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003013 r = migrate_page(mapping, newpage, page, MIGRATE_SYNC_NO_COPY);
3014 if (r != MIGRATEPAGE_SUCCESS)
3015 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
3016 }
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003017
Jérôme Glisse4645b9f2017-11-15 17:34:11 -08003018 /*
3019 * No need to double call mmu_notifier->invalidate_range() callback as
3020 * the above ptep_clear_flush_notify() inside migrate_vma_insert_page()
3021 * did already call it.
3022 */
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003023 if (notified)
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003024 mmu_notifier_invalidate_range_only_end(&range);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003025}
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003026EXPORT_SYMBOL(migrate_vma_pages);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003027
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003028/**
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003029 * migrate_vma_finalize() - restore CPU page table entry
3030 * @migrate: migrate struct containing all migration information
3031 *
3032 * This replaces the special migration pte entry with either a mapping to the
3033 * new page if migration was successful for that page, or to the original page
3034 * otherwise.
3035 *
3036 * This also unlocks the pages and puts them back on the lru, or drops the extra
3037 * refcount, for device pages.
3038 */
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003039void migrate_vma_finalize(struct migrate_vma *migrate)
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003040{
3041 const unsigned long npages = migrate->npages;
3042 unsigned long i;
3043
3044 for (i = 0; i < npages; i++) {
3045 struct page *newpage = migrate_pfn_to_page(migrate->dst[i]);
3046 struct page *page = migrate_pfn_to_page(migrate->src[i]);
3047
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003048 if (!page) {
3049 if (newpage) {
3050 unlock_page(newpage);
3051 put_page(newpage);
3052 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003053 continue;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003054 }
3055
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003056 if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE) || !newpage) {
3057 if (newpage) {
3058 unlock_page(newpage);
3059 put_page(newpage);
3060 }
3061 newpage = page;
3062 }
3063
3064 remove_migration_ptes(page, newpage, false);
3065 unlock_page(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003066
Jérôme Glissea5430dd2017-09-08 16:12:17 -07003067 if (is_zone_device_page(page))
3068 put_page(page);
3069 else
3070 putback_lru_page(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003071
3072 if (newpage != page) {
3073 unlock_page(newpage);
Jérôme Glissea5430dd2017-09-08 16:12:17 -07003074 if (is_zone_device_page(newpage))
3075 put_page(newpage);
3076 else
3077 putback_lru_page(newpage);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003078 }
3079 }
3080}
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003081EXPORT_SYMBOL(migrate_vma_finalize);
Christoph Hellwig9b2ed9c2019-08-14 09:59:28 +02003082#endif /* CONFIG_DEVICE_PRIVATE */
Dave Hansen79c28a42021-09-02 14:59:06 -07003083
Dave Hansen76af6a02021-10-18 15:15:32 -07003084#if defined(CONFIG_HOTPLUG_CPU)
Dave Hansen79c28a42021-09-02 14:59:06 -07003085/* Disable reclaim-based migration. */
3086static void __disable_all_migrate_targets(void)
3087{
3088 int node;
3089
3090 for_each_online_node(node)
3091 node_demotion[node] = NUMA_NO_NODE;
3092}
3093
3094static void disable_all_migrate_targets(void)
3095{
3096 __disable_all_migrate_targets();
3097
3098 /*
3099 * Ensure that the "disable" is visible across the system.
3100 * Readers will see either a combination of before+disable
3101 * state or disable+after. They will never see before and
3102 * after state together.
3103 *
3104 * The before+after state together might have cycles and
3105 * could cause readers to do things like loop until this
3106 * function finishes. This ensures they can only see a
3107 * single "bad" read and would, for instance, only loop
3108 * once.
3109 */
3110 synchronize_rcu();
3111}
3112
3113/*
3114 * Find an automatic demotion target for 'node'.
3115 * Failing here is OK. It might just indicate
3116 * being at the end of a chain.
3117 */
3118static int establish_migrate_target(int node, nodemask_t *used)
3119{
3120 int migration_target;
3121
3122 /*
3123 * Can not set a migration target on a
3124 * node with it already set.
3125 *
3126 * No need for READ_ONCE() here since this
3127 * in the write path for node_demotion[].
3128 * This should be the only thread writing.
3129 */
3130 if (node_demotion[node] != NUMA_NO_NODE)
3131 return NUMA_NO_NODE;
3132
3133 migration_target = find_next_best_node(node, used);
3134 if (migration_target == NUMA_NO_NODE)
3135 return NUMA_NO_NODE;
3136
3137 node_demotion[node] = migration_target;
3138
3139 return migration_target;
3140}
3141
3142/*
3143 * When memory fills up on a node, memory contents can be
3144 * automatically migrated to another node instead of
3145 * discarded at reclaim.
3146 *
3147 * Establish a "migration path" which will start at nodes
3148 * with CPUs and will follow the priorities used to build the
3149 * page allocator zonelists.
3150 *
3151 * The difference here is that cycles must be avoided. If
3152 * node0 migrates to node1, then neither node1, nor anything
3153 * node1 migrates to can migrate to node0.
3154 *
3155 * This function can run simultaneously with readers of
3156 * node_demotion[]. However, it can not run simultaneously
3157 * with itself. Exclusion is provided by memory hotplug events
3158 * being single-threaded.
3159 */
3160static void __set_migration_target_nodes(void)
3161{
3162 nodemask_t next_pass = NODE_MASK_NONE;
3163 nodemask_t this_pass = NODE_MASK_NONE;
3164 nodemask_t used_targets = NODE_MASK_NONE;
3165 int node;
3166
3167 /*
3168 * Avoid any oddities like cycles that could occur
3169 * from changes in the topology. This will leave
3170 * a momentary gap when migration is disabled.
3171 */
3172 disable_all_migrate_targets();
3173
3174 /*
3175 * Allocations go close to CPUs, first. Assume that
3176 * the migration path starts at the nodes with CPUs.
3177 */
3178 next_pass = node_states[N_CPU];
3179again:
3180 this_pass = next_pass;
3181 next_pass = NODE_MASK_NONE;
3182 /*
3183 * To avoid cycles in the migration "graph", ensure
3184 * that migration sources are not future targets by
3185 * setting them in 'used_targets'. Do this only
3186 * once per pass so that multiple source nodes can
3187 * share a target node.
3188 *
3189 * 'used_targets' will become unavailable in future
3190 * passes. This limits some opportunities for
3191 * multiple source nodes to share a destination.
3192 */
3193 nodes_or(used_targets, used_targets, this_pass);
3194 for_each_node_mask(node, this_pass) {
3195 int target_node = establish_migrate_target(node, &used_targets);
3196
3197 if (target_node == NUMA_NO_NODE)
3198 continue;
3199
3200 /*
3201 * Visit targets from this pass in the next pass.
3202 * Eventually, every node will have been part of
3203 * a pass, and will become set in 'used_targets'.
3204 */
3205 node_set(target_node, next_pass);
3206 }
3207 /*
3208 * 'next_pass' contains nodes which became migration
3209 * targets in this pass. Make additional passes until
3210 * no more migrations targets are available.
3211 */
3212 if (!nodes_empty(next_pass))
3213 goto again;
3214}
3215
3216/*
3217 * For callers that do not hold get_online_mems() already.
3218 */
Dave Hansen79c28a42021-09-02 14:59:06 -07003219static void set_migration_target_nodes(void)
3220{
3221 get_online_mems();
3222 __set_migration_target_nodes();
3223 put_online_mems();
3224}
Dave Hansen884a6e52021-09-02 14:59:09 -07003225
3226/*
Dave Hansen884a6e52021-09-02 14:59:09 -07003227 * This leaves migrate-on-reclaim transiently disabled between
3228 * the MEM_GOING_OFFLINE and MEM_OFFLINE events. This runs
3229 * whether reclaim-based migration is enabled or not, which
3230 * ensures that the user can turn reclaim-based migration at
3231 * any time without needing to recalculate migration targets.
3232 *
3233 * These callbacks already hold get_online_mems(). That is why
3234 * __set_migration_target_nodes() can be used as opposed to
3235 * set_migration_target_nodes().
3236 */
3237static int __meminit migrate_on_reclaim_callback(struct notifier_block *self,
Dave Hansen295be912021-10-18 15:15:29 -07003238 unsigned long action, void *_arg)
Dave Hansen884a6e52021-09-02 14:59:09 -07003239{
Dave Hansen295be912021-10-18 15:15:29 -07003240 struct memory_notify *arg = _arg;
3241
3242 /*
3243 * Only update the node migration order when a node is
3244 * changing status, like online->offline. This avoids
3245 * the overhead of synchronize_rcu() in most cases.
3246 */
3247 if (arg->status_change_nid < 0)
3248 return notifier_from_errno(0);
3249
Dave Hansen884a6e52021-09-02 14:59:09 -07003250 switch (action) {
3251 case MEM_GOING_OFFLINE:
3252 /*
3253 * Make sure there are not transient states where
3254 * an offline node is a migration target. This
3255 * will leave migration disabled until the offline
3256 * completes and the MEM_OFFLINE case below runs.
3257 */
3258 disable_all_migrate_targets();
3259 break;
3260 case MEM_OFFLINE:
3261 case MEM_ONLINE:
3262 /*
3263 * Recalculate the target nodes once the node
3264 * reaches its final state (online or offline).
3265 */
3266 __set_migration_target_nodes();
3267 break;
3268 case MEM_CANCEL_OFFLINE:
3269 /*
3270 * MEM_GOING_OFFLINE disabled all the migration
3271 * targets. Reenable them.
3272 */
3273 __set_migration_target_nodes();
3274 break;
3275 case MEM_GOING_ONLINE:
3276 case MEM_CANCEL_ONLINE:
3277 break;
3278 }
3279
3280 return notifier_from_errno(0);
3281}
3282
Dave Hansen76af6a02021-10-18 15:15:32 -07003283/*
3284 * React to hotplug events that might affect the migration targets
3285 * like events that online or offline NUMA nodes.
3286 *
3287 * The ordering is also currently dependent on which nodes have
3288 * CPUs. That means we need CPU on/offline notification too.
3289 */
3290static int migration_online_cpu(unsigned int cpu)
3291{
3292 set_migration_target_nodes();
3293 return 0;
3294}
3295
3296static int migration_offline_cpu(unsigned int cpu)
3297{
3298 set_migration_target_nodes();
3299 return 0;
3300}
3301
Dave Hansen884a6e52021-09-02 14:59:09 -07003302static int __init migrate_on_reclaim_init(void)
3303{
3304 int ret;
3305
Huang Yinga6a02512021-10-18 15:15:35 -07003306 ret = cpuhp_setup_state_nocalls(CPUHP_MM_DEMOTION_DEAD, "mm/demotion:offline",
3307 NULL, migration_offline_cpu);
Dave Hansen884a6e52021-09-02 14:59:09 -07003308 /*
3309 * In the unlikely case that this fails, the automatic
3310 * migration targets may become suboptimal for nodes
3311 * where N_CPU changes. With such a small impact in a
3312 * rare case, do not bother trying to do anything special.
3313 */
3314 WARN_ON(ret < 0);
Huang Yinga6a02512021-10-18 15:15:35 -07003315 ret = cpuhp_setup_state(CPUHP_AP_MM_DEMOTION_ONLINE, "mm/demotion:online",
3316 migration_online_cpu, NULL);
3317 WARN_ON(ret < 0);
Dave Hansen884a6e52021-09-02 14:59:09 -07003318
3319 hotplug_memory_notifier(migrate_on_reclaim_callback, 100);
3320 return 0;
3321}
3322late_initcall(migrate_on_reclaim_init);
Dave Hansen76af6a02021-10-18 15:15:32 -07003323#endif /* CONFIG_HOTPLUG_CPU */