blob: 9b14fe8603bb6b3dd656368a246e841e5cd25a7a [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>
Tao Zeng54f845e2022-01-26 16:04:08 +080053#ifdef CONFIG_AMLOGIC_CMA
54#include <linux/amlogic/aml_cma.h>
55#include <linux/delay.h>
56#endif
Jianxiong Pan07622f02022-09-22 17:25:35 +080057#ifdef CONFIG_AMLOGIC_PIN_LOCKED_FILE
58#include <linux/amlogic/pin_file.h>
59#endif
Christoph Lameterb20a3502006-03-22 00:09:12 -080060
Michal Nazarewicz0d1836c2010-12-21 17:24:26 -080061#include <asm/tlbflush.h>
62
Mel Gorman7b2a2d42012-10-19 14:07:31 +010063#define CREATE_TRACE_POINTS
64#include <trace/events/migrate.h>
65
qinglin.lia1a623f2024-06-21 14:36:34 +080066#undef CREATE_TRACE_POINTS
67#include <trace/hooks/mm.h>
68
Christoph Lameterb20a3502006-03-22 00:09:12 -080069#include "internal.h"
70
Yisheng Xie9e5bcd62017-02-24 14:57:29 -080071int isolate_movable_page(struct page *page, isolate_mode_t mode)
Minchan Kimbda807d2016-07-26 15:23:05 -070072{
73 struct address_space *mapping;
74
75 /*
76 * Avoid burning cycles with pages that are yet under __free_pages(),
77 * or just got freed under us.
78 *
79 * In case we 'win' a race for a movable page being freed under us and
80 * raise its refcount preventing __free_pages() from doing its job
81 * the put_page() at the end of this block will take care of
82 * release this page, thus avoiding a nasty leakage.
83 */
84 if (unlikely(!get_page_unless_zero(page)))
85 goto out;
86
87 /*
88 * Check PageMovable before holding a PG_lock because page's owner
89 * assumes anybody doesn't touch PG_lock of newly allocated page
Wei Yang8bb4e7a2019-03-05 15:46:22 -080090 * so unconditionally grabbing the lock ruins page's owner side.
Minchan Kimbda807d2016-07-26 15:23:05 -070091 */
92 if (unlikely(!__PageMovable(page)))
93 goto out_putpage;
94 /*
95 * As movable pages are not isolated from LRU lists, concurrent
96 * compaction threads can race against page migration functions
97 * as well as race against the releasing a page.
98 *
99 * In order to avoid having an already isolated movable page
100 * being (wrongly) re-isolated while it is under migration,
101 * or to avoid attempting to isolate pages being released,
102 * lets be sure we have the page lock
103 * before proceeding with the movable page isolation steps.
104 */
105 if (unlikely(!trylock_page(page)))
106 goto out_putpage;
107
108 if (!PageMovable(page) || PageIsolated(page))
109 goto out_no_isolated;
110
111 mapping = page_mapping(page);
112 VM_BUG_ON_PAGE(!mapping, page);
113
114 if (!mapping->a_ops->isolate_page(page, mode))
115 goto out_no_isolated;
116
117 /* Driver shouldn't use PG_isolated bit of page->flags */
118 WARN_ON_ONCE(PageIsolated(page));
andrew.yang0d8a8362022-03-15 16:58:34 +1100119 SetPageIsolated(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700120 unlock_page(page);
121
Yisheng Xie9e5bcd62017-02-24 14:57:29 -0800122 return 0;
Minchan Kimbda807d2016-07-26 15:23:05 -0700123
124out_no_isolated:
125 unlock_page(page);
126out_putpage:
127 put_page(page);
128out:
Yisheng Xie9e5bcd62017-02-24 14:57:29 -0800129 return -EBUSY;
Minchan Kimbda807d2016-07-26 15:23:05 -0700130}
131
Miaohe Lin606a6f72021-05-04 18:37:04 -0700132static void putback_movable_page(struct page *page)
Minchan Kimbda807d2016-07-26 15:23:05 -0700133{
134 struct address_space *mapping;
135
Minchan Kimbda807d2016-07-26 15:23:05 -0700136 mapping = page_mapping(page);
137 mapping->a_ops->putback_page(page);
andrew.yang0d8a8362022-03-15 16:58:34 +1100138 ClearPageIsolated(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700139}
140
Christoph Lameterb20a3502006-03-22 00:09:12 -0800141/*
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800142 * Put previously isolated pages back onto the appropriate lists
143 * from where they were once taken off for compaction/migration.
144 *
Joonsoo Kim59c82b72014-01-21 15:51:17 -0800145 * This function shall be used whenever the isolated pageset has been
146 * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range()
Miaohe Lin072e7412022-05-30 19:30:15 +0800147 * and isolate_hugetlb().
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800148 */
149void putback_movable_pages(struct list_head *l)
150{
151 struct page *page;
152 struct page *page2;
153
154 list_for_each_entry_safe(page, page2, l, lru) {
Naoya Horiguchi31caf662013-09-11 14:21:59 -0700155 if (unlikely(PageHuge(page))) {
156 putback_active_hugepage(page);
157 continue;
158 }
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800159 list_del(&page->lru);
Minchan Kimbda807d2016-07-26 15:23:05 -0700160 /*
161 * We isolated non-lru movable page so here we can use
162 * __PageMovable because LRU page's mapping cannot have
163 * PAGE_MAPPING_MOVABLE.
164 */
Minchan Kimb1123ea62016-07-26 15:23:09 -0700165 if (unlikely(__PageMovable(page))) {
Minchan Kimbda807d2016-07-26 15:23:05 -0700166 VM_BUG_ON_PAGE(!PageIsolated(page), page);
167 lock_page(page);
168 if (PageMovable(page))
169 putback_movable_page(page);
170 else
andrew.yang0d8a8362022-03-15 16:58:34 +1100171 ClearPageIsolated(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700172 unlock_page(page);
173 put_page(page);
174 } else {
Naoya Horiguchie8db67e2017-09-08 16:11:12 -0700175 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -0700176 page_is_file_lru(page), -thp_nr_pages(page));
Rabin Vincentfc280fe2017-04-20 14:37:46 -0700177 putback_lru_page(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700178 }
Christoph Lameterb20a3502006-03-22 00:09:12 -0800179 }
Christoph Lameterb20a3502006-03-22 00:09:12 -0800180}
Charan Teja Reddyf47b8522021-02-16 13:59:45 +0530181EXPORT_SYMBOL_GPL(putback_movable_pages);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800182
Christoph Lameter06972122006-06-23 02:03:35 -0700183/*
184 * Restore a potential migration pte to a working pte entry
185 */
Minchan Kime4b82222017-05-03 14:54:27 -0700186static bool remove_migration_pte(struct page *page, struct vm_area_struct *vma,
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800187 unsigned long addr, void *old)
Christoph Lameter06972122006-06-23 02:03:35 -0700188{
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800189 struct page_vma_mapped_walk pvmw = {
190 .page = old,
191 .vma = vma,
192 .address = addr,
193 .flags = PVMW_SYNC | PVMW_MIGRATION,
194 };
195 struct page *new;
196 pte_t pte;
Christoph Lameter06972122006-06-23 02:03:35 -0700197 swp_entry_t entry;
Christoph Lameter06972122006-06-23 02:03:35 -0700198
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800199 VM_BUG_ON_PAGE(PageTail(page), page);
200 while (page_vma_mapped_walk(&pvmw)) {
Naoya Horiguchi4b0ece62017-03-31 15:11:44 -0700201 if (PageKsm(page))
202 new = page;
203 else
204 new = page - pvmw.page->index +
205 linear_page_index(vma, pvmw.address);
Christoph Lameter06972122006-06-23 02:03:35 -0700206
Zi Yan616b8372017-09-08 16:10:57 -0700207#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
208 /* PMD-mapped THP migration entry */
209 if (!pvmw.pte) {
210 VM_BUG_ON_PAGE(PageHuge(page) || !PageTransCompound(page), page);
211 remove_migration_pmd(&pvmw, new);
212 continue;
213 }
214#endif
215
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800216 get_page(new);
217 pte = pte_mkold(mk_pte(new, READ_ONCE(vma->vm_page_prot)));
218 if (pte_swp_soft_dirty(*pvmw.pte))
219 pte = pte_mksoft_dirty(pte);
Christoph Lameter06972122006-06-23 02:03:35 -0700220
Hugh Dickins486cf462011-10-19 12:50:35 -0700221 /*
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800222 * Recheck VMA as permissions can change since migration started
Hugh Dickins486cf462011-10-19 12:50:35 -0700223 */
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800224 entry = pte_to_swp_entry(*pvmw.pte);
Alistair Popple4dd845b2021-06-30 18:54:09 -0700225 if (is_writable_migration_entry(entry))
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800226 pte = maybe_mkwrite(pte, vma);
Peter Xuf45ec5f2020-04-06 20:06:01 -0700227 else if (pte_swp_uffd_wp(*pvmw.pte))
228 pte = pte_mkuffd_wp(pte);
Mel Gormand3cb8bf2014-10-02 19:47:41 +0100229
Ralph Campbell61287632020-09-04 16:36:04 -0700230 if (unlikely(is_device_private_page(new))) {
Alistair Popple4dd845b2021-06-30 18:54:09 -0700231 if (pte_write(pte))
232 entry = make_writable_device_private_entry(
233 page_to_pfn(new));
234 else
235 entry = make_readable_device_private_entry(
236 page_to_pfn(new));
Ralph Campbell61287632020-09-04 16:36:04 -0700237 pte = swp_entry_to_pte(entry);
Ralph Campbell3d321bf82020-09-04 16:36:07 -0700238 if (pte_swp_soft_dirty(*pvmw.pte))
239 pte = pte_swp_mksoft_dirty(pte);
Ralph Campbell61287632020-09-04 16:36:04 -0700240 if (pte_swp_uffd_wp(*pvmw.pte))
241 pte = pte_swp_mkuffd_wp(pte);
Lars Perssond2b2c6dd2019-03-28 20:44:28 -0700242 }
Jérôme Glissea5430dd2017-09-08 16:12:17 -0700243
Andi Kleen3ef8fd72010-10-11 16:03:21 +0200244#ifdef CONFIG_HUGETLB_PAGE
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800245 if (PageHuge(new)) {
Christophe Leroy79c1c592021-06-30 18:48:00 -0700246 unsigned int shift = huge_page_shift(hstate_vma(vma));
247
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800248 pte = pte_mkhuge(pte);
Christophe Leroy79c1c592021-06-30 18:48:00 -0700249 pte = arch_make_huge_pte(pte, shift, vma->vm_flags);
Aneesh Kumar K.V383321a2017-07-06 15:38:41 -0700250 set_huge_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800251 if (PageAnon(new))
252 hugepage_add_anon_rmap(new, vma, pvmw.address);
253 else
254 page_dup_rmap(new, true);
Aneesh Kumar K.V383321a2017-07-06 15:38:41 -0700255 } else
256#endif
257 {
258 set_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700259
Aneesh Kumar K.V383321a2017-07-06 15:38:41 -0700260 if (PageAnon(new))
261 page_add_anon_rmap(new, vma, pvmw.address, false);
262 else
263 page_add_file_rmap(new, false);
264 }
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800265 if (vma->vm_flags & VM_LOCKED && !PageTransCompound(new))
266 mlock_vma_page(new);
Hugh Dickins51afb122015-11-05 18:49:37 -0800267
Kirill A. Shutemove125fe42018-10-05 15:51:41 -0700268 if (PageTransHuge(page) && PageMlocked(page))
269 clear_page_mlock(page);
270
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800271 /* No need to invalidate - it was non-present before */
272 update_mmu_cache(vma, pvmw.address, pvmw.pte);
273 }
274
Minchan Kime4b82222017-05-03 14:54:27 -0700275 return true;
Christoph Lameter06972122006-06-23 02:03:35 -0700276}
277
278/*
Christoph Lameter04e62a22006-06-23 02:03:38 -0700279 * Get rid of all migration entries and replace them by
280 * references to the indicated page.
281 */
Kirill A. Shutemove3884662016-03-17 14:20:07 -0700282void remove_migration_ptes(struct page *old, struct page *new, bool locked)
Christoph Lameter04e62a22006-06-23 02:03:38 -0700283{
Joonsoo Kim051ac832014-01-21 15:49:48 -0800284 struct rmap_walk_control rwc = {
285 .rmap_one = remove_migration_pte,
286 .arg = old,
287 };
288
Kirill A. Shutemove3884662016-03-17 14:20:07 -0700289 if (locked)
290 rmap_walk_locked(new, &rwc);
291 else
292 rmap_walk(new, &rwc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700293}
294
295/*
Christoph Lameter06972122006-06-23 02:03:35 -0700296 * Something used the pte of a page under migration. We need to
297 * get to the page and wait until migration is finished.
298 * When we return from this function the fault will be retried.
Christoph Lameter06972122006-06-23 02:03:35 -0700299 */
Naoya Horiguchie66f17f2015-02-11 15:25:22 -0800300void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700301 spinlock_t *ptl)
Christoph Lameter06972122006-06-23 02:03:35 -0700302{
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700303 pte_t pte;
Christoph Lameter06972122006-06-23 02:03:35 -0700304 swp_entry_t entry;
305 struct page *page;
Tao Zeng54f845e2022-01-26 16:04:08 +0800306#ifdef CONFIG_AMLOGIC_CMA
307 bool need_wait = 0;
308#endif
Christoph Lameter06972122006-06-23 02:03:35 -0700309
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700310 spin_lock(ptl);
Christoph Lameter06972122006-06-23 02:03:35 -0700311 pte = *ptep;
312 if (!is_swap_pte(pte))
313 goto out;
314
315 entry = pte_to_swp_entry(pte);
316 if (!is_migration_entry(entry))
317 goto out;
318
Alistair Poppleaf5cdaf2021-06-30 18:54:06 -0700319 page = pfn_swap_entry_to_page(entry);
Tao Zeng54f845e2022-01-26 16:04:08 +0800320#ifdef CONFIG_AMLOGIC_CMA
321 /* This page is under cma allocating, do not increase it ref */
322 if (in_cma_allocating(page)) {
323 pr_debug("%s, Page:%lx, flags:%lx, m:%d, c:%d, map:%p\n",
324 __func__, page_to_pfn(page), page->flags,
325 page_mapcount(page), page_count(page),
326 page->mapping);
327 need_wait = 1;
328 goto out;
329 }
330#endif
Xu Yuffc90cb2021-06-15 18:23:42 -0700331 page = compound_head(page);
Christoph Lameter06972122006-06-23 02:03:35 -0700332
Nick Piggine2867812008-07-25 19:45:30 -0700333 /*
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500334 * Once page cache replacement of page migration started, page_count
Hugh Dickins9a1ea432018-12-28 00:36:14 -0800335 * is zero; but we must not call put_and_wait_on_page_locked() without
336 * a ref. Use get_page_unless_zero(), and just fault again if it fails.
Nick Piggine2867812008-07-25 19:45:30 -0700337 */
338 if (!get_page_unless_zero(page))
339 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700340 pte_unmap_unlock(ptep, ptl);
Matthew Wilcox (Oracle)48054622021-02-24 12:02:02 -0800341 put_and_wait_on_page_locked(page, TASK_UNINTERRUPTIBLE);
Christoph Lameter06972122006-06-23 02:03:35 -0700342 return;
343out:
344 pte_unmap_unlock(ptep, ptl);
Tao Zeng54f845e2022-01-26 16:04:08 +0800345#ifdef CONFIG_AMLOGIC_CMA
346 if (need_wait)
347 usleep_range(1000, 1100);
348#endif
Christoph Lameter06972122006-06-23 02:03:35 -0700349}
350
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700351void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
352 unsigned long address)
353{
354 spinlock_t *ptl = pte_lockptr(mm, pmd);
355 pte_t *ptep = pte_offset_map(pmd, address);
356 __migration_entry_wait(mm, ptep, ptl);
357}
358
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -0800359void migration_entry_wait_huge(struct vm_area_struct *vma,
360 struct mm_struct *mm, pte_t *pte)
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700361{
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -0800362 spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte);
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700363 __migration_entry_wait(mm, pte, ptl);
364}
365
Zi Yan616b8372017-09-08 16:10:57 -0700366#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
367void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd)
368{
369 spinlock_t *ptl;
370 struct page *page;
371
372 ptl = pmd_lock(mm, pmd);
373 if (!is_pmd_migration_entry(*pmd))
374 goto unlock;
Alistair Poppleaf5cdaf2021-06-30 18:54:06 -0700375 page = pfn_swap_entry_to_page(pmd_to_swp_entry(*pmd));
Zi Yan616b8372017-09-08 16:10:57 -0700376 if (!get_page_unless_zero(page))
377 goto unlock;
378 spin_unlock(ptl);
Matthew Wilcox (Oracle)48054622021-02-24 12:02:02 -0800379 put_and_wait_on_page_locked(page, TASK_UNINTERRUPTIBLE);
Zi Yan616b8372017-09-08 16:10:57 -0700380 return;
381unlock:
382 spin_unlock(ptl);
383}
384#endif
385
Jan Karaf9004822019-03-05 15:48:46 -0800386static int expected_page_refs(struct address_space *mapping, struct page *page)
Jan Kara0b3901b2018-12-28 00:39:01 -0800387{
388 int expected_count = 1;
389
390 /*
Ralph Campbellf1f4f3a2020-10-13 16:58:42 -0700391 * Device private pages have an extra refcount as they are
Jan Kara0b3901b2018-12-28 00:39:01 -0800392 * ZONE_DEVICE pages.
393 */
394 expected_count += is_device_private_page(page);
Jan Karaf9004822019-03-05 15:48:46 -0800395 if (mapping)
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -0700396 expected_count += thp_nr_pages(page) + page_has_private(page);
Jan Kara0b3901b2018-12-28 00:39:01 -0800397
398 return expected_count;
399}
400
Christoph Lameterb20a3502006-03-22 00:09:12 -0800401/*
Christoph Lameterc3fcf8a2006-06-23 02:03:32 -0700402 * Replace the page in the mapping.
Christoph Lameter5b5c7122006-06-23 02:03:29 -0700403 *
404 * The number of remaining references must be:
405 * 1 for anonymous pages without a mapping
406 * 2 for pages with a mapping
David Howells266cf652009-04-03 16:42:36 +0100407 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800408 */
Gu Zheng36bc08c2013-07-16 17:56:16 +0800409int migrate_page_move_mapping(struct address_space *mapping,
Keith Busch37109692019-07-18 15:58:46 -0700410 struct page *newpage, struct page *page, int extra_count)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800411{
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500412 XA_STATE(xas, &mapping->i_pages, page_index(page));
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800413 struct zone *oldzone, *newzone;
414 int dirty;
Jan Karaf9004822019-03-05 15:48:46 -0800415 int expected_count = expected_page_refs(mapping, page) + extra_count;
Shakeel Butt5c447d22021-01-23 21:01:15 -0800416 int nr = thp_nr_pages(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -0700417
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700418 if (!mapping) {
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700419 /* Anonymous page without mapping */
Tao Zeng54f845e2022-01-26 16:04:08 +0800420 #ifdef CONFIG_AMLOGIC_CMA
421 if (page_count(page) != expected_count)
422 cma_debug(2, page, " anon page cnt miss match, e:%d\n",
423 expected_count);
424 #endif
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500425 if (page_count(page) != expected_count)
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700426 return -EAGAIN;
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800427
428 /* No turning back from here */
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800429 newpage->index = page->index;
430 newpage->mapping = page->mapping;
431 if (PageSwapBacked(page))
Hugh Dickinsfa9949d2016-05-19 17:12:41 -0700432 __SetPageSwapBacked(newpage);
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800433
Rafael Aquini78bd5202012-12-11 16:02:31 -0800434 return MIGRATEPAGE_SUCCESS;
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700435 }
436
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800437 oldzone = page_zone(page);
438 newzone = page_zone(newpage);
439
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500440 xas_lock_irq(&xas);
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500441 if (page_count(page) != expected_count || xas_load(&xas) != page) {
442 xas_unlock_irq(&xas);
Tao Zeng54f845e2022-01-26 16:04:08 +0800443 #ifdef CONFIG_AMLOGIC_CMA
444 cma_debug(2, page, " anon page cnt miss match, e:%d, p:%d\n",
445 expected_count, page_has_private(page));
446 #endif
Christoph Lametere23ca002006-04-10 22:52:57 -0700447 return -EAGAIN;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800448 }
449
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700450 if (!page_ref_freeze(page, expected_count)) {
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500451 xas_unlock_irq(&xas);
Tao Zeng54f845e2022-01-26 16:04:08 +0800452 #ifdef CONFIG_AMLOGIC_CMA
453 cma_debug(2, page, " page free fail, e:%d, p:%d\n",
454 expected_count, page_has_private(page));
455 #endif
Nick Piggine2867812008-07-25 19:45:30 -0700456 return -EAGAIN;
457 }
458
Christoph Lameterb20a3502006-03-22 00:09:12 -0800459 /*
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800460 * Now we know that no one else is looking at the page:
461 * no turning back from here.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800462 */
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800463 newpage->index = page->index;
464 newpage->mapping = page->mapping;
Shakeel Butt5c447d22021-01-23 21:01:15 -0800465 page_ref_add(newpage, nr); /* add cache reference */
Nicholas Piggin6326fec2016-12-25 13:00:29 +1000466 if (PageSwapBacked(page)) {
467 __SetPageSwapBacked(newpage);
468 if (PageSwapCache(page)) {
469 SetPageSwapCache(newpage);
470 set_page_private(newpage, page_private(page));
471 }
472 } else {
473 VM_BUG_ON_PAGE(PageSwapCache(page), page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800474 }
475
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800476 /* Move dirty while page refs frozen and newpage not yet exposed */
477 dirty = PageDirty(page);
478 if (dirty) {
479 ClearPageDirty(page);
480 SetPageDirty(newpage);
481 }
482
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500483 xas_store(&xas, newpage);
Naoya Horiguchie71769a2018-04-20 14:55:45 -0700484 if (PageTransHuge(page)) {
485 int i;
Naoya Horiguchie71769a2018-04-20 14:55:45 -0700486
Shakeel Butt5c447d22021-01-23 21:01:15 -0800487 for (i = 1; i < nr; i++) {
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500488 xas_next(&xas);
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700489 xas_store(&xas, newpage);
Naoya Horiguchie71769a2018-04-20 14:55:45 -0700490 }
Naoya Horiguchie71769a2018-04-20 14:55:45 -0700491 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800492
493 /*
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800494 * Drop cache reference from old page by unfreezing
495 * to one less reference.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800496 * We know this isn't the last reference.
497 */
Shakeel Butt5c447d22021-01-23 21:01:15 -0800498 page_ref_unfreeze(page, expected_count - nr);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800499
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500500 xas_unlock(&xas);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800501 /* Leave irq disabled to prevent preemption while updating stats */
502
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700503 /*
504 * If moved to a different zone then also account
505 * the page for that zone. Other VM counters will be
506 * taken care of when we establish references to the
507 * new page and drop references to the old page.
508 *
509 * Note that anonymous pages are accounted for
Mel Gorman4b9d0fa2016-07-28 15:46:17 -0700510 * via NR_FILE_PAGES and NR_ANON_MAPPED if they
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700511 * are mapped to swap space.
512 */
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800513 if (newzone != oldzone) {
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700514 struct lruvec *old_lruvec, *new_lruvec;
515 struct mem_cgroup *memcg;
516
517 memcg = page_memcg(page);
518 old_lruvec = mem_cgroup_lruvec(memcg, oldzone->zone_pgdat);
519 new_lruvec = mem_cgroup_lruvec(memcg, newzone->zone_pgdat);
520
Shakeel Butt5c447d22021-01-23 21:01:15 -0800521 __mod_lruvec_state(old_lruvec, NR_FILE_PAGES, -nr);
522 __mod_lruvec_state(new_lruvec, NR_FILE_PAGES, nr);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800523 if (PageSwapBacked(page) && !PageSwapCache(page)) {
Shakeel Butt5c447d22021-01-23 21:01:15 -0800524 __mod_lruvec_state(old_lruvec, NR_SHMEM, -nr);
525 __mod_lruvec_state(new_lruvec, NR_SHMEM, nr);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800526 }
Shakeel Buttb6038942021-02-24 12:03:55 -0800527#ifdef CONFIG_SWAP
528 if (PageSwapCache(page)) {
529 __mod_lruvec_state(old_lruvec, NR_SWAPCACHE, -nr);
530 __mod_lruvec_state(new_lruvec, NR_SWAPCACHE, nr);
531 }
532#endif
Christoph Hellwigf56753a2020-09-24 08:51:40 +0200533 if (dirty && mapping_can_writeback(mapping)) {
Shakeel Butt5c447d22021-01-23 21:01:15 -0800534 __mod_lruvec_state(old_lruvec, NR_FILE_DIRTY, -nr);
535 __mod_zone_page_state(oldzone, NR_ZONE_WRITE_PENDING, -nr);
536 __mod_lruvec_state(new_lruvec, NR_FILE_DIRTY, nr);
537 __mod_zone_page_state(newzone, NR_ZONE_WRITE_PENDING, nr);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800538 }
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700539 }
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800540 local_irq_enable();
Christoph Lameterb20a3502006-03-22 00:09:12 -0800541
Rafael Aquini78bd5202012-12-11 16:02:31 -0800542 return MIGRATEPAGE_SUCCESS;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800543}
Richard Weinberger1118dce2016-06-16 23:26:14 +0200544EXPORT_SYMBOL(migrate_page_move_mapping);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800545
546/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900547 * The expected number of remaining references is the same as that
548 * of migrate_page_move_mapping().
549 */
550int migrate_huge_page_move_mapping(struct address_space *mapping,
551 struct page *newpage, struct page *page)
552{
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500553 XA_STATE(xas, &mapping->i_pages, page_index(page));
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900554 int expected_count;
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900555
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500556 xas_lock_irq(&xas);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900557 expected_count = 2 + page_has_private(page);
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500558 if (page_count(page) != expected_count || xas_load(&xas) != page) {
559 xas_unlock_irq(&xas);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900560 return -EAGAIN;
561 }
562
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700563 if (!page_ref_freeze(page, expected_count)) {
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500564 xas_unlock_irq(&xas);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900565 return -EAGAIN;
566 }
567
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800568 newpage->index = page->index;
569 newpage->mapping = page->mapping;
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700570
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900571 get_page(newpage);
572
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500573 xas_store(&xas, newpage);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900574
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700575 page_ref_unfreeze(page, expected_count - 1);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900576
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500577 xas_unlock_irq(&xas);
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700578
Rafael Aquini78bd5202012-12-11 16:02:31 -0800579 return MIGRATEPAGE_SUCCESS;
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900580}
581
582/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800583 * Copy the page to its new location
584 */
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700585void migrate_page_states(struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800586{
Rik van Riel7851a452013-10-07 11:29:23 +0100587 int cpupid;
588
Christoph Lameterb20a3502006-03-22 00:09:12 -0800589 if (PageError(page))
590 SetPageError(newpage);
591 if (PageReferenced(page))
592 SetPageReferenced(newpage);
593 if (PageUptodate(page))
594 SetPageUptodate(newpage);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700595 if (TestClearPageActive(page)) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800596 VM_BUG_ON_PAGE(PageUnevictable(page), page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800597 SetPageActive(newpage);
Lee Schermerhorn418b27e2009-12-14 17:59:54 -0800598 } else if (TestClearPageUnevictable(page))
599 SetPageUnevictable(newpage);
Johannes Weiner1899ad12018-10-26 15:06:04 -0700600 if (PageWorkingset(page))
601 SetPageWorkingset(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800602 if (PageChecked(page))
603 SetPageChecked(newpage);
604 if (PageMappedToDisk(page))
605 SetPageMappedToDisk(newpage);
606
qinglin.lia1a623f2024-06-21 14:36:34 +0800607 trace_android_vh_look_around_migrate_page(page, newpage);
608
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800609 /* Move dirty on pages not done by migrate_page_move_mapping() */
610 if (PageDirty(page))
611 SetPageDirty(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800612
Vladimir Davydov33c3fc72015-09-09 15:35:45 -0700613 if (page_is_young(page))
614 set_page_young(newpage);
615 if (page_is_idle(page))
616 set_page_idle(newpage);
617
Rik van Riel7851a452013-10-07 11:29:23 +0100618 /*
619 * Copy NUMA information to the new page, to prevent over-eager
620 * future migrations of this same page.
621 */
622 cpupid = page_cpupid_xchg_last(page, -1);
623 page_cpupid_xchg_last(newpage, cpupid);
624
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800625 ksm_migrate_page(newpage, page);
Hugh Dickinsc8d65532013-02-22 16:35:10 -0800626 /*
627 * Please do not reorder this without considering how mm/ksm.c's
628 * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache().
629 */
Naoya Horiguchib3b3a992015-04-15 16:13:15 -0700630 if (PageSwapCache(page))
631 ClearPageSwapCache(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800632 ClearPagePrivate(page);
Muchun Songad2fa372021-06-30 18:47:21 -0700633
634 /* page->private contains hugetlb specific flags */
635 if (!PageHuge(page))
636 set_page_private(page, 0);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800637
638 /*
639 * If any waiters have accumulated on the new page then
640 * wake them up.
641 */
642 if (PageWriteback(newpage))
643 end_page_writeback(newpage);
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700644
Yang Shi6aeff242020-04-06 20:04:21 -0700645 /*
646 * PG_readahead shares the same bit with PG_reclaim. The above
647 * end_page_writeback() may clear PG_readahead mistakenly, so set the
648 * bit after that.
649 */
650 if (PageReadahead(page))
651 SetPageReadahead(newpage);
652
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700653 copy_page_owner(page, newpage);
Johannes Weiner74485cf2016-03-15 14:57:54 -0700654
Hugh Dickinsa333e3e2020-09-18 21:20:06 -0700655 if (!PageHuge(page))
656 mem_cgroup_migrate(page, newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800657}
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700658EXPORT_SYMBOL(migrate_page_states);
659
660void migrate_page_copy(struct page *newpage, struct page *page)
661{
662 if (PageHuge(page) || PageTransHuge(page))
663 copy_huge_page(newpage, page);
664 else
665 copy_highpage(newpage, page);
666
667 migrate_page_states(newpage, page);
668}
Richard Weinberger1118dce2016-06-16 23:26:14 +0200669EXPORT_SYMBOL(migrate_page_copy);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800670
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700671/************************************************************
672 * Migration functions
673 ***********************************************************/
674
Christoph Lameterb20a3502006-03-22 00:09:12 -0800675/*
Minchan Kimbda807d2016-07-26 15:23:05 -0700676 * Common logic to directly migrate a single LRU page suitable for
David Howells266cf652009-04-03 16:42:36 +0100677 * pages that do not use PagePrivate/PagePrivate2.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800678 *
679 * Pages are locked upon entry and exit.
680 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700681int migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800682 struct page *newpage, struct page *page,
683 enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800684{
685 int rc;
686
687 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
688
Keith Busch37109692019-07-18 15:58:46 -0700689 rc = migrate_page_move_mapping(mapping, newpage, page, 0);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800690
Rafael Aquini78bd5202012-12-11 16:02:31 -0800691 if (rc != MIGRATEPAGE_SUCCESS)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800692 return rc;
693
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700694 if (mode != MIGRATE_SYNC_NO_COPY)
695 migrate_page_copy(newpage, page);
696 else
697 migrate_page_states(newpage, page);
Rafael Aquini78bd5202012-12-11 16:02:31 -0800698 return MIGRATEPAGE_SUCCESS;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800699}
700EXPORT_SYMBOL(migrate_page);
701
David Howells93614012006-09-30 20:45:40 +0200702#ifdef CONFIG_BLOCK
Jan Kara84ade7c2018-12-28 00:39:09 -0800703/* Returns true if all buffers are successfully locked */
704static bool buffer_migrate_lock_buffers(struct buffer_head *head,
705 enum migrate_mode mode)
706{
707 struct buffer_head *bh = head;
708
709 /* Simple case, sync compaction */
710 if (mode != MIGRATE_ASYNC) {
711 do {
Jan Kara84ade7c2018-12-28 00:39:09 -0800712 lock_buffer(bh);
713 bh = bh->b_this_page;
714
715 } while (bh != head);
716
717 return true;
718 }
719
720 /* async case, we cannot block on lock_buffer so use trylock_buffer */
721 do {
Jan Kara84ade7c2018-12-28 00:39:09 -0800722 if (!trylock_buffer(bh)) {
723 /*
724 * We failed to lock the buffer and cannot stall in
725 * async migration. Release the taken locks
726 */
727 struct buffer_head *failed_bh = bh;
Jan Kara84ade7c2018-12-28 00:39:09 -0800728 bh = head;
729 while (bh != failed_bh) {
730 unlock_buffer(bh);
Jan Kara84ade7c2018-12-28 00:39:09 -0800731 bh = bh->b_this_page;
732 }
733 return false;
734 }
735
736 bh = bh->b_this_page;
737 } while (bh != head);
738 return true;
739}
740
Jan Kara89cb0882018-12-28 00:39:12 -0800741static int __buffer_migrate_page(struct address_space *mapping,
742 struct page *newpage, struct page *page, enum migrate_mode mode,
743 bool check_refs)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700744{
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700745 struct buffer_head *bh, *head;
746 int rc;
Jan Karacc4f11e2018-12-28 00:39:05 -0800747 int expected_count;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700748
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700749 if (!page_has_buffers(page))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800750 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700751
Jan Karacc4f11e2018-12-28 00:39:05 -0800752 /* Check whether page does not have extra refs before we do more work */
Jan Karaf9004822019-03-05 15:48:46 -0800753 expected_count = expected_page_refs(mapping, page);
Jan Karacc4f11e2018-12-28 00:39:05 -0800754 if (page_count(page) != expected_count)
755 return -EAGAIN;
756
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700757 head = page_buffers(page);
Jan Karacc4f11e2018-12-28 00:39:05 -0800758 if (!buffer_migrate_lock_buffers(head, mode))
759 return -EAGAIN;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700760
Jan Kara89cb0882018-12-28 00:39:12 -0800761 if (check_refs) {
762 bool busy;
763 bool invalidated = false;
764
765recheck_buffers:
766 busy = false;
767 spin_lock(&mapping->private_lock);
768 bh = head;
769 do {
770 if (atomic_read(&bh->b_count)) {
771 busy = true;
772 break;
773 }
774 bh = bh->b_this_page;
775 } while (bh != head);
Jan Kara89cb0882018-12-28 00:39:12 -0800776 if (busy) {
777 if (invalidated) {
778 rc = -EAGAIN;
779 goto unlock_buffers;
780 }
Jan Karaebdf4de2019-08-02 21:48:47 -0700781 spin_unlock(&mapping->private_lock);
Jan Kara89cb0882018-12-28 00:39:12 -0800782 invalidate_bh_lrus();
783 invalidated = true;
784 goto recheck_buffers;
785 }
786 }
787
Keith Busch37109692019-07-18 15:58:46 -0700788 rc = migrate_page_move_mapping(mapping, newpage, page, 0);
Rafael Aquini78bd5202012-12-11 16:02:31 -0800789 if (rc != MIGRATEPAGE_SUCCESS)
Jan Karacc4f11e2018-12-28 00:39:05 -0800790 goto unlock_buffers;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700791
Guoqing Jiangcd0f3712020-06-01 21:48:06 -0700792 attach_page_private(newpage, detach_page_private(page));
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700793
794 bh = head;
795 do {
796 set_bh_page(bh, newpage, bh_offset(bh));
797 bh = bh->b_this_page;
798
799 } while (bh != head);
800
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700801 if (mode != MIGRATE_SYNC_NO_COPY)
802 migrate_page_copy(newpage, page);
803 else
804 migrate_page_states(newpage, page);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700805
Jan Karacc4f11e2018-12-28 00:39:05 -0800806 rc = MIGRATEPAGE_SUCCESS;
807unlock_buffers:
Jan Karaebdf4de2019-08-02 21:48:47 -0700808 if (check_refs)
809 spin_unlock(&mapping->private_lock);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700810 bh = head;
811 do {
812 unlock_buffer(bh);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700813 bh = bh->b_this_page;
814
815 } while (bh != head);
816
Jan Karacc4f11e2018-12-28 00:39:05 -0800817 return rc;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700818}
Jan Kara89cb0882018-12-28 00:39:12 -0800819
820/*
821 * Migration function for pages with buffers. This function can only be used
822 * if the underlying filesystem guarantees that no other references to "page"
823 * exist. For example attached buffer heads are accessed only under page lock.
824 */
825int buffer_migrate_page(struct address_space *mapping,
826 struct page *newpage, struct page *page, enum migrate_mode mode)
827{
828 return __buffer_migrate_page(mapping, newpage, page, mode, false);
829}
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700830EXPORT_SYMBOL(buffer_migrate_page);
Jan Kara89cb0882018-12-28 00:39:12 -0800831
832/*
833 * Same as above except that this variant is more careful and checks that there
834 * are also no buffer head references. This function is the right one for
835 * mappings where buffer heads are directly looked up and referenced (such as
836 * block device mappings).
837 */
838int buffer_migrate_page_norefs(struct address_space *mapping,
839 struct page *newpage, struct page *page, enum migrate_mode mode)
840{
841 return __buffer_migrate_page(mapping, newpage, page, mode, true);
842}
David Howells93614012006-09-30 20:45:40 +0200843#endif
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700844
Christoph Lameter04e62a22006-06-23 02:03:38 -0700845/*
846 * Writeback a page to clean the dirty state
847 */
848static int writeout(struct address_space *mapping, struct page *page)
849{
850 struct writeback_control wbc = {
851 .sync_mode = WB_SYNC_NONE,
852 .nr_to_write = 1,
853 .range_start = 0,
854 .range_end = LLONG_MAX,
Christoph Lameter04e62a22006-06-23 02:03:38 -0700855 .for_reclaim = 1
856 };
857 int rc;
858
859 if (!mapping->a_ops->writepage)
860 /* No write method for the address space */
861 return -EINVAL;
862
863 if (!clear_page_dirty_for_io(page))
864 /* Someone else already triggered a write */
865 return -EAGAIN;
866
867 /*
868 * A dirty page may imply that the underlying filesystem has
869 * the page on some queue. So the page must be clean for
870 * migration. Writeout may mean we loose the lock and the
871 * page state is no longer what we checked for earlier.
872 * At this point we know that the migration attempt cannot
873 * be successful.
874 */
Kirill A. Shutemove3884662016-03-17 14:20:07 -0700875 remove_migration_ptes(page, page, false);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700876
877 rc = mapping->a_ops->writepage(page, &wbc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700878
879 if (rc != AOP_WRITEPAGE_ACTIVATE)
880 /* unlocked. Relock */
881 lock_page(page);
882
Hugh Dickinsbda85502008-11-19 15:36:36 -0800883 return (rc < 0) ? -EIO : -EAGAIN;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700884}
885
886/*
887 * Default handling if a filesystem does not provide a migration function.
888 */
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700889static int fallback_migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800890 struct page *newpage, struct page *page, enum migrate_mode mode)
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700891{
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800892 if (PageDirty(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800893 /* Only writeback pages in full synchronous migration */
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700894 switch (mode) {
895 case MIGRATE_SYNC:
896 case MIGRATE_SYNC_NO_COPY:
897 break;
898 default:
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800899 return -EBUSY;
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700900 }
Christoph Lameter04e62a22006-06-23 02:03:38 -0700901 return writeout(mapping, page);
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800902 }
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700903
904 /*
905 * Buffers may be managed in a filesystem specific way.
906 * We must have no buffers or drop them.
907 */
David Howells266cf652009-04-03 16:42:36 +0100908 if (page_has_private(page) &&
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700909 !try_to_release_page(page, GFP_KERNEL))
Mel Gorman806031b2019-03-05 15:44:43 -0800910 return mode == MIGRATE_SYNC ? -EAGAIN : -EBUSY;
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700911
Mel Gormana6bc32b2012-01-12 17:19:43 -0800912 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700913}
914
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700915/*
Christoph Lametere24f0b82006-06-23 02:03:51 -0700916 * Move a page to a newly allocated page
917 * The page is locked and all ptes have been successfully removed.
918 *
919 * The new page will have replaced the old page if this function
920 * is successful.
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700921 *
922 * Return value:
923 * < 0 - error code
Rafael Aquini78bd5202012-12-11 16:02:31 -0800924 * MIGRATEPAGE_SUCCESS - success
Christoph Lametere24f0b82006-06-23 02:03:51 -0700925 */
Mel Gorman3fe20112010-05-24 14:32:20 -0700926static int move_to_new_page(struct page *newpage, struct page *page,
Hugh Dickins5c3f9a62015-11-05 18:49:53 -0800927 enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700928{
929 struct address_space *mapping;
Minchan Kimbda807d2016-07-26 15:23:05 -0700930 int rc = -EAGAIN;
931 bool is_lru = !__PageMovable(page);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700932
Hugh Dickins7db76712015-11-05 18:49:49 -0800933 VM_BUG_ON_PAGE(!PageLocked(page), page);
934 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700935
Christoph Lametere24f0b82006-06-23 02:03:51 -0700936 mapping = page_mapping(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700937
938 if (likely(is_lru)) {
939 if (!mapping)
940 rc = migrate_page(mapping, newpage, page, mode);
941 else if (mapping->a_ops->migratepage)
942 /*
943 * Most pages have a mapping and most filesystems
944 * provide a migratepage callback. Anonymous pages
945 * are part of swap space which also has its own
946 * migratepage callback. This is the most common path
947 * for page migration.
948 */
949 rc = mapping->a_ops->migratepage(mapping, newpage,
950 page, mode);
951 else
952 rc = fallback_migrate_page(mapping, newpage,
953 page, mode);
954 } else {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700955 /*
Minchan Kimbda807d2016-07-26 15:23:05 -0700956 * In case of non-lru page, it could be released after
957 * isolation step. In that case, we shouldn't try migration.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700958 */
Minchan Kimbda807d2016-07-26 15:23:05 -0700959 VM_BUG_ON_PAGE(!PageIsolated(page), page);
960 if (!PageMovable(page)) {
961 rc = MIGRATEPAGE_SUCCESS;
andrew.yang0d8a8362022-03-15 16:58:34 +1100962 ClearPageIsolated(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700963 goto out;
964 }
965
966 rc = mapping->a_ops->migratepage(mapping, newpage,
967 page, mode);
968 WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS &&
969 !PageIsolated(page));
970 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700971
Hugh Dickins5c3f9a62015-11-05 18:49:53 -0800972 /*
973 * When successful, old pagecache page->mapping must be cleared before
974 * page is freed; but stats require that PageAnon be left as PageAnon.
975 */
976 if (rc == MIGRATEPAGE_SUCCESS) {
Minchan Kimbda807d2016-07-26 15:23:05 -0700977 if (__PageMovable(page)) {
978 VM_BUG_ON_PAGE(!PageIsolated(page), page);
979
980 /*
981 * We clear PG_movable under page_lock so any compactor
982 * cannot try to migrate this page.
983 */
andrew.yang0d8a8362022-03-15 16:58:34 +1100984 ClearPageIsolated(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700985 }
986
987 /*
Ralph Campbellc23a0c92020-01-30 22:14:41 -0800988 * Anonymous and movable page->mapping will be cleared by
Minchan Kimbda807d2016-07-26 15:23:05 -0700989 * free_pages_prepare so don't reset it here for keeping
990 * the type to work PageAnon, for example.
991 */
992 if (!PageMappingFlags(page))
Hugh Dickins5c3f9a62015-11-05 18:49:53 -0800993 page->mapping = NULL;
Lars Perssond2b2c6dd2019-03-28 20:44:28 -0700994
Muchun Song97a9f802022-03-22 14:41:56 -0700995 if (likely(!is_zone_device_page(newpage))) {
996 int i, nr = compound_nr(newpage);
Lars Perssond2b2c6dd2019-03-28 20:44:28 -0700997
Muchun Song97a9f802022-03-22 14:41:56 -0700998 for (i = 0; i < nr; i++)
999 flush_dcache_page(newpage + i);
1000 }
Mel Gorman3fe20112010-05-24 14:32:20 -07001001 }
Minchan Kimbda807d2016-07-26 15:23:05 -07001002out:
Christoph Lametere24f0b82006-06-23 02:03:51 -07001003 return rc;
1004}
1005
Minchan Kim0dabec92011-10-31 17:06:57 -07001006static int __unmap_and_move(struct page *page, struct page *newpage,
Hugh Dickins9c620e22013-02-22 16:35:14 -08001007 int force, enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -07001008{
Minchan Kim0dabec92011-10-31 17:06:57 -07001009 int rc = -EAGAIN;
Baolin Wang213ecb32021-09-08 15:18:06 -07001010 bool page_was_mapped = false;
Mel Gorman3f6c8272010-05-24 14:32:17 -07001011 struct anon_vma *anon_vma = NULL;
Minchan Kimbda807d2016-07-26 15:23:05 -07001012 bool is_lru = !__PageMovable(page);
Christoph Lameter95a402c2006-06-23 02:03:53 -07001013
Nick Piggin529ae9a2008-08-02 12:01:03 +02001014 if (!trylock_page(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -08001015 if (!force || mode == MIGRATE_ASYNC)
Minchan Kim0dabec92011-10-31 17:06:57 -07001016 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -08001017
1018 /*
1019 * It's not safe for direct compaction to call lock_page.
1020 * For example, during page readahead pages are added locked
1021 * to the LRU. Later, when the IO completes the pages are
1022 * marked uptodate and unlocked. However, the queueing
1023 * could be merging multiple pages for one bio (e.g.
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -07001024 * mpage_readahead). If an allocation happens for the
Mel Gorman3e7d3442011-01-13 15:45:56 -08001025 * second or third page, the process can end up locking
1026 * the same page twice and deadlocking. Rather than
1027 * trying to be clever about what pages can be locked,
1028 * avoid the use of lock_page for direct compaction
1029 * altogether.
1030 */
1031 if (current->flags & PF_MEMALLOC)
Minchan Kim0dabec92011-10-31 17:06:57 -07001032 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -08001033
Christoph Lametere24f0b82006-06-23 02:03:51 -07001034 lock_page(page);
1035 }
1036
1037 if (PageWriteback(page)) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -07001038 /*
Jianguo Wufed5b642013-04-29 15:07:58 -07001039 * Only in the case of a full synchronous migration is it
Mel Gormana6bc32b2012-01-12 17:19:43 -08001040 * necessary to wait for PageWriteback. In the async case,
1041 * the retry loop is too short and in the sync-light case,
1042 * the overhead of stalling is too much
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -07001043 */
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07001044 switch (mode) {
1045 case MIGRATE_SYNC:
1046 case MIGRATE_SYNC_NO_COPY:
1047 break;
1048 default:
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -07001049 rc = -EBUSY;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07001050 goto out_unlock;
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -07001051 }
1052 if (!force)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07001053 goto out_unlock;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001054 wait_on_page_writeback(page);
1055 }
Hugh Dickins03f15c82015-11-05 18:49:56 -08001056
Christoph Lametere24f0b82006-06-23 02:03:51 -07001057 /*
Baolin Wang68a98432021-09-08 15:18:03 -07001058 * By try_to_migrate(), page->mapcount goes down to 0 here. In this case,
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001059 * we cannot notice that anon_vma is freed while we migrates a page.
Hugh Dickins1ce82b62011-01-13 15:47:30 -08001060 * This get_anon_vma() delays freeing anon_vma pointer until the end
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001061 * of migration. File cache pages are no problem because of page_lock()
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -07001062 * File Caches may use write_page() or lock_page() in migration, then,
1063 * just care Anon page here.
Hugh Dickins03f15c82015-11-05 18:49:56 -08001064 *
1065 * Only page_get_anon_vma() understands the subtleties of
1066 * getting a hold on an anon_vma from outside one of its mms.
1067 * But if we cannot get anon_vma, then we won't need it anyway,
1068 * because that implies that the anon page is no longer mapped
1069 * (and cannot be remapped so long as we hold the page lock).
Christoph Lametere24f0b82006-06-23 02:03:51 -07001070 */
Hugh Dickins03f15c82015-11-05 18:49:56 -08001071 if (PageAnon(page) && !PageKsm(page))
Peter Zijlstra746b18d2011-05-24 17:12:10 -07001072 anon_vma = page_get_anon_vma(page);
Shaohua Li62e1c552008-02-04 22:29:33 -08001073
Hugh Dickins7db76712015-11-05 18:49:49 -08001074 /*
1075 * Block others from accessing the new page when we get around to
1076 * establishing additional references. We are usually the only one
1077 * holding a reference to newpage at this point. We used to have a BUG
1078 * here if trylock_page(newpage) fails, but would like to allow for
1079 * cases where there might be a race with the previous use of newpage.
1080 * This is much like races on refcount of oldpage: just don't BUG().
1081 */
1082 if (unlikely(!trylock_page(newpage)))
1083 goto out_unlock;
1084
Minchan Kimbda807d2016-07-26 15:23:05 -07001085 if (unlikely(!is_lru)) {
1086 rc = move_to_new_page(newpage, page, mode);
1087 goto out_unlock_both;
1088 }
1089
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001090 /*
Shaohua Li62e1c552008-02-04 22:29:33 -08001091 * Corner case handling:
1092 * 1. When a new swap-cache page is read into, it is added to the LRU
1093 * and treated as swapcache but it has no rmap yet.
1094 * Calling try_to_unmap() against a page->mapping==NULL page will
1095 * trigger a BUG. So handle it here.
Yang Shid12b8952020-12-14 19:13:02 -08001096 * 2. An orphaned page (see truncate_cleanup_page) might have
Shaohua Li62e1c552008-02-04 22:29:33 -08001097 * fs-private metadata. The page can be picked up due to memory
1098 * offlining. Everywhere else except page reclaim, the page is
1099 * invisible to the vm, so the page can not be migrated. So try to
1100 * free the metadata, so the page can be freed.
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001101 */
Shaohua Li62e1c552008-02-04 22:29:33 -08001102 if (!page->mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -08001103 VM_BUG_ON_PAGE(PageAnon(page), page);
Hugh Dickins1ce82b62011-01-13 15:47:30 -08001104 if (page_has_private(page)) {
Shaohua Li62e1c552008-02-04 22:29:33 -08001105 try_to_free_buffers(page);
Hugh Dickins7db76712015-11-05 18:49:49 -08001106 goto out_unlock_both;
Shaohua Li62e1c552008-02-04 22:29:33 -08001107 }
Hugh Dickins7db76712015-11-05 18:49:49 -08001108 } else if (page_mapped(page)) {
1109 /* Establish migration ptes */
Hugh Dickins03f15c82015-11-05 18:49:56 -08001110 VM_BUG_ON_PAGE(PageAnon(page) && !PageKsm(page) && !anon_vma,
1111 page);
Alistair Popplea98a2f02021-06-30 18:54:16 -07001112 try_to_migrate(page, 0);
Baolin Wang213ecb32021-09-08 15:18:06 -07001113 page_was_mapped = true;
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001114 }
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001115
Christoph Lametere6a15302006-06-25 05:46:49 -07001116 if (!page_mapped(page))
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001117 rc = move_to_new_page(newpage, page, mode);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001118
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001119 if (page_was_mapped)
1120 remove_migration_ptes(page,
Kirill A. Shutemove3884662016-03-17 14:20:07 -07001121 rc == MIGRATEPAGE_SUCCESS ? newpage : page, false);
Mel Gorman3f6c8272010-05-24 14:32:17 -07001122
Hugh Dickins7db76712015-11-05 18:49:49 -08001123out_unlock_both:
1124 unlock_page(newpage);
1125out_unlock:
Mel Gorman3f6c8272010-05-24 14:32:17 -07001126 /* Drop an anon_vma reference if we took one */
Rik van Riel76545062010-08-09 17:18:41 -07001127 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -07001128 put_anon_vma(anon_vma);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001129 unlock_page(page);
Minchan Kim0dabec92011-10-31 17:06:57 -07001130out:
Minchan Kimc6c919e2016-07-26 15:23:02 -07001131 /*
1132 * If migration is successful, decrease refcount of the newpage
1133 * which will not free the page because new page owner increased
1134 * refcounter. As well, if it is LRU page, add the page to LRU
David Hildenbrande0a352f2019-02-01 14:21:19 -08001135 * list in here. Use the old state of the isolated source page to
1136 * determine if we migrated a LRU page. newpage was already unlocked
1137 * and possibly modified by its owner - don't rely on the page
1138 * state.
Minchan Kimc6c919e2016-07-26 15:23:02 -07001139 */
1140 if (rc == MIGRATEPAGE_SUCCESS) {
David Hildenbrande0a352f2019-02-01 14:21:19 -08001141 if (unlikely(!is_lru))
Minchan Kimc6c919e2016-07-26 15:23:02 -07001142 put_page(newpage);
1143 else
1144 putback_lru_page(newpage);
1145 }
1146
Minchan Kim0dabec92011-10-31 17:06:57 -07001147 return rc;
1148}
Christoph Lameter95a402c2006-06-23 02:03:53 -07001149
Dave Hansen79c28a42021-09-02 14:59:06 -07001150
1151/*
1152 * node_demotion[] example:
1153 *
1154 * Consider a system with two sockets. Each socket has
1155 * three classes of memory attached: fast, medium and slow.
1156 * Each memory class is placed in its own NUMA node. The
1157 * CPUs are placed in the node with the "fast" memory. The
1158 * 6 NUMA nodes (0-5) might be split among the sockets like
1159 * this:
1160 *
1161 * Socket A: 0, 1, 2
1162 * Socket B: 3, 4, 5
1163 *
1164 * When Node 0 fills up, its memory should be migrated to
1165 * Node 1. When Node 1 fills up, it should be migrated to
1166 * Node 2. The migration path start on the nodes with the
1167 * processors (since allocations default to this node) and
1168 * fast memory, progress through medium and end with the
1169 * slow memory:
1170 *
1171 * 0 -> 1 -> 2 -> stop
1172 * 3 -> 4 -> 5 -> stop
1173 *
1174 * This is represented in the node_demotion[] like this:
1175 *
1176 * { 1, // Node 0 migrates to 1
1177 * 2, // Node 1 migrates to 2
1178 * -1, // Node 2 does not migrate
1179 * 4, // Node 3 migrates to 4
1180 * 5, // Node 4 migrates to 5
1181 * -1} // Node 5 does not migrate
1182 */
1183
1184/*
1185 * Writes to this array occur without locking. Cycles are
1186 * not allowed: Node X demotes to Y which demotes to X...
1187 *
1188 * If multiple reads are performed, a single rcu_read_lock()
1189 * must be held over all reads to ensure that no cycles are
1190 * observed.
1191 */
1192static int node_demotion[MAX_NUMNODES] __read_mostly =
1193 {[0 ... MAX_NUMNODES - 1] = NUMA_NO_NODE};
1194
1195/**
1196 * next_demotion_node() - Get the next node in the demotion path
1197 * @node: The starting node to lookup the next node
1198 *
Randy Dunlapc9bd7d12021-09-02 15:00:36 -07001199 * Return: node id for next memory node in the demotion path hierarchy
Dave Hansen79c28a42021-09-02 14:59:06 -07001200 * from @node; NUMA_NO_NODE if @node is terminal. This does not keep
1201 * @node online or guarantee that it *continues* to be the next demotion
1202 * target.
1203 */
1204int next_demotion_node(int node)
1205{
1206 int target;
1207
1208 /*
1209 * node_demotion[] is updated without excluding this
1210 * function from running. RCU doesn't provide any
1211 * compiler barriers, so the READ_ONCE() is required
1212 * to avoid compiler reordering or read merging.
1213 *
1214 * Make sure to use RCU over entire code blocks if
1215 * node_demotion[] reads need to be consistent.
1216 */
1217 rcu_read_lock();
1218 target = READ_ONCE(node_demotion[node]);
1219 rcu_read_unlock();
1220
1221 return target;
1222}
1223
Minchan Kim0dabec92011-10-31 17:06:57 -07001224/*
1225 * Obtain the lock on page, remove all ptes and migrate the page
1226 * to the newly allocated page in newpage.
1227 */
Linus Torvalds6ec44762020-07-08 10:48:35 -07001228static int unmap_and_move(new_page_t get_new_page,
Geert Uytterhoevenef2a5152015-04-14 15:44:22 -07001229 free_page_t put_new_page,
1230 unsigned long private, struct page *page,
Naoya Horiguchiadd05ce2015-06-24 16:56:50 -07001231 int force, enum migrate_mode mode,
Yang Shidd4ae782020-12-14 19:13:06 -08001232 enum migrate_reason reason,
1233 struct list_head *ret)
Minchan Kim0dabec92011-10-31 17:06:57 -07001234{
Hugh Dickins2def7422015-11-05 18:49:46 -08001235 int rc = MIGRATEPAGE_SUCCESS;
Yang Shi74d4a572019-11-30 17:57:12 -08001236 struct page *newpage = NULL;
Minchan Kim0dabec92011-10-31 17:06:57 -07001237
Michal Hocko94723aa2018-04-10 16:30:07 -07001238 if (!thp_migration_supported() && PageTransHuge(page))
Yang Shid532e2e2020-12-14 19:13:16 -08001239 return -ENOSYS;
Michal Hocko94723aa2018-04-10 16:30:07 -07001240
Minchan Kim0dabec92011-10-31 17:06:57 -07001241 if (page_count(page) == 1) {
1242 /* page was freed from under us. So we are done. */
Minchan Kimc6c919e2016-07-26 15:23:02 -07001243 ClearPageActive(page);
1244 ClearPageUnevictable(page);
Minchan Kimbda807d2016-07-26 15:23:05 -07001245 if (unlikely(__PageMovable(page))) {
1246 lock_page(page);
1247 if (!PageMovable(page))
andrew.yang0d8a8362022-03-15 16:58:34 +11001248 ClearPageIsolated(page);
Minchan Kimbda807d2016-07-26 15:23:05 -07001249 unlock_page(page);
1250 }
Minchan Kim0dabec92011-10-31 17:06:57 -07001251 goto out;
1252 }
1253
Yang Shi74d4a572019-11-30 17:57:12 -08001254 newpage = get_new_page(page, private);
1255 if (!newpage)
1256 return -ENOMEM;
1257
Hugh Dickins9c620e22013-02-22 16:35:14 -08001258 rc = __unmap_and_move(page, newpage, force, mode);
Minchan Kimc6c919e2016-07-26 15:23:02 -07001259 if (rc == MIGRATEPAGE_SUCCESS)
Vlastimil Babka7cd12b42016-03-15 14:56:18 -07001260 set_page_owner_migrate_reason(newpage, reason);
Rafael Aquinibf6bddf12012-12-11 16:02:42 -08001261
Minchan Kim0dabec92011-10-31 17:06:57 -07001262out:
Christoph Lametere24f0b82006-06-23 02:03:51 -07001263 if (rc != -EAGAIN) {
Minchan Kim0dabec92011-10-31 17:06:57 -07001264 /*
1265 * A page that has been migrated has all references
1266 * removed and will be freed. A page that has not been
Ralph Campbellc23a0c92020-01-30 22:14:41 -08001267 * migrated will have kept its references and be restored.
Minchan Kim0dabec92011-10-31 17:06:57 -07001268 */
1269 list_del(&page->lru);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001270 }
David Rientjes68711a72014-06-04 16:08:25 -07001271
Christoph Lameter95a402c2006-06-23 02:03:53 -07001272 /*
Minchan Kimc6c919e2016-07-26 15:23:02 -07001273 * If migration is successful, releases reference grabbed during
1274 * isolation. Otherwise, restore the page to right list unless
1275 * we want to retry.
Christoph Lameter95a402c2006-06-23 02:03:53 -07001276 */
Minchan Kimc6c919e2016-07-26 15:23:02 -07001277 if (rc == MIGRATEPAGE_SUCCESS) {
Yang Shidd4ae782020-12-14 19:13:06 -08001278 /*
1279 * Compaction can migrate also non-LRU pages which are
1280 * not accounted to NR_ISOLATED_*. They can be recognized
1281 * as __PageMovable
1282 */
1283 if (likely(!__PageMovable(page)))
1284 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
1285 page_is_file_lru(page), -thp_nr_pages(page));
1286
Oscar Salvador79f5f8f2020-10-15 20:07:09 -07001287 if (reason != MR_MEMORY_FAILURE)
Minchan Kimc6c919e2016-07-26 15:23:02 -07001288 /*
Oscar Salvador79f5f8f2020-10-15 20:07:09 -07001289 * We release the page in page_handle_poison.
Minchan Kimc6c919e2016-07-26 15:23:02 -07001290 */
Oscar Salvador79f5f8f2020-10-15 20:07:09 -07001291 put_page(page);
Minchan Kimc6c919e2016-07-26 15:23:02 -07001292 } else {
Yang Shidd4ae782020-12-14 19:13:06 -08001293 if (rc != -EAGAIN)
1294 list_add_tail(&page->lru, ret);
Minchan Kimbda807d2016-07-26 15:23:05 -07001295
Minchan Kimc6c919e2016-07-26 15:23:02 -07001296 if (put_new_page)
1297 put_new_page(newpage, private);
1298 else
1299 put_page(newpage);
1300 }
Jianxiong Pan07622f02022-09-22 17:25:35 +08001301#ifdef CONFIG_AMLOGIC_PIN_LOCKED_FILE
1302 if (reason == MR_CONTIG_RANGE && rc == MIGRATEPAGE_SUCCESS)
1303 aml_clear_pin_locked_file(page);
1304#endif
David Rientjes68711a72014-06-04 16:08:25 -07001305
Christoph Lametere24f0b82006-06-23 02:03:51 -07001306 return rc;
1307}
1308
1309/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001310 * Counterpart of unmap_and_move_page() for hugepage migration.
1311 *
1312 * This function doesn't wait the completion of hugepage I/O
1313 * because there is no race between I/O and migration for hugepage.
1314 * Note that currently hugepage I/O occurs only in direct I/O
1315 * where no lock is held and PG_writeback is irrelevant,
1316 * and writeback status of all subpages are counted in the reference
1317 * count of the head page (i.e. if all subpages of a 2MB hugepage are
1318 * under direct I/O, the reference of the head page is 512 and a bit more.)
1319 * This means that when we try to migrate hugepage whose subpages are
1320 * doing direct I/O, some references remain after try_to_unmap() and
1321 * hugepage migration fails without data corruption.
1322 *
1323 * There is also no race when direct I/O is issued on the page under migration,
1324 * because then pte is replaced with migration swap entry and direct I/O code
1325 * will wait in the page fault for migration to complete.
1326 */
1327static int unmap_and_move_huge_page(new_page_t get_new_page,
David Rientjes68711a72014-06-04 16:08:25 -07001328 free_page_t put_new_page, unsigned long private,
1329 struct page *hpage, int force,
Yang Shidd4ae782020-12-14 19:13:06 -08001330 enum migrate_mode mode, int reason,
1331 struct list_head *ret)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001332{
Hugh Dickins2def7422015-11-05 18:49:46 -08001333 int rc = -EAGAIN;
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001334 int page_was_mapped = 0;
Joonsoo Kim32665f22014-01-21 15:51:15 -08001335 struct page *new_hpage;
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001336 struct anon_vma *anon_vma = NULL;
Mike Kravetzc0d03812020-04-01 21:11:05 -07001337 struct address_space *mapping = NULL;
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001338
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001339 /*
Anshuman Khandual7ed2c312019-03-05 15:43:44 -08001340 * Migratability of hugepages depends on architectures and their size.
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001341 * This check is necessary because some callers of hugepage migration
1342 * like soft offline and memory hotremove don't walk through page
1343 * tables or check whether the hugepage is pmd-based or not before
1344 * kicking migration.
1345 */
Naoya Horiguchi100873d2014-06-04 16:10:56 -07001346 if (!hugepage_migration_supported(page_hstate(hpage))) {
Yang Shidd4ae782020-12-14 19:13:06 -08001347 list_move_tail(&hpage->lru, ret);
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001348 return -ENOSYS;
Joonsoo Kim32665f22014-01-21 15:51:15 -08001349 }
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001350
Muchun Song71a64f62021-02-04 18:32:17 -08001351 if (page_count(hpage) == 1) {
1352 /* page was freed from under us. So we are done. */
1353 putback_active_hugepage(hpage);
1354 return MIGRATEPAGE_SUCCESS;
1355 }
1356
Michal Hocko666feb22018-04-10 16:30:03 -07001357 new_hpage = get_new_page(hpage, private);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001358 if (!new_hpage)
1359 return -ENOMEM;
1360
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001361 if (!trylock_page(hpage)) {
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07001362 if (!force)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001363 goto out;
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07001364 switch (mode) {
1365 case MIGRATE_SYNC:
1366 case MIGRATE_SYNC_NO_COPY:
1367 break;
1368 default:
1369 goto out;
1370 }
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001371 lock_page(hpage);
1372 }
1373
Mike Kravetzcb6acd02019-02-28 16:22:02 -08001374 /*
1375 * Check for pages which are in the process of being freed. Without
1376 * page_mapping() set, hugetlbfs specific move page routine will not
1377 * be called and we could leak usage counts for subpools.
1378 */
Muchun Song6acfb5b2021-06-30 18:51:29 -07001379 if (hugetlb_page_subpool(hpage) && !page_mapping(hpage)) {
Mike Kravetzcb6acd02019-02-28 16:22:02 -08001380 rc = -EBUSY;
1381 goto out_unlock;
1382 }
1383
Peter Zijlstra746b18d2011-05-24 17:12:10 -07001384 if (PageAnon(hpage))
1385 anon_vma = page_get_anon_vma(hpage);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001386
Hugh Dickins7db76712015-11-05 18:49:49 -08001387 if (unlikely(!trylock_page(new_hpage)))
1388 goto put_anon;
1389
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001390 if (page_mapped(hpage)) {
Mike Kravetz336bf302020-11-13 22:52:16 -08001391 bool mapping_locked = false;
Alistair Popplea98a2f02021-06-30 18:54:16 -07001392 enum ttu_flags ttu = 0;
Mike Kravetzc0d03812020-04-01 21:11:05 -07001393
Mike Kravetz336bf302020-11-13 22:52:16 -08001394 if (!PageAnon(hpage)) {
1395 /*
1396 * In shared mappings, try_to_unmap could potentially
1397 * call huge_pmd_unshare. Because of this, take
1398 * semaphore in write mode here and set TTU_RMAP_LOCKED
1399 * to let lower levels know we have taken the lock.
1400 */
1401 mapping = hugetlb_page_mapping_lock_write(hpage);
1402 if (unlikely(!mapping))
1403 goto unlock_put_anon;
1404
1405 mapping_locked = true;
1406 ttu |= TTU_RMAP_LOCKED;
1407 }
1408
Alistair Popplea98a2f02021-06-30 18:54:16 -07001409 try_to_migrate(hpage, ttu);
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001410 page_was_mapped = 1;
Mike Kravetz336bf302020-11-13 22:52:16 -08001411
1412 if (mapping_locked)
1413 i_mmap_unlock_write(mapping);
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001414 }
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001415
1416 if (!page_mapped(hpage))
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001417 rc = move_to_new_page(new_hpage, hpage, mode);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001418
Mike Kravetz336bf302020-11-13 22:52:16 -08001419 if (page_was_mapped)
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001420 remove_migration_ptes(hpage,
Mike Kravetz336bf302020-11-13 22:52:16 -08001421 rc == MIGRATEPAGE_SUCCESS ? new_hpage : hpage, false);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001422
Mike Kravetzc0d03812020-04-01 21:11:05 -07001423unlock_put_anon:
Hugh Dickins7db76712015-11-05 18:49:49 -08001424 unlock_page(new_hpage);
1425
1426put_anon:
Hugh Dickinsfd4a4662011-01-13 15:47:31 -08001427 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -07001428 put_anon_vma(anon_vma);
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -07001429
Hugh Dickins2def7422015-11-05 18:49:46 -08001430 if (rc == MIGRATEPAGE_SUCCESS) {
Michal Hockoab5ac902018-01-31 16:20:48 -08001431 move_hugetlb_state(hpage, new_hpage, reason);
Hugh Dickins2def7422015-11-05 18:49:46 -08001432 put_new_page = NULL;
1433 }
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -07001434
Mike Kravetzcb6acd02019-02-28 16:22:02 -08001435out_unlock:
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001436 unlock_page(hpage);
Hillf Danton09761332011-12-08 14:34:20 -08001437out:
Yang Shidd4ae782020-12-14 19:13:06 -08001438 if (rc == MIGRATEPAGE_SUCCESS)
Naoya Horiguchib8ec1ce2013-09-11 14:22:01 -07001439 putback_active_hugepage(hpage);
Miaohe Lina04840c2021-05-04 18:37:07 -07001440 else if (rc != -EAGAIN)
Yang Shidd4ae782020-12-14 19:13:06 -08001441 list_move_tail(&hpage->lru, ret);
David Rientjes68711a72014-06-04 16:08:25 -07001442
1443 /*
1444 * If migration was not successful and there's a freeing callback, use
1445 * it. Otherwise, put_page() will drop the reference grabbed during
1446 * isolation.
1447 */
Hugh Dickins2def7422015-11-05 18:49:46 -08001448 if (put_new_page)
David Rientjes68711a72014-06-04 16:08:25 -07001449 put_new_page(new_hpage, private);
1450 else
Naoya Horiguchi3aaa76e2015-09-22 14:59:14 -07001451 putback_active_hugepage(new_hpage);
David Rientjes68711a72014-06-04 16:08:25 -07001452
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001453 return rc;
1454}
1455
Yang Shid532e2e2020-12-14 19:13:16 -08001456static inline int try_split_thp(struct page *page, struct page **page2,
1457 struct list_head *from)
1458{
1459 int rc = 0;
1460
1461 lock_page(page);
1462 rc = split_huge_page_to_list(page, from);
1463 unlock_page(page);
1464 if (!rc)
1465 list_safe_reset_next(page, *page2, lru);
1466
1467 return rc;
1468}
1469
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001470/*
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001471 * migrate_pages - migrate the pages specified in a list, to the free pages
1472 * supplied as the target for the page migration
Christoph Lameterb20a3502006-03-22 00:09:12 -08001473 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001474 * @from: The list of pages to be migrated.
1475 * @get_new_page: The function used to allocate free pages to be used
1476 * as the target of the page migration.
David Rientjes68711a72014-06-04 16:08:25 -07001477 * @put_new_page: The function used to free target pages if migration
1478 * fails, or NULL if no special handling is necessary.
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001479 * @private: Private data to be passed on to get_new_page()
1480 * @mode: The migration mode that specifies the constraints for
1481 * page migration, if any.
1482 * @reason: The reason for page migration.
Yang Shi5ac95882021-09-02 14:59:13 -07001483 * @ret_succeeded: Set to the number of pages migrated successfully if
1484 * the caller passes a non-NULL pointer.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001485 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001486 * The function returns after 10 attempts or if no pages are movable any more
1487 * because the list has become empty or no retryable pages exist any more.
Yang Shidd4ae782020-12-14 19:13:06 -08001488 * It is caller's responsibility to call putback_movable_pages() to return pages
1489 * to the LRU or free list only if ret != 0.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001490 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001491 * Returns the number of pages that were not migrated, or an error code.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001492 */
Hugh Dickins9c620e22013-02-22 16:35:14 -08001493int migrate_pages(struct list_head *from, new_page_t get_new_page,
David Rientjes68711a72014-06-04 16:08:25 -07001494 free_page_t put_new_page, unsigned long private,
Yang Shi5ac95882021-09-02 14:59:13 -07001495 enum migrate_mode mode, int reason, unsigned int *ret_succeeded)
Christoph Lameterb20a3502006-03-22 00:09:12 -08001496{
Christoph Lametere24f0b82006-06-23 02:03:51 -07001497 int retry = 1;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001498 int thp_retry = 1;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001499 int nr_failed = 0;
Mel Gorman5647bc22012-10-19 10:46:20 +01001500 int nr_succeeded = 0;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001501 int nr_thp_succeeded = 0;
1502 int nr_thp_failed = 0;
1503 int nr_thp_split = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001504 int pass = 0;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001505 bool is_thp = false;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001506 struct page *page;
1507 struct page *page2;
1508 int swapwrite = current->flags & PF_SWAPWRITE;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001509 int rc, nr_subpages;
Yang Shidd4ae782020-12-14 19:13:06 -08001510 LIST_HEAD(ret_pages);
Yang Shib0b515b2021-06-30 18:51:48 -07001511 bool nosplit = (reason == MR_NUMA_MISPLACED);
Christoph Lameterb20a3502006-03-22 00:09:12 -08001512
Liam Mark7bc1aec2021-05-04 18:37:25 -07001513 trace_mm_migrate_pages_start(mode, reason);
1514
Christoph Lameterb20a3502006-03-22 00:09:12 -08001515 if (!swapwrite)
1516 current->flags |= PF_SWAPWRITE;
1517
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001518 for (pass = 0; pass < 10 && (retry || thp_retry); pass++) {
Christoph Lametere24f0b82006-06-23 02:03:51 -07001519 retry = 0;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001520 thp_retry = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001521
Christoph Lametere24f0b82006-06-23 02:03:51 -07001522 list_for_each_entry_safe(page, page2, from, lru) {
Michal Hocko94723aa2018-04-10 16:30:07 -07001523retry:
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001524 /*
1525 * THP statistics is based on the source huge page.
1526 * Capture required information that might get lost
1527 * during migration.
1528 */
Zi Yan6c5c7b92020-09-25 21:19:14 -07001529 is_thp = PageTransHuge(page) && !PageHuge(page);
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -07001530 nr_subpages = thp_nr_pages(page);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001531 cond_resched();
Christoph Lameterb20a3502006-03-22 00:09:12 -08001532
Naoya Horiguchi31caf662013-09-11 14:21:59 -07001533 if (PageHuge(page))
1534 rc = unmap_and_move_huge_page(get_new_page,
David Rientjes68711a72014-06-04 16:08:25 -07001535 put_new_page, private, page,
Yang Shidd4ae782020-12-14 19:13:06 -08001536 pass > 2, mode, reason,
1537 &ret_pages);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07001538 else
David Rientjes68711a72014-06-04 16:08:25 -07001539 rc = unmap_and_move(get_new_page, put_new_page,
Naoya Horiguchiadd05ce2015-06-24 16:56:50 -07001540 private, page, pass > 2, mode,
Yang Shidd4ae782020-12-14 19:13:06 -08001541 reason, &ret_pages);
1542 /*
1543 * The rules are:
1544 * Success: non hugetlb page will be freed, hugetlb
1545 * page will be put back
1546 * -EAGAIN: stay on the from list
1547 * -ENOMEM: stay on the from list
1548 * Other errno: put on ret_pages list then splice to
1549 * from list
1550 */
Christoph Lametere24f0b82006-06-23 02:03:51 -07001551 switch(rc) {
Yang Shid532e2e2020-12-14 19:13:16 -08001552 /*
1553 * THP migration might be unsupported or the
1554 * allocation could've failed so we should
1555 * retry on the same page with the THP split
1556 * to base pages.
1557 *
1558 * Head page is retried immediately and tail
1559 * pages are added to the tail of the list so
1560 * we encounter them after the rest of the list
1561 * is processed.
1562 */
1563 case -ENOSYS:
1564 /* THP migration is unsupported */
1565 if (is_thp) {
1566 if (!try_split_thp(page, &page2, from)) {
1567 nr_thp_split++;
1568 goto retry;
1569 }
1570
1571 nr_thp_failed++;
1572 nr_failed += nr_subpages;
1573 break;
1574 }
1575
1576 /* Hugetlb migration is unsupported */
1577 nr_failed++;
Tao Zeng54f845e2022-01-26 16:04:08 +08001578 #ifdef CONFIG_AMLOGIC_CMA
1579 cma_debug(2, page, " NO SYS\n");
1580 #endif
Yang Shid532e2e2020-12-14 19:13:16 -08001581 break;
Christoph Lameter95a402c2006-06-23 02:03:53 -07001582 case -ENOMEM:
Michal Hocko94723aa2018-04-10 16:30:07 -07001583 /*
Yang Shid532e2e2020-12-14 19:13:16 -08001584 * When memory is low, don't bother to try to migrate
1585 * other pages, just exit.
Yang Shib0b515b2021-06-30 18:51:48 -07001586 * THP NUMA faulting doesn't split THP to retry.
Michal Hocko94723aa2018-04-10 16:30:07 -07001587 */
Yang Shib0b515b2021-06-30 18:51:48 -07001588 if (is_thp && !nosplit) {
Yang Shid532e2e2020-12-14 19:13:16 -08001589 if (!try_split_thp(page, &page2, from)) {
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001590 nr_thp_split++;
Michal Hocko94723aa2018-04-10 16:30:07 -07001591 goto retry;
1592 }
Zi Yan6c5c7b92020-09-25 21:19:14 -07001593
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001594 nr_thp_failed++;
1595 nr_failed += nr_subpages;
1596 goto out;
1597 }
David Rientjesdfef2ef2016-05-20 16:59:05 -07001598 nr_failed++;
Tao Zeng54f845e2022-01-26 16:04:08 +08001599 #ifdef CONFIG_AMLOGIC_CMA
1600 cma_debug(2, page, " NO MEM\n");
1601 #endif
Christoph Lameter95a402c2006-06-23 02:03:53 -07001602 goto out;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001603 case -EAGAIN:
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001604 if (is_thp) {
1605 thp_retry++;
1606 break;
1607 }
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001608 retry++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001609 break;
Rafael Aquini78bd5202012-12-11 16:02:31 -08001610 case MIGRATEPAGE_SUCCESS:
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001611 if (is_thp) {
1612 nr_thp_succeeded++;
1613 nr_succeeded += nr_subpages;
1614 break;
1615 }
Mel Gorman5647bc22012-10-19 10:46:20 +01001616 nr_succeeded++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001617 break;
1618 default:
Naoya Horiguchi354a3362014-01-21 15:51:14 -08001619 /*
Yang Shid532e2e2020-12-14 19:13:16 -08001620 * Permanent failure (-EBUSY, etc.):
Naoya Horiguchi354a3362014-01-21 15:51:14 -08001621 * unlike -EAGAIN case, the failed page is
1622 * removed from migration page list and not
1623 * retried in the next outer loop.
1624 */
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001625 if (is_thp) {
1626 nr_thp_failed++;
1627 nr_failed += nr_subpages;
1628 break;
1629 }
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001630 nr_failed++;
Tao Zeng54f845e2022-01-26 16:04:08 +08001631 #ifdef CONFIG_AMLOGIC_CMA
1632 cma_debug(2, page, " failed:%d\n", rc);
1633 #endif
Christoph Lametere24f0b82006-06-23 02:03:51 -07001634 break;
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001635 }
Christoph Lameterb20a3502006-03-22 00:09:12 -08001636 }
1637 }
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001638 nr_failed += retry + thp_retry;
1639 nr_thp_failed += thp_retry;
Vlastimil Babkaf2f81fb2015-11-05 18:47:03 -08001640 rc = nr_failed;
Christoph Lameter95a402c2006-06-23 02:03:53 -07001641out:
Yang Shidd4ae782020-12-14 19:13:06 -08001642 /*
1643 * Put the permanent failure page back to migration list, they
1644 * will be put back to the right list by the caller.
1645 */
1646 list_splice(&ret_pages, from);
1647
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001648 count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
1649 count_vm_events(PGMIGRATE_FAIL, nr_failed);
1650 count_vm_events(THP_MIGRATION_SUCCESS, nr_thp_succeeded);
1651 count_vm_events(THP_MIGRATION_FAIL, nr_thp_failed);
1652 count_vm_events(THP_MIGRATION_SPLIT, nr_thp_split);
1653 trace_mm_migrate_pages(nr_succeeded, nr_failed, nr_thp_succeeded,
1654 nr_thp_failed, nr_thp_split, mode, reason);
Mel Gorman7b2a2d42012-10-19 14:07:31 +01001655
Christoph Lameterb20a3502006-03-22 00:09:12 -08001656 if (!swapwrite)
1657 current->flags &= ~PF_SWAPWRITE;
1658
Yang Shi5ac95882021-09-02 14:59:13 -07001659 if (ret_succeeded)
1660 *ret_succeeded = nr_succeeded;
1661
Rafael Aquini78bd5202012-12-11 16:02:31 -08001662 return rc;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001663}
Charan Teja Reddyf47b8522021-02-16 13:59:45 +05301664EXPORT_SYMBOL_GPL(migrate_pages);
Christoph Lameterb20a3502006-03-22 00:09:12 -08001665
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001666struct page *alloc_migration_target(struct page *page, unsigned long private)
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001667{
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001668 struct migration_target_control *mtc;
1669 gfp_t gfp_mask;
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001670 unsigned int order = 0;
1671 struct page *new_page = NULL;
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001672 int nid;
1673 int zidx;
1674
1675 mtc = (struct migration_target_control *)private;
1676 gfp_mask = mtc->gfp_mask;
1677 nid = mtc->nid;
1678 if (nid == NUMA_NO_NODE)
1679 nid = page_to_nid(page);
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001680
Joonsoo Kimd92bbc22020-08-11 18:37:17 -07001681 if (PageHuge(page)) {
1682 struct hstate *h = page_hstate(compound_head(page));
1683
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001684 gfp_mask = htlb_modify_alloc_mask(h, gfp_mask);
1685 return alloc_huge_page_nodemask(h, nid, mtc->nmask, gfp_mask);
Joonsoo Kimd92bbc22020-08-11 18:37:17 -07001686 }
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001687
1688 if (PageTransHuge(page)) {
Joonsoo Kim9933a0c2020-08-11 18:37:20 -07001689 /*
1690 * clear __GFP_RECLAIM to make the migration callback
1691 * consistent with regular THP allocations.
1692 */
1693 gfp_mask &= ~__GFP_RECLAIM;
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001694 gfp_mask |= GFP_TRANSHUGE;
1695 order = HPAGE_PMD_ORDER;
1696 }
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001697 zidx = zone_idx(page_zone(page));
1698 if (is_highmem_idx(zidx) || zidx == ZONE_MOVABLE)
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001699 gfp_mask |= __GFP_HIGHMEM;
1700
Matthew Wilcox (Oracle)84172f42021-04-29 23:01:15 -07001701 new_page = __alloc_pages(gfp_mask, order, nid, mtc->nmask);
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001702
1703 if (new_page && PageTransHuge(new_page))
1704 prep_transhuge_page(new_page);
1705
1706 return new_page;
1707}
1708
Christoph Lameter742755a2006-06-23 02:03:55 -07001709#ifdef CONFIG_NUMA
Christoph Lameter742755a2006-06-23 02:03:55 -07001710
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001711static int store_status(int __user *status, int start, int value, int nr)
Christoph Lameter742755a2006-06-23 02:03:55 -07001712{
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001713 while (nr-- > 0) {
1714 if (put_user(value, status + start))
1715 return -EFAULT;
1716 start++;
1717 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001718
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001719 return 0;
1720}
Christoph Lameter742755a2006-06-23 02:03:55 -07001721
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001722static int do_move_pages_to_node(struct mm_struct *mm,
1723 struct list_head *pagelist, int node)
1724{
1725 int err;
Joonsoo Kima0976312020-08-11 18:37:28 -07001726 struct migration_target_control mtc = {
1727 .nid = node,
1728 .gfp_mask = GFP_HIGHUSER_MOVABLE | __GFP_THISNODE,
1729 };
Christoph Lameter742755a2006-06-23 02:03:55 -07001730
Joonsoo Kima0976312020-08-11 18:37:28 -07001731 err = migrate_pages(pagelist, alloc_migration_target, NULL,
Yang Shi5ac95882021-09-02 14:59:13 -07001732 (unsigned long)&mtc, MIGRATE_SYNC, MR_SYSCALL, NULL);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001733 if (err)
1734 putback_movable_pages(pagelist);
1735 return err;
Christoph Lameter742755a2006-06-23 02:03:55 -07001736}
1737
1738/*
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001739 * Resolves the given address to a struct page, isolates it from the LRU and
1740 * puts it to the given pagelist.
Yang Shie0153fc2020-01-04 12:59:46 -08001741 * Returns:
1742 * errno - if the page cannot be found/isolated
1743 * 0 - when it doesn't have to be migrated because it is already on the
1744 * target node
1745 * 1 - when it has been queued
Christoph Lameter742755a2006-06-23 02:03:55 -07001746 */
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001747static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
1748 int node, struct list_head *pagelist, bool migrate_all)
Christoph Lameter742755a2006-06-23 02:03:55 -07001749{
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001750 struct vm_area_struct *vma;
1751 struct page *page;
1752 unsigned int follflags;
Christoph Lameter742755a2006-06-23 02:03:55 -07001753 int err;
Christoph Lameter742755a2006-06-23 02:03:55 -07001754
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001755 mmap_read_lock(mm);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001756 err = -EFAULT;
1757 vma = find_vma(mm, addr);
1758 if (!vma || addr < vma->vm_start || !vma_migratable(vma))
1759 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001760
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001761 /* FOLL_DUMP to ignore special (like zero) pages */
1762 follflags = FOLL_GET | FOLL_DUMP;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001763 page = follow_page(vma, addr, follflags);
Christoph Lameter742755a2006-06-23 02:03:55 -07001764
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001765 err = PTR_ERR(page);
1766 if (IS_ERR(page))
1767 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001768
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001769 err = -ENOENT;
1770 if (!page)
1771 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001772
Brice Gogline78bbfa2008-10-18 20:27:15 -07001773 err = 0;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001774 if (page_to_nid(page) == node)
1775 goto out_putpage;
Christoph Lameter742755a2006-06-23 02:03:55 -07001776
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001777 err = -EACCES;
1778 if (page_mapcount(page) > 1 && !migrate_all)
1779 goto out_putpage;
1780
1781 if (PageHuge(page)) {
1782 if (PageHead(page)) {
Miaohe Lin072e7412022-05-30 19:30:15 +08001783 err = isolate_hugetlb(page, pagelist);
1784 if (!err)
1785 err = 1;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001786 }
1787 } else {
1788 struct page *head;
1789
1790 head = compound_head(page);
1791 err = isolate_lru_page(head);
1792 if (err)
1793 goto out_putpage;
1794
Yang Shie0153fc2020-01-04 12:59:46 -08001795 err = 1;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001796 list_add_tail(&head->lru, pagelist);
1797 mod_node_page_state(page_pgdat(head),
Huang Ying9de4f222020-04-06 20:04:41 -07001798 NR_ISOLATED_ANON + page_is_file_lru(head),
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -07001799 thp_nr_pages(head));
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001800 }
1801out_putpage:
1802 /*
1803 * Either remove the duplicate refcount from
1804 * isolate_lru_page() or drop the page ref if it was
1805 * not isolated.
1806 */
1807 put_page(page);
1808out:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001809 mmap_read_unlock(mm);
Christoph Lameter742755a2006-06-23 02:03:55 -07001810 return err;
1811}
1812
Wei Yang7ca87832020-04-06 20:04:12 -07001813static int move_pages_and_store_status(struct mm_struct *mm, int node,
1814 struct list_head *pagelist, int __user *status,
1815 int start, int i, unsigned long nr_pages)
1816{
1817 int err;
1818
Wei Yang5d7ae892020-04-06 20:04:15 -07001819 if (list_empty(pagelist))
1820 return 0;
1821
Wei Yang7ca87832020-04-06 20:04:12 -07001822 err = do_move_pages_to_node(mm, pagelist, node);
1823 if (err) {
1824 /*
1825 * Positive err means the number of failed
1826 * pages to migrate. Since we are going to
1827 * abort and return the number of non-migrated
Long Liab9dd4f2020-12-14 19:12:52 -08001828 * pages, so need to include the rest of the
Wei Yang7ca87832020-04-06 20:04:12 -07001829 * nr_pages that have not been attempted as
1830 * well.
1831 */
1832 if (err > 0)
1833 err += nr_pages - i - 1;
1834 return err;
1835 }
1836 return store_status(status, start, node, i - start);
1837}
1838
Christoph Lameter742755a2006-06-23 02:03:55 -07001839/*
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001840 * Migrate an array of page address onto an array of nodes and fill
1841 * the corresponding array of status.
1842 */
Christoph Lameter3268c632012-03-21 16:34:06 -07001843static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001844 unsigned long nr_pages,
1845 const void __user * __user *pages,
1846 const int __user *nodes,
1847 int __user *status, int flags)
1848{
Gregory Price556b68d2023-10-03 10:48:56 -04001849 compat_uptr_t __user *compat_pages = (void __user *)pages;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001850 int current_node = NUMA_NO_NODE;
1851 LIST_HEAD(pagelist);
1852 int start, i;
1853 int err = 0, err1;
Brice Goglin35282a22009-06-16 15:32:43 -07001854
Minchan Kim361a2a22021-05-04 18:36:57 -07001855 lru_cache_disable();
Brice Goglin35282a22009-06-16 15:32:43 -07001856
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001857 for (i = start = 0; i < nr_pages; i++) {
1858 const void __user *p;
1859 unsigned long addr;
1860 int node;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001861
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001862 err = -EFAULT;
Gregory Price556b68d2023-10-03 10:48:56 -04001863 if (in_compat_syscall()) {
1864 compat_uptr_t cp;
1865
1866 if (get_user(cp, compat_pages + i))
1867 goto out_flush;
1868
1869 p = compat_ptr(cp);
1870 } else {
1871 if (get_user(p, pages + i))
1872 goto out_flush;
1873 }
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001874 if (get_user(node, nodes + i))
1875 goto out_flush;
Andrey Konovalov057d33892019-09-25 16:48:30 -07001876 addr = (unsigned long)untagged_addr(p);
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001877
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001878 err = -ENODEV;
1879 if (node < 0 || node >= MAX_NUMNODES)
1880 goto out_flush;
1881 if (!node_state(node, N_MEMORY))
1882 goto out_flush;
Brice Goglin3140a222009-01-06 14:38:57 -08001883
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001884 err = -EACCES;
1885 if (!node_isset(node, task_nodes))
1886 goto out_flush;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001887
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001888 if (current_node == NUMA_NO_NODE) {
1889 current_node = node;
1890 start = i;
1891 } else if (node != current_node) {
Wei Yang7ca87832020-04-06 20:04:12 -07001892 err = move_pages_and_store_status(mm, current_node,
1893 &pagelist, status, start, i, nr_pages);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001894 if (err)
1895 goto out;
1896 start = i;
1897 current_node = node;
Brice Goglin3140a222009-01-06 14:38:57 -08001898 }
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001899
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001900 /*
1901 * Errors in the page lookup or isolation are not fatal and we simply
1902 * report them via status
1903 */
1904 err = add_page_for_migration(mm, addr, current_node,
1905 &pagelist, flags & MPOL_MF_MOVE_ALL);
Yang Shie0153fc2020-01-04 12:59:46 -08001906
Wei Yangd08221a2020-04-06 20:04:18 -07001907 if (err > 0) {
Yang Shie0153fc2020-01-04 12:59:46 -08001908 /* The page is successfully queued for migration */
1909 continue;
1910 }
Brice Goglin3140a222009-01-06 14:38:57 -08001911
Wei Yangd08221a2020-04-06 20:04:18 -07001912 /*
1913 * If the page is already on the target node (!err), store the
1914 * node, otherwise, store the err.
1915 */
1916 err = store_status(status, i, err ? : current_node, 1);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001917 if (err)
1918 goto out_flush;
Brice Goglin3140a222009-01-06 14:38:57 -08001919
Wei Yang7ca87832020-04-06 20:04:12 -07001920 err = move_pages_and_store_status(mm, current_node, &pagelist,
1921 status, start, i, nr_pages);
Wei Yang4afdace2020-04-06 20:04:09 -07001922 if (err)
1923 goto out;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001924 current_node = NUMA_NO_NODE;
Brice Goglin3140a222009-01-06 14:38:57 -08001925 }
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001926out_flush:
1927 /* Make sure we do not overwrite the existing error */
Wei Yang7ca87832020-04-06 20:04:12 -07001928 err1 = move_pages_and_store_status(mm, current_node, &pagelist,
1929 status, start, i, nr_pages);
Wei Yangdfe9aa22020-01-30 22:11:14 -08001930 if (err >= 0)
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001931 err = err1;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001932out:
Minchan Kim361a2a22021-05-04 18:36:57 -07001933 lru_cache_enable();
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001934 return err;
1935}
1936
1937/*
Brice Goglin2f007e72008-10-18 20:27:16 -07001938 * Determine the nodes of an array of pages and store it in an array of status.
Christoph Lameter742755a2006-06-23 02:03:55 -07001939 */
Brice Goglin80bba122008-12-09 13:14:23 -08001940static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1941 const void __user **pages, int *status)
Christoph Lameter742755a2006-06-23 02:03:55 -07001942{
Brice Goglin2f007e72008-10-18 20:27:16 -07001943 unsigned long i;
Brice Goglin2f007e72008-10-18 20:27:16 -07001944
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001945 mmap_read_lock(mm);
Christoph Lameter742755a2006-06-23 02:03:55 -07001946
Brice Goglin2f007e72008-10-18 20:27:16 -07001947 for (i = 0; i < nr_pages; i++) {
Brice Goglin80bba122008-12-09 13:14:23 -08001948 unsigned long addr = (unsigned long)(*pages);
Christoph Lameter742755a2006-06-23 02:03:55 -07001949 struct vm_area_struct *vma;
1950 struct page *page;
KOSAKI Motohiroc095adb2008-12-16 16:06:43 +09001951 int err = -EFAULT;
Brice Goglin2f007e72008-10-18 20:27:16 -07001952
Liam Howlett059b8b42021-06-28 19:39:44 -07001953 vma = vma_lookup(mm, addr);
1954 if (!vma)
Christoph Lameter742755a2006-06-23 02:03:55 -07001955 goto set_status;
1956
Kirill A. Shutemovd8998442015-09-04 15:47:53 -07001957 /* FOLL_DUMP to ignore special (like zero) pages */
1958 page = follow_page(vma, addr, FOLL_DUMP);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001959
1960 err = PTR_ERR(page);
1961 if (IS_ERR(page))
1962 goto set_status;
1963
Kirill A. Shutemovd8998442015-09-04 15:47:53 -07001964 err = page ? page_to_nid(page) : -ENOENT;
Christoph Lameter742755a2006-06-23 02:03:55 -07001965set_status:
Brice Goglin80bba122008-12-09 13:14:23 -08001966 *status = err;
1967
1968 pages++;
1969 status++;
1970 }
1971
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001972 mmap_read_unlock(mm);
Brice Goglin80bba122008-12-09 13:14:23 -08001973}
1974
Arnd Bergmann5b1b5612021-09-08 15:18:17 -07001975static int get_compat_pages_array(const void __user *chunk_pages[],
1976 const void __user * __user *pages,
1977 unsigned long chunk_nr)
1978{
1979 compat_uptr_t __user *pages32 = (compat_uptr_t __user *)pages;
1980 compat_uptr_t p;
1981 int i;
1982
1983 for (i = 0; i < chunk_nr; i++) {
1984 if (get_user(p, pages32 + i))
1985 return -EFAULT;
1986 chunk_pages[i] = compat_ptr(p);
1987 }
1988
1989 return 0;
1990}
1991
Brice Goglin80bba122008-12-09 13:14:23 -08001992/*
1993 * Determine the nodes of a user array of pages and store it in
1994 * a user array of status.
1995 */
1996static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1997 const void __user * __user *pages,
1998 int __user *status)
1999{
2000#define DO_PAGES_STAT_CHUNK_NR 16
2001 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
2002 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
Brice Goglin80bba122008-12-09 13:14:23 -08002003
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08002004 while (nr_pages) {
2005 unsigned long chunk_nr;
Brice Goglin80bba122008-12-09 13:14:23 -08002006
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08002007 chunk_nr = nr_pages;
2008 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
2009 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
2010
Arnd Bergmann5b1b5612021-09-08 15:18:17 -07002011 if (in_compat_syscall()) {
2012 if (get_compat_pages_array(chunk_pages, pages,
2013 chunk_nr))
2014 break;
2015 } else {
2016 if (copy_from_user(chunk_pages, pages,
2017 chunk_nr * sizeof(*chunk_pages)))
2018 break;
2019 }
Brice Goglin80bba122008-12-09 13:14:23 -08002020
2021 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
2022
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08002023 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
2024 break;
Christoph Lameter742755a2006-06-23 02:03:55 -07002025
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08002026 pages += chunk_nr;
2027 status += chunk_nr;
2028 nr_pages -= chunk_nr;
2029 }
2030 return nr_pages ? -EFAULT : 0;
Christoph Lameter742755a2006-06-23 02:03:55 -07002031}
2032
Miaohe Lin4dc200c2020-10-17 16:14:03 -07002033static struct mm_struct *find_mm_struct(pid_t pid, nodemask_t *mem_nodes)
2034{
2035 struct task_struct *task;
2036 struct mm_struct *mm;
2037
2038 /*
2039 * There is no need to check if current process has the right to modify
2040 * the specified process when they are same.
2041 */
2042 if (!pid) {
2043 mmget(current->mm);
2044 *mem_nodes = cpuset_mems_allowed(current);
2045 return current->mm;
2046 }
2047
2048 /* Find the mm_struct */
2049 rcu_read_lock();
2050 task = find_task_by_vpid(pid);
2051 if (!task) {
2052 rcu_read_unlock();
2053 return ERR_PTR(-ESRCH);
2054 }
2055 get_task_struct(task);
2056
2057 /*
2058 * Check if this process has the right to modify the specified
2059 * process. Use the regular "ptrace_may_access()" checks.
2060 */
2061 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
2062 rcu_read_unlock();
2063 mm = ERR_PTR(-EPERM);
2064 goto out;
2065 }
2066 rcu_read_unlock();
2067
2068 mm = ERR_PTR(security_task_movememory(task));
2069 if (IS_ERR(mm))
2070 goto out;
2071 *mem_nodes = cpuset_mems_allowed(task);
2072 mm = get_task_mm(task);
2073out:
2074 put_task_struct(task);
2075 if (!mm)
2076 mm = ERR_PTR(-EINVAL);
2077 return mm;
2078}
2079
Christoph Lameter742755a2006-06-23 02:03:55 -07002080/*
2081 * Move a list of pages in the address space of the currently executing
2082 * process.
2083 */
Dominik Brodowski7addf442018-03-17 16:08:03 +01002084static int kernel_move_pages(pid_t pid, unsigned long nr_pages,
2085 const void __user * __user *pages,
2086 const int __user *nodes,
2087 int __user *status, int flags)
Christoph Lameter742755a2006-06-23 02:03:55 -07002088{
Christoph Lameter742755a2006-06-23 02:03:55 -07002089 struct mm_struct *mm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07002090 int err;
Christoph Lameter3268c632012-03-21 16:34:06 -07002091 nodemask_t task_nodes;
Christoph Lameter742755a2006-06-23 02:03:55 -07002092
2093 /* Check flags */
2094 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
2095 return -EINVAL;
2096
2097 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
2098 return -EPERM;
2099
Miaohe Lin4dc200c2020-10-17 16:14:03 -07002100 mm = find_mm_struct(pid, &task_nodes);
2101 if (IS_ERR(mm))
2102 return PTR_ERR(mm);
Sasha Levin6e8b09e2012-04-25 16:01:53 -07002103
2104 if (nodes)
2105 err = do_pages_move(mm, task_nodes, nr_pages, pages,
2106 nodes, status, flags);
2107 else
2108 err = do_pages_stat(mm, nr_pages, pages, status);
Christoph Lameter3268c632012-03-21 16:34:06 -07002109
2110 mmput(mm);
2111 return err;
Christoph Lameter742755a2006-06-23 02:03:55 -07002112}
Christoph Lameter742755a2006-06-23 02:03:55 -07002113
Dominik Brodowski7addf442018-03-17 16:08:03 +01002114SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
2115 const void __user * __user *, pages,
2116 const int __user *, nodes,
2117 int __user *, status, int, flags)
2118{
2119 return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags);
2120}
2121
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002122#ifdef CONFIG_NUMA_BALANCING
2123/*
2124 * Returns true if this is a safe migration target node for misplaced NUMA
2125 * pages. Currently it only checks the watermarks which crude
2126 */
2127static bool migrate_balanced_pgdat(struct pglist_data *pgdat,
Mel Gorman3abef4e2013-02-22 16:34:27 -08002128 unsigned long nr_migrate_pages)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002129{
2130 int z;
Mel Gorman599d0c92016-07-28 15:45:31 -07002131
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002132 for (z = pgdat->nr_zones - 1; z >= 0; z--) {
2133 struct zone *zone = pgdat->node_zones + z;
2134
2135 if (!populated_zone(zone))
2136 continue;
2137
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002138 /* Avoid waking kswapd by allocating pages_to_migrate pages. */
2139 if (!zone_watermark_ok(zone, 0,
2140 high_wmark_pages(zone) +
2141 nr_migrate_pages,
Huang Yingbfe9d002019-11-30 17:57:28 -08002142 ZONE_MOVABLE, 0))
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002143 continue;
2144 return true;
2145 }
2146 return false;
2147}
2148
2149static struct page *alloc_misplaced_dst_page(struct page *page,
Michal Hocko666feb22018-04-10 16:30:03 -07002150 unsigned long data)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002151{
2152 int nid = (int) data;
2153 struct page *newpage;
2154
Vlastimil Babka96db8002015-09-08 15:03:50 -07002155 newpage = __alloc_pages_node(nid,
Johannes Weinere97ca8e52014-03-10 15:49:43 -07002156 (GFP_HIGHUSER_MOVABLE |
2157 __GFP_THISNODE | __GFP_NOMEMALLOC |
2158 __GFP_NORETRY | __GFP_NOWARN) &
Mel Gorman8479eba2016-02-26 15:19:31 -08002159 ~__GFP_RECLAIM, 0);
Hillf Dantonbac03822012-11-27 14:46:24 +00002160
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002161 return newpage;
2162}
2163
Yang Shic5b5a3d2021-06-30 18:51:42 -07002164static struct page *alloc_misplaced_dst_page_thp(struct page *page,
2165 unsigned long data)
2166{
2167 int nid = (int) data;
2168 struct page *newpage;
2169
2170 newpage = alloc_pages_node(nid, (GFP_TRANSHUGE_LIGHT | __GFP_THISNODE),
2171 HPAGE_PMD_ORDER);
2172 if (!newpage)
2173 goto out;
2174
2175 prep_transhuge_page(newpage);
2176
2177out:
2178 return newpage;
2179}
2180
Mel Gorman1c30e012014-01-21 15:50:58 -08002181static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
Mel Gormanb32967f2012-11-19 12:35:47 +00002182{
Hugh Dickins340ef392013-02-22 16:34:33 -08002183 int page_lru;
Baolin Wang2b9b6242021-09-08 15:18:01 -07002184 int nr_pages = thp_nr_pages(page);
Mel Gormanb32967f2012-11-19 12:35:47 +00002185
Sasha Levin309381fea2014-01-23 15:52:54 -08002186 VM_BUG_ON_PAGE(compound_order(page) && !PageTransHuge(page), page);
Mel Gorman3abef4e2013-02-22 16:34:27 -08002187
Yang Shi662aeea2021-06-30 18:51:51 -07002188 /* Do not migrate THP mapped by multiple processes */
2189 if (PageTransHuge(page) && total_mapcount(page) > 1)
2190 return 0;
2191
Mel Gormanb32967f2012-11-19 12:35:47 +00002192 /* Avoid migrating to a node that is nearly full */
Baolin Wang2b9b6242021-09-08 15:18:01 -07002193 if (!migrate_balanced_pgdat(pgdat, nr_pages))
Hugh Dickins340ef392013-02-22 16:34:33 -08002194 return 0;
Mel Gormanb32967f2012-11-19 12:35:47 +00002195
Hugh Dickins340ef392013-02-22 16:34:33 -08002196 if (isolate_lru_page(page))
2197 return 0;
Mel Gormanb32967f2012-11-19 12:35:47 +00002198
Huang Ying9de4f222020-04-06 20:04:41 -07002199 page_lru = page_is_file_lru(page);
Mel Gorman599d0c92016-07-28 15:45:31 -07002200 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_lru,
Baolin Wang2b9b6242021-09-08 15:18:01 -07002201 nr_pages);
Hugh Dickins340ef392013-02-22 16:34:33 -08002202
2203 /*
2204 * Isolating the page has taken another reference, so the
2205 * caller's reference can be safely dropped without the page
2206 * disappearing underneath us during migration.
Mel Gormanb32967f2012-11-19 12:35:47 +00002207 */
2208 put_page(page);
Hugh Dickins340ef392013-02-22 16:34:33 -08002209 return 1;
Mel Gormanb32967f2012-11-19 12:35:47 +00002210}
2211
Mel Gormana8f60772012-11-14 21:41:46 +00002212/*
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002213 * Attempt to migrate a misplaced page to the specified destination
2214 * node. Caller is expected to have an elevated reference count on
2215 * the page that will be dropped by this function before returning.
2216 */
Mel Gorman1bc115d2013-10-07 11:29:05 +01002217int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma,
2218 int node)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002219{
Mel Gormana8f60772012-11-14 21:41:46 +00002220 pg_data_t *pgdat = NODE_DATA(node);
Hugh Dickins340ef392013-02-22 16:34:33 -08002221 int isolated;
Mel Gormanb32967f2012-11-19 12:35:47 +00002222 int nr_remaining;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002223 LIST_HEAD(migratepages);
Yang Shic5b5a3d2021-06-30 18:51:42 -07002224 new_page_t *new;
2225 bool compound;
Aneesh Kumar K.Vb5916c02021-07-29 14:53:47 -07002226 int nr_pages = thp_nr_pages(page);
Yang Shic5b5a3d2021-06-30 18:51:42 -07002227
2228 /*
2229 * PTE mapped THP or HugeTLB page can't reach here so the page could
2230 * be either base page or THP. And it must be head page if it is
2231 * THP.
2232 */
2233 compound = PageTransHuge(page);
2234
2235 if (compound)
2236 new = alloc_misplaced_dst_page_thp;
2237 else
2238 new = alloc_misplaced_dst_page;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002239
2240 /*
Mel Gorman1bc115d2013-10-07 11:29:05 +01002241 * Don't migrate file pages that are mapped in multiple processes
2242 * with execute permissions as they are probably shared libraries.
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002243 */
Miaohe Lin7ee820e2021-05-04 18:37:16 -07002244 if (page_mapcount(page) != 1 && page_is_file_lru(page) &&
2245 (vma->vm_flags & VM_EXEC))
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002246 goto out;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002247
Mel Gormana8f60772012-11-14 21:41:46 +00002248 /*
Mel Gorman09a913a2018-04-10 16:29:20 -07002249 * Also do not migrate dirty pages as not all filesystems can move
2250 * dirty pages in MIGRATE_ASYNC mode which is a waste of cycles.
2251 */
Huang Ying9de4f222020-04-06 20:04:41 -07002252 if (page_is_file_lru(page) && PageDirty(page))
Mel Gorman09a913a2018-04-10 16:29:20 -07002253 goto out;
2254
Mel Gormanb32967f2012-11-19 12:35:47 +00002255 isolated = numamigrate_isolate_page(pgdat, page);
2256 if (!isolated)
2257 goto out;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002258
Mel Gormanb32967f2012-11-19 12:35:47 +00002259 list_add(&page->lru, &migratepages);
Yang Shic5b5a3d2021-06-30 18:51:42 -07002260 nr_remaining = migrate_pages(&migratepages, *new, NULL, node,
Yang Shi5ac95882021-09-02 14:59:13 -07002261 MIGRATE_ASYNC, MR_NUMA_MISPLACED, NULL);
Mel Gormanb32967f2012-11-19 12:35:47 +00002262 if (nr_remaining) {
Joonsoo Kim59c82b72014-01-21 15:51:17 -08002263 if (!list_empty(&migratepages)) {
2264 list_del(&page->lru);
Yang Shic5fc5c32021-06-30 18:51:45 -07002265 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
2266 page_is_file_lru(page), -nr_pages);
Joonsoo Kim59c82b72014-01-21 15:51:17 -08002267 putback_lru_page(page);
2268 }
Mel Gormanb32967f2012-11-19 12:35:47 +00002269 isolated = 0;
2270 } else
Yang Shic5fc5c32021-06-30 18:51:45 -07002271 count_vm_numa_events(NUMA_PAGE_MIGRATE, nr_pages);
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002272 BUG_ON(!list_empty(&migratepages));
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002273 return isolated;
Hugh Dickins340ef392013-02-22 16:34:33 -08002274
2275out:
2276 put_page(page);
2277 return 0;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002278}
Mel Gorman220018d2012-12-05 09:32:56 +00002279#endif /* CONFIG_NUMA_BALANCING */
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002280#endif /* CONFIG_NUMA */
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002281
Christoph Hellwig9b2ed9c2019-08-14 09:59:28 +02002282#ifdef CONFIG_DEVICE_PRIVATE
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002283static int migrate_vma_collect_skip(unsigned long start,
2284 unsigned long end,
2285 struct mm_walk *walk)
2286{
2287 struct migrate_vma *migrate = walk->private;
2288 unsigned long addr;
2289
Ralph Campbell872ea702020-01-30 22:14:38 -08002290 for (addr = start; addr < end; addr += PAGE_SIZE) {
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002291 migrate->dst[migrate->npages] = 0;
2292 migrate->src[migrate->npages++] = 0;
2293 }
2294
2295 return 0;
2296}
2297
Miaohe Lin843e1be2021-05-04 18:37:13 -07002298static int migrate_vma_collect_hole(unsigned long start,
2299 unsigned long end,
2300 __always_unused int depth,
2301 struct mm_walk *walk)
2302{
2303 struct migrate_vma *migrate = walk->private;
2304 unsigned long addr;
2305
2306 /* Only allow populating anonymous memory. */
2307 if (!vma_is_anonymous(walk->vma))
2308 return migrate_vma_collect_skip(start, end, walk);
2309
2310 for (addr = start; addr < end; addr += PAGE_SIZE) {
2311 migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE;
2312 migrate->dst[migrate->npages] = 0;
2313 migrate->npages++;
2314 migrate->cpages++;
2315 }
2316
2317 return 0;
2318}
2319
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002320static int migrate_vma_collect_pmd(pmd_t *pmdp,
2321 unsigned long start,
2322 unsigned long end,
2323 struct mm_walk *walk)
2324{
2325 struct migrate_vma *migrate = walk->private;
2326 struct vm_area_struct *vma = walk->vma;
2327 struct mm_struct *mm = vma->vm_mm;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002328 unsigned long addr = start, unmapped = 0;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002329 spinlock_t *ptl;
2330 pte_t *ptep;
2331
2332again:
2333 if (pmd_none(*pmdp))
Steven Priceb7a16c72020-02-03 17:36:03 -08002334 return migrate_vma_collect_hole(start, end, -1, walk);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002335
2336 if (pmd_trans_huge(*pmdp)) {
2337 struct page *page;
2338
2339 ptl = pmd_lock(mm, pmdp);
2340 if (unlikely(!pmd_trans_huge(*pmdp))) {
2341 spin_unlock(ptl);
2342 goto again;
2343 }
2344
2345 page = pmd_page(*pmdp);
2346 if (is_huge_zero_page(page)) {
2347 spin_unlock(ptl);
2348 split_huge_pmd(vma, pmdp, addr);
2349 if (pmd_trans_unstable(pmdp))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002350 return migrate_vma_collect_skip(start, end,
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002351 walk);
2352 } else {
2353 int ret;
2354
2355 get_page(page);
2356 spin_unlock(ptl);
2357 if (unlikely(!trylock_page(page)))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002358 return migrate_vma_collect_skip(start, end,
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002359 walk);
2360 ret = split_huge_page(page);
2361 unlock_page(page);
2362 put_page(page);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002363 if (ret)
2364 return migrate_vma_collect_skip(start, end,
2365 walk);
2366 if (pmd_none(*pmdp))
Steven Priceb7a16c72020-02-03 17:36:03 -08002367 return migrate_vma_collect_hole(start, end, -1,
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002368 walk);
2369 }
2370 }
2371
2372 if (unlikely(pmd_bad(*pmdp)))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002373 return migrate_vma_collect_skip(start, end, walk);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002374
2375 ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002376 arch_enter_lazy_mmu_mode();
2377
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002378 for (; addr < end; addr += PAGE_SIZE, ptep++) {
Christoph Hellwig800bb1c2020-03-16 20:32:14 +01002379 unsigned long mpfn = 0, pfn;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002380 struct page *page;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002381 swp_entry_t entry;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002382 pte_t pte;
2383
2384 pte = *ptep;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002385
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002386 if (pte_none(pte)) {
Ralph Campbell0744f282020-08-11 18:31:41 -07002387 if (vma_is_anonymous(vma)) {
2388 mpfn = MIGRATE_PFN_MIGRATE;
2389 migrate->cpages++;
2390 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002391 goto next;
2392 }
2393
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002394 if (!pte_present(pte)) {
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002395 /*
2396 * Only care about unaddressable device page special
2397 * page table entry. Other special swap entries are not
2398 * migratable, and we ignore regular swapped page.
2399 */
2400 entry = pte_to_swp_entry(pte);
2401 if (!is_device_private_entry(entry))
2402 goto next;
2403
Alistair Poppleaf5cdaf2021-06-30 18:54:06 -07002404 page = pfn_swap_entry_to_page(entry);
Ralph Campbell51431922020-07-23 15:30:00 -07002405 if (!(migrate->flags &
2406 MIGRATE_VMA_SELECT_DEVICE_PRIVATE) ||
2407 page->pgmap->owner != migrate->pgmap_owner)
Christoph Hellwig800bb1c2020-03-16 20:32:14 +01002408 goto next;
2409
Christoph Hellwig06d462b2019-08-14 09:59:27 +02002410 mpfn = migrate_pfn(page_to_pfn(page)) |
2411 MIGRATE_PFN_MIGRATE;
Alistair Popple4dd845b2021-06-30 18:54:09 -07002412 if (is_writable_device_private_entry(entry))
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002413 mpfn |= MIGRATE_PFN_WRITE;
2414 } else {
Ralph Campbell51431922020-07-23 15:30:00 -07002415 if (!(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM))
Christoph Hellwig800bb1c2020-03-16 20:32:14 +01002416 goto next;
Pingfan Liu276f7562019-09-23 15:37:38 -07002417 pfn = pte_pfn(pte);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002418 if (is_zero_pfn(pfn)) {
2419 mpfn = MIGRATE_PFN_MIGRATE;
2420 migrate->cpages++;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002421 goto next;
2422 }
Christoph Hellwig25b29952019-06-13 22:50:49 +02002423 page = vm_normal_page(migrate->vma, addr, pte);
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002424 mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
2425 mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0;
2426 }
2427
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002428 /* FIXME support THP */
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002429 if (!page || !page->mapping || PageTransCompound(page)) {
Pingfan Liu276f7562019-09-23 15:37:38 -07002430 mpfn = 0;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002431 goto next;
2432 }
2433
2434 /*
2435 * By getting a reference on the page we pin it and that blocks
2436 * any kind of migration. Side effect is that it "freezes" the
2437 * pte.
2438 *
2439 * We drop this reference after isolating the page from the lru
2440 * for non device page (device page are not on the lru and thus
2441 * can't be dropped from it).
2442 */
2443 get_page(page);
2444 migrate->cpages++;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002445
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002446 /*
2447 * Optimize for the common case where page is only mapped once
2448 * in one process. If we can lock the page, then we can safely
2449 * set up a special migration page table entry now.
2450 */
2451 if (trylock_page(page)) {
2452 pte_t swp_pte;
2453
2454 mpfn |= MIGRATE_PFN_LOCKED;
2455 ptep_get_and_clear(mm, addr, ptep);
2456
2457 /* Setup special migration page table entry */
Alistair Popple4dd845b2021-06-30 18:54:09 -07002458 if (mpfn & MIGRATE_PFN_WRITE)
2459 entry = make_writable_migration_entry(
2460 page_to_pfn(page));
2461 else
2462 entry = make_readable_migration_entry(
2463 page_to_pfn(page));
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002464 swp_pte = swp_entry_to_pte(entry);
Alistair Popplead7df762020-09-04 16:36:01 -07002465 if (pte_present(pte)) {
2466 if (pte_soft_dirty(pte))
2467 swp_pte = pte_swp_mksoft_dirty(swp_pte);
2468 if (pte_uffd_wp(pte))
2469 swp_pte = pte_swp_mkuffd_wp(swp_pte);
2470 } else {
2471 if (pte_swp_soft_dirty(pte))
2472 swp_pte = pte_swp_mksoft_dirty(swp_pte);
2473 if (pte_swp_uffd_wp(pte))
2474 swp_pte = pte_swp_mkuffd_wp(swp_pte);
2475 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002476 set_pte_at(mm, addr, ptep, swp_pte);
2477
2478 /*
2479 * This is like regular unmap: we remove the rmap and
2480 * drop page refcount. Page won't be freed, as we took
2481 * a reference just above.
2482 */
2483 page_remove_rmap(page, false);
2484 put_page(page);
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002485
2486 if (pte_present(pte))
2487 unmapped++;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002488 }
2489
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002490next:
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002491 migrate->dst[migrate->npages] = 0;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002492 migrate->src[migrate->npages++] = mpfn;
2493 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002494
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002495 /* Only flush the TLB if we actually modified any entries */
2496 if (unmapped)
2497 flush_tlb_range(walk->vma, start, end);
2498
Alistair Popple1299c112022-09-02 10:35:51 +10002499 arch_leave_lazy_mmu_mode();
2500 pte_unmap_unlock(ptep - 1, ptl);
2501
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002502 return 0;
2503}
2504
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02002505static const struct mm_walk_ops migrate_vma_walk_ops = {
2506 .pmd_entry = migrate_vma_collect_pmd,
2507 .pte_hole = migrate_vma_collect_hole,
2508};
2509
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002510/*
2511 * migrate_vma_collect() - collect pages over a range of virtual addresses
2512 * @migrate: migrate struct containing all migration information
2513 *
2514 * This will walk the CPU page table. For each virtual address backed by a
2515 * valid page, it updates the src array and takes a reference on the page, in
2516 * order to pin the page until we lock it and unmap it.
2517 */
2518static void migrate_vma_collect(struct migrate_vma *migrate)
2519{
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002520 struct mmu_notifier_range range;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002521
Ralph Campbell998427b2020-07-23 15:30:01 -07002522 /*
2523 * Note that the pgmap_owner is passed to the mmu notifier callback so
2524 * that the registered device driver can skip invalidating device
2525 * private page mappings that won't be migrated.
2526 */
Alistair Popple6b49bf62021-06-30 18:54:19 -07002527 mmu_notifier_range_init_owner(&range, MMU_NOTIFY_MIGRATE, 0,
2528 migrate->vma, migrate->vma->vm_mm, migrate->start, migrate->end,
Ralph Campbellc1a06df2020-08-06 23:17:09 -07002529 migrate->pgmap_owner);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002530 mmu_notifier_invalidate_range_start(&range);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002531
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02002532 walk_page_range(migrate->vma->vm_mm, migrate->start, migrate->end,
2533 &migrate_vma_walk_ops, migrate);
2534
2535 mmu_notifier_invalidate_range_end(&range);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002536 migrate->end = migrate->start + (migrate->npages << PAGE_SHIFT);
2537}
2538
2539/*
2540 * migrate_vma_check_page() - check if page is pinned or not
2541 * @page: struct page to check
2542 *
2543 * Pinned pages cannot be migrated. This is the same test as in
2544 * migrate_page_move_mapping(), except that here we allow migration of a
2545 * ZONE_DEVICE page.
2546 */
2547static bool migrate_vma_check_page(struct page *page)
2548{
2549 /*
2550 * One extra ref because caller holds an extra reference, either from
2551 * isolate_lru_page() for a regular page, or migrate_vma_collect() for
2552 * a device page.
2553 */
2554 int extra = 1;
2555
2556 /*
2557 * FIXME support THP (transparent huge page), it is bit more complex to
2558 * check them than regular pages, because they can be mapped with a pmd
2559 * or with a pte (split pte mapping).
2560 */
2561 if (PageCompound(page))
2562 return false;
2563
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002564 /* Page from ZONE_DEVICE have one extra reference */
2565 if (is_zone_device_page(page)) {
2566 /*
2567 * Private page can never be pin as they have no valid pte and
2568 * GUP will fail for those. Yet if there is a pending migration
2569 * a thread might try to wait on the pte migration entry and
2570 * will bump the page reference count. Sadly there is no way to
2571 * differentiate a regular pin from migration wait. Hence to
2572 * avoid 2 racing thread trying to migrate back to CPU to enter
Haitao Shi8958b242020-12-15 20:47:26 -08002573 * infinite loop (one stopping migration because the other is
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002574 * waiting on pte migration entry). We always return true here.
2575 *
2576 * FIXME proper solution is to rework migration_entry_wait() so
2577 * it does not need to take a reference on page.
2578 */
Christoph Hellwig25b29952019-06-13 22:50:49 +02002579 return is_device_private_page(page);
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002580 }
2581
Jérôme Glissedf6ad692017-09-08 16:12:24 -07002582 /* For file back page */
2583 if (page_mapping(page))
2584 extra += 1 + page_has_private(page);
2585
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002586 if ((page_count(page) - extra) > page_mapcount(page))
2587 return false;
2588
2589 return true;
2590}
2591
2592/*
2593 * migrate_vma_prepare() - lock pages and isolate them from the lru
2594 * @migrate: migrate struct containing all migration information
2595 *
2596 * This locks pages that have been collected by migrate_vma_collect(). Once each
2597 * page is locked it is isolated from the lru (for non-device pages). Finally,
2598 * the ref taken by migrate_vma_collect() is dropped, as locked pages cannot be
2599 * migrated by concurrent kernel threads.
2600 */
2601static void migrate_vma_prepare(struct migrate_vma *migrate)
2602{
2603 const unsigned long npages = migrate->npages;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002604 const unsigned long start = migrate->start;
2605 unsigned long addr, i, restore = 0;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002606 bool allow_drain = true;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002607
2608 lru_add_drain();
2609
2610 for (i = 0; (i < npages) && migrate->cpages; i++) {
2611 struct page *page = migrate_pfn_to_page(migrate->src[i]);
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002612 bool remap = true;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002613
2614 if (!page)
2615 continue;
2616
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002617 if (!(migrate->src[i] & MIGRATE_PFN_LOCKED)) {
2618 /*
2619 * Because we are migrating several pages there can be
2620 * a deadlock between 2 concurrent migration where each
2621 * are waiting on each other page lock.
2622 *
2623 * Make migrate_vma() a best effort thing and backoff
2624 * for any page we can not lock right away.
2625 */
2626 if (!trylock_page(page)) {
2627 migrate->src[i] = 0;
2628 migrate->cpages--;
2629 put_page(page);
2630 continue;
2631 }
2632 remap = false;
2633 migrate->src[i] |= MIGRATE_PFN_LOCKED;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002634 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002635
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002636 /* ZONE_DEVICE pages are not on LRU */
2637 if (!is_zone_device_page(page)) {
2638 if (!PageLRU(page) && allow_drain) {
2639 /* Drain CPU's pagevec */
2640 lru_add_drain_all();
2641 allow_drain = false;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002642 }
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002643
2644 if (isolate_lru_page(page)) {
2645 if (remap) {
2646 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2647 migrate->cpages--;
2648 restore++;
2649 } else {
2650 migrate->src[i] = 0;
2651 unlock_page(page);
2652 migrate->cpages--;
2653 put_page(page);
2654 }
2655 continue;
2656 }
2657
2658 /* Drop the reference we took in collect */
2659 put_page(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002660 }
2661
2662 if (!migrate_vma_check_page(page)) {
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002663 if (remap) {
2664 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2665 migrate->cpages--;
2666 restore++;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002667
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002668 if (!is_zone_device_page(page)) {
2669 get_page(page);
2670 putback_lru_page(page);
2671 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002672 } else {
2673 migrate->src[i] = 0;
2674 unlock_page(page);
2675 migrate->cpages--;
2676
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002677 if (!is_zone_device_page(page))
2678 putback_lru_page(page);
2679 else
2680 put_page(page);
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002681 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002682 }
2683 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002684
2685 for (i = 0, addr = start; i < npages && restore; i++, addr += PAGE_SIZE) {
2686 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2687
2688 if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE))
2689 continue;
2690
2691 remove_migration_pte(page, migrate->vma, addr, page);
2692
2693 migrate->src[i] = 0;
2694 unlock_page(page);
2695 put_page(page);
2696 restore--;
2697 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002698}
2699
2700/*
2701 * migrate_vma_unmap() - replace page mapping with special migration pte entry
2702 * @migrate: migrate struct containing all migration information
2703 *
2704 * Replace page mapping (CPU page table pte) with a special migration pte entry
2705 * and check again if it has been pinned. Pinned pages are restored because we
2706 * cannot migrate them.
2707 *
2708 * This is the last step before we call the device driver callback to allocate
2709 * destination memory and copy contents of original page over to new page.
2710 */
2711static void migrate_vma_unmap(struct migrate_vma *migrate)
2712{
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002713 const unsigned long npages = migrate->npages;
2714 const unsigned long start = migrate->start;
2715 unsigned long addr, i, restore = 0;
2716
2717 for (i = 0; i < npages; i++) {
2718 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2719
2720 if (!page || !(migrate->src[i] & MIGRATE_PFN_MIGRATE))
2721 continue;
2722
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002723 if (page_mapped(page)) {
Alistair Popplea98a2f02021-06-30 18:54:16 -07002724 try_to_migrate(page, 0);
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002725 if (page_mapped(page))
2726 goto restore;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002727 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002728
2729 if (migrate_vma_check_page(page))
2730 continue;
2731
2732restore:
2733 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2734 migrate->cpages--;
2735 restore++;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002736 }
2737
2738 for (addr = start, i = 0; i < npages && restore; addr += PAGE_SIZE, i++) {
2739 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2740
2741 if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE))
2742 continue;
2743
2744 remove_migration_ptes(page, page, false);
2745
2746 migrate->src[i] = 0;
2747 unlock_page(page);
2748 restore--;
2749
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002750 if (is_zone_device_page(page))
2751 put_page(page);
2752 else
2753 putback_lru_page(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002754 }
2755}
2756
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002757/**
2758 * migrate_vma_setup() - prepare to migrate a range of memory
Randy Dunlapeaf444d2020-08-11 18:33:08 -07002759 * @args: contains the vma, start, and pfns arrays for the migration
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002760 *
2761 * Returns: negative errno on failures, 0 when 0 or more pages were migrated
2762 * without an error.
2763 *
2764 * Prepare to migrate a range of memory virtual address range by collecting all
2765 * the pages backing each virtual address in the range, saving them inside the
2766 * src array. Then lock those pages and unmap them. Once the pages are locked
2767 * and unmapped, check whether each page is pinned or not. Pages that aren't
2768 * pinned have the MIGRATE_PFN_MIGRATE flag set (by this function) in the
2769 * corresponding src array entry. Then restores any pages that are pinned, by
2770 * remapping and unlocking those pages.
2771 *
2772 * The caller should then allocate destination memory and copy source memory to
2773 * it for all those entries (ie with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE
2774 * flag set). Once these are allocated and copied, the caller must update each
2775 * corresponding entry in the dst array with the pfn value of the destination
2776 * page and with the MIGRATE_PFN_VALID and MIGRATE_PFN_LOCKED flags set
2777 * (destination pages must have their struct pages locked, via lock_page()).
2778 *
2779 * Note that the caller does not have to migrate all the pages that are marked
2780 * with MIGRATE_PFN_MIGRATE flag in src array unless this is a migration from
2781 * device memory to system memory. If the caller cannot migrate a device page
2782 * back to system memory, then it must return VM_FAULT_SIGBUS, which has severe
2783 * consequences for the userspace process, so it must be avoided if at all
2784 * possible.
2785 *
2786 * For empty entries inside CPU page table (pte_none() or pmd_none() is true) we
2787 * do set MIGRATE_PFN_MIGRATE flag inside the corresponding source array thus
Ingo Molnarf0953a12021-05-06 18:06:47 -07002788 * allowing the caller to allocate device memory for those unbacked virtual
2789 * addresses. For this the caller simply has to allocate device memory and
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002790 * properly set the destination entry like for regular migration. Note that
Ingo Molnarf0953a12021-05-06 18:06:47 -07002791 * this can still fail, and thus inside the device driver you must check if the
2792 * migration was successful for those entries after calling migrate_vma_pages(),
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002793 * just like for regular migration.
2794 *
2795 * After that, the callers must call migrate_vma_pages() to go over each entry
2796 * in the src array that has the MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag
2797 * set. If the corresponding entry in dst array has MIGRATE_PFN_VALID flag set,
2798 * then migrate_vma_pages() to migrate struct page information from the source
2799 * struct page to the destination struct page. If it fails to migrate the
2800 * struct page information, then it clears the MIGRATE_PFN_MIGRATE flag in the
2801 * src array.
2802 *
2803 * At this point all successfully migrated pages have an entry in the src
2804 * array with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag set and the dst
2805 * array entry with MIGRATE_PFN_VALID flag set.
2806 *
2807 * Once migrate_vma_pages() returns the caller may inspect which pages were
2808 * successfully migrated, and which were not. Successfully migrated pages will
2809 * have the MIGRATE_PFN_MIGRATE flag set for their src array entry.
2810 *
2811 * It is safe to update device page table after migrate_vma_pages() because
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002812 * both destination and source page are still locked, and the mmap_lock is held
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002813 * in read mode (hence no one can unmap the range being migrated).
2814 *
2815 * Once the caller is done cleaning up things and updating its page table (if it
2816 * chose to do so, this is not an obligation) it finally calls
2817 * migrate_vma_finalize() to update the CPU page table to point to new pages
2818 * for successfully migrated pages or otherwise restore the CPU page table to
2819 * point to the original source pages.
2820 */
2821int migrate_vma_setup(struct migrate_vma *args)
2822{
2823 long nr_pages = (args->end - args->start) >> PAGE_SHIFT;
2824
2825 args->start &= PAGE_MASK;
2826 args->end &= PAGE_MASK;
2827 if (!args->vma || is_vm_hugetlb_page(args->vma) ||
2828 (args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma))
2829 return -EINVAL;
2830 if (nr_pages <= 0)
2831 return -EINVAL;
2832 if (args->start < args->vma->vm_start ||
2833 args->start >= args->vma->vm_end)
2834 return -EINVAL;
2835 if (args->end <= args->vma->vm_start || args->end > args->vma->vm_end)
2836 return -EINVAL;
2837 if (!args->src || !args->dst)
2838 return -EINVAL;
2839
2840 memset(args->src, 0, sizeof(*args->src) * nr_pages);
2841 args->cpages = 0;
2842 args->npages = 0;
2843
2844 migrate_vma_collect(args);
2845
2846 if (args->cpages)
2847 migrate_vma_prepare(args);
2848 if (args->cpages)
2849 migrate_vma_unmap(args);
2850
2851 /*
2852 * At this point pages are locked and unmapped, and thus they have
2853 * stable content and can safely be copied to destination memory that
2854 * is allocated by the drivers.
2855 */
2856 return 0;
2857
2858}
2859EXPORT_SYMBOL(migrate_vma_setup);
2860
Ralph Campbell34290e22020-01-30 22:14:44 -08002861/*
2862 * This code closely matches the code in:
2863 * __handle_mm_fault()
2864 * handle_pte_fault()
2865 * do_anonymous_page()
2866 * to map in an anonymous zero page but the struct page will be a ZONE_DEVICE
2867 * private page.
2868 */
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002869static void migrate_vma_insert_page(struct migrate_vma *migrate,
2870 unsigned long addr,
2871 struct page *page,
Stephen Zhangd85c6db2020-12-14 19:13:20 -08002872 unsigned long *src)
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002873{
2874 struct vm_area_struct *vma = migrate->vma;
2875 struct mm_struct *mm = vma->vm_mm;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002876 bool flush = false;
2877 spinlock_t *ptl;
2878 pte_t entry;
2879 pgd_t *pgdp;
2880 p4d_t *p4dp;
2881 pud_t *pudp;
2882 pmd_t *pmdp;
2883 pte_t *ptep;
2884
2885 /* Only allow populating anonymous memory */
2886 if (!vma_is_anonymous(vma))
2887 goto abort;
2888
2889 pgdp = pgd_offset(mm, addr);
2890 p4dp = p4d_alloc(mm, pgdp, addr);
2891 if (!p4dp)
2892 goto abort;
2893 pudp = pud_alloc(mm, p4dp, addr);
2894 if (!pudp)
2895 goto abort;
2896 pmdp = pmd_alloc(mm, pudp, addr);
2897 if (!pmdp)
2898 goto abort;
2899
2900 if (pmd_trans_huge(*pmdp) || pmd_devmap(*pmdp))
2901 goto abort;
2902
2903 /*
2904 * Use pte_alloc() instead of pte_alloc_map(). We can't run
2905 * pte_offset_map() on pmds where a huge pmd might be created
2906 * from a different thread.
2907 *
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002908 * pte_alloc_map() is safe to use under mmap_write_lock(mm) or when
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002909 * parallel threads are excluded by other means.
2910 *
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002911 * Here we only have mmap_read_lock(mm).
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002912 */
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -08002913 if (pte_alloc(mm, pmdp))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002914 goto abort;
2915
2916 /* See the comment in pte_alloc_one_map() */
2917 if (unlikely(pmd_trans_unstable(pmdp)))
2918 goto abort;
2919
2920 if (unlikely(anon_vma_prepare(vma)))
2921 goto abort;
Johannes Weinerd9eb1ea2020-06-03 16:02:24 -07002922 if (mem_cgroup_charge(page, vma->vm_mm, GFP_KERNEL))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002923 goto abort;
2924
2925 /*
2926 * The memory barrier inside __SetPageUptodate makes sure that
2927 * preceding stores to the page contents become visible before
2928 * the set_pte_at() write.
2929 */
2930 __SetPageUptodate(page);
2931
Jérôme Glissedf6ad692017-09-08 16:12:24 -07002932 if (is_zone_device_page(page)) {
2933 if (is_device_private_page(page)) {
2934 swp_entry_t swp_entry;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002935
Alistair Popple4dd845b2021-06-30 18:54:09 -07002936 if (vma->vm_flags & VM_WRITE)
2937 swp_entry = make_writable_device_private_entry(
2938 page_to_pfn(page));
2939 else
2940 swp_entry = make_readable_device_private_entry(
2941 page_to_pfn(page));
Jérôme Glissedf6ad692017-09-08 16:12:24 -07002942 entry = swp_entry_to_pte(swp_entry);
Miaohe Lin34f5e9b2021-05-04 18:37:10 -07002943 } else {
2944 /*
2945 * For now we only support migrating to un-addressable
2946 * device memory.
2947 */
2948 pr_warn_once("Unsupported ZONE_DEVICE page type.\n");
2949 goto abort;
Jérôme Glissedf6ad692017-09-08 16:12:24 -07002950 }
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002951 } else {
2952 entry = mk_pte(page, vma->vm_page_prot);
2953 if (vma->vm_flags & VM_WRITE)
2954 entry = pte_mkwrite(pte_mkdirty(entry));
2955 }
2956
2957 ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
2958
Ralph Campbell34290e22020-01-30 22:14:44 -08002959 if (check_stable_address_space(mm))
2960 goto unlock_abort;
2961
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002962 if (pte_present(*ptep)) {
2963 unsigned long pfn = pte_pfn(*ptep);
2964
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002965 if (!is_zero_pfn(pfn))
2966 goto unlock_abort;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002967 flush = true;
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002968 } else if (!pte_none(*ptep))
2969 goto unlock_abort;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002970
2971 /*
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002972 * Check for userfaultfd but do not deliver the fault. Instead,
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002973 * just back off.
2974 */
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002975 if (userfaultfd_missing(vma))
2976 goto unlock_abort;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002977
2978 inc_mm_counter(mm, MM_ANONPAGES);
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07002979 page_add_new_anon_rmap(page, vma, addr, false);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002980 if (!is_zone_device_page(page))
Joonsoo Kimb5181542020-08-11 18:30:40 -07002981 lru_cache_add_inactive_or_unevictable(page, vma);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002982 get_page(page);
2983
2984 if (flush) {
2985 flush_cache_page(vma, addr, pte_pfn(*ptep));
2986 ptep_clear_flush_notify(vma, addr, ptep);
2987 set_pte_at_notify(mm, addr, ptep, entry);
2988 update_mmu_cache(vma, addr, ptep);
2989 } else {
2990 /* No need to invalidate - it was non-present before */
2991 set_pte_at(mm, addr, ptep, entry);
2992 update_mmu_cache(vma, addr, ptep);
2993 }
2994
2995 pte_unmap_unlock(ptep, ptl);
2996 *src = MIGRATE_PFN_MIGRATE;
2997 return;
2998
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002999unlock_abort:
3000 pte_unmap_unlock(ptep, ptl);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003001abort:
3002 *src &= ~MIGRATE_PFN_MIGRATE;
3003}
3004
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003005/**
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003006 * migrate_vma_pages() - migrate meta-data from src page to dst page
3007 * @migrate: migrate struct containing all migration information
3008 *
3009 * This migrates struct page meta-data from source struct page to destination
3010 * struct page. This effectively finishes the migration from source page to the
3011 * destination page.
3012 */
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003013void migrate_vma_pages(struct migrate_vma *migrate)
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003014{
3015 const unsigned long npages = migrate->npages;
3016 const unsigned long start = migrate->start;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003017 struct mmu_notifier_range range;
3018 unsigned long addr, i;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003019 bool notified = false;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003020
3021 for (i = 0, addr = start; i < npages; addr += PAGE_SIZE, i++) {
3022 struct page *newpage = migrate_pfn_to_page(migrate->dst[i]);
3023 struct page *page = migrate_pfn_to_page(migrate->src[i]);
3024 struct address_space *mapping;
3025 int r;
3026
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003027 if (!newpage) {
3028 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003029 continue;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003030 }
3031
3032 if (!page) {
Ralph Campbellc23a0c92020-01-30 22:14:41 -08003033 if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003034 continue;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003035 if (!notified) {
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003036 notified = true;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003037
Alistair Popple6b49bf62021-06-30 18:54:19 -07003038 mmu_notifier_range_init_owner(&range,
3039 MMU_NOTIFY_MIGRATE, 0, migrate->vma,
3040 migrate->vma->vm_mm, addr, migrate->end,
Ralph Campbell5e5dda82020-12-14 19:12:55 -08003041 migrate->pgmap_owner);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003042 mmu_notifier_invalidate_range_start(&range);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003043 }
3044 migrate_vma_insert_page(migrate, addr, newpage,
Stephen Zhangd85c6db2020-12-14 19:13:20 -08003045 &migrate->src[i]);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003046 continue;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003047 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003048
3049 mapping = page_mapping(page);
3050
Jérôme Glissea5430dd2017-09-08 16:12:17 -07003051 if (is_zone_device_page(newpage)) {
3052 if (is_device_private_page(newpage)) {
3053 /*
3054 * For now only support private anonymous when
3055 * migrating to un-addressable device memory.
3056 */
3057 if (mapping) {
3058 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
3059 continue;
3060 }
Christoph Hellwig25b29952019-06-13 22:50:49 +02003061 } else {
Jérôme Glissea5430dd2017-09-08 16:12:17 -07003062 /*
3063 * Other types of ZONE_DEVICE page are not
3064 * supported.
3065 */
3066 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
3067 continue;
3068 }
3069 }
3070
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003071 r = migrate_page(mapping, newpage, page, MIGRATE_SYNC_NO_COPY);
3072 if (r != MIGRATEPAGE_SUCCESS)
3073 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
3074 }
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003075
Jérôme Glisse4645b9f2017-11-15 17:34:11 -08003076 /*
3077 * No need to double call mmu_notifier->invalidate_range() callback as
3078 * the above ptep_clear_flush_notify() inside migrate_vma_insert_page()
3079 * did already call it.
3080 */
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003081 if (notified)
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003082 mmu_notifier_invalidate_range_only_end(&range);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003083}
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003084EXPORT_SYMBOL(migrate_vma_pages);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003085
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003086/**
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003087 * migrate_vma_finalize() - restore CPU page table entry
3088 * @migrate: migrate struct containing all migration information
3089 *
3090 * This replaces the special migration pte entry with either a mapping to the
3091 * new page if migration was successful for that page, or to the original page
3092 * otherwise.
3093 *
3094 * This also unlocks the pages and puts them back on the lru, or drops the extra
3095 * refcount, for device pages.
3096 */
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003097void migrate_vma_finalize(struct migrate_vma *migrate)
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003098{
3099 const unsigned long npages = migrate->npages;
3100 unsigned long i;
3101
3102 for (i = 0; i < npages; i++) {
3103 struct page *newpage = migrate_pfn_to_page(migrate->dst[i]);
3104 struct page *page = migrate_pfn_to_page(migrate->src[i]);
3105
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003106 if (!page) {
3107 if (newpage) {
3108 unlock_page(newpage);
3109 put_page(newpage);
3110 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003111 continue;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003112 }
3113
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003114 if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE) || !newpage) {
3115 if (newpage) {
3116 unlock_page(newpage);
3117 put_page(newpage);
3118 }
3119 newpage = page;
3120 }
3121
3122 remove_migration_ptes(page, newpage, false);
3123 unlock_page(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003124
Jérôme Glissea5430dd2017-09-08 16:12:17 -07003125 if (is_zone_device_page(page))
3126 put_page(page);
3127 else
3128 putback_lru_page(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003129
3130 if (newpage != page) {
3131 unlock_page(newpage);
Jérôme Glissea5430dd2017-09-08 16:12:17 -07003132 if (is_zone_device_page(newpage))
3133 put_page(newpage);
3134 else
3135 putback_lru_page(newpage);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003136 }
3137 }
3138}
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003139EXPORT_SYMBOL(migrate_vma_finalize);
Christoph Hellwig9b2ed9c2019-08-14 09:59:28 +02003140#endif /* CONFIG_DEVICE_PRIVATE */
Dave Hansen79c28a42021-09-02 14:59:06 -07003141
Dave Hansen76af6a02021-10-18 15:15:32 -07003142#if defined(CONFIG_HOTPLUG_CPU)
Dave Hansen79c28a42021-09-02 14:59:06 -07003143/* Disable reclaim-based migration. */
3144static void __disable_all_migrate_targets(void)
3145{
3146 int node;
3147
3148 for_each_online_node(node)
3149 node_demotion[node] = NUMA_NO_NODE;
3150}
3151
3152static void disable_all_migrate_targets(void)
3153{
3154 __disable_all_migrate_targets();
3155
3156 /*
3157 * Ensure that the "disable" is visible across the system.
3158 * Readers will see either a combination of before+disable
3159 * state or disable+after. They will never see before and
3160 * after state together.
3161 *
3162 * The before+after state together might have cycles and
3163 * could cause readers to do things like loop until this
3164 * function finishes. This ensures they can only see a
3165 * single "bad" read and would, for instance, only loop
3166 * once.
3167 */
3168 synchronize_rcu();
3169}
3170
3171/*
3172 * Find an automatic demotion target for 'node'.
3173 * Failing here is OK. It might just indicate
3174 * being at the end of a chain.
3175 */
3176static int establish_migrate_target(int node, nodemask_t *used)
3177{
3178 int migration_target;
3179
3180 /*
3181 * Can not set a migration target on a
3182 * node with it already set.
3183 *
3184 * No need for READ_ONCE() here since this
3185 * in the write path for node_demotion[].
3186 * This should be the only thread writing.
3187 */
3188 if (node_demotion[node] != NUMA_NO_NODE)
3189 return NUMA_NO_NODE;
3190
3191 migration_target = find_next_best_node(node, used);
3192 if (migration_target == NUMA_NO_NODE)
3193 return NUMA_NO_NODE;
3194
3195 node_demotion[node] = migration_target;
3196
3197 return migration_target;
3198}
3199
3200/*
3201 * When memory fills up on a node, memory contents can be
3202 * automatically migrated to another node instead of
3203 * discarded at reclaim.
3204 *
3205 * Establish a "migration path" which will start at nodes
3206 * with CPUs and will follow the priorities used to build the
3207 * page allocator zonelists.
3208 *
3209 * The difference here is that cycles must be avoided. If
3210 * node0 migrates to node1, then neither node1, nor anything
3211 * node1 migrates to can migrate to node0.
3212 *
3213 * This function can run simultaneously with readers of
3214 * node_demotion[]. However, it can not run simultaneously
3215 * with itself. Exclusion is provided by memory hotplug events
3216 * being single-threaded.
3217 */
3218static void __set_migration_target_nodes(void)
3219{
3220 nodemask_t next_pass = NODE_MASK_NONE;
3221 nodemask_t this_pass = NODE_MASK_NONE;
3222 nodemask_t used_targets = NODE_MASK_NONE;
3223 int node;
3224
3225 /*
3226 * Avoid any oddities like cycles that could occur
3227 * from changes in the topology. This will leave
3228 * a momentary gap when migration is disabled.
3229 */
3230 disable_all_migrate_targets();
3231
3232 /*
3233 * Allocations go close to CPUs, first. Assume that
3234 * the migration path starts at the nodes with CPUs.
3235 */
3236 next_pass = node_states[N_CPU];
3237again:
3238 this_pass = next_pass;
3239 next_pass = NODE_MASK_NONE;
3240 /*
3241 * To avoid cycles in the migration "graph", ensure
3242 * that migration sources are not future targets by
3243 * setting them in 'used_targets'. Do this only
3244 * once per pass so that multiple source nodes can
3245 * share a target node.
3246 *
3247 * 'used_targets' will become unavailable in future
3248 * passes. This limits some opportunities for
3249 * multiple source nodes to share a destination.
3250 */
3251 nodes_or(used_targets, used_targets, this_pass);
3252 for_each_node_mask(node, this_pass) {
3253 int target_node = establish_migrate_target(node, &used_targets);
3254
3255 if (target_node == NUMA_NO_NODE)
3256 continue;
3257
3258 /*
3259 * Visit targets from this pass in the next pass.
3260 * Eventually, every node will have been part of
3261 * a pass, and will become set in 'used_targets'.
3262 */
3263 node_set(target_node, next_pass);
3264 }
3265 /*
3266 * 'next_pass' contains nodes which became migration
3267 * targets in this pass. Make additional passes until
3268 * no more migrations targets are available.
3269 */
3270 if (!nodes_empty(next_pass))
3271 goto again;
3272}
3273
3274/*
3275 * For callers that do not hold get_online_mems() already.
3276 */
Dave Hansen79c28a42021-09-02 14:59:06 -07003277static void set_migration_target_nodes(void)
3278{
3279 get_online_mems();
3280 __set_migration_target_nodes();
3281 put_online_mems();
3282}
Dave Hansen884a6e52021-09-02 14:59:09 -07003283
3284/*
Dave Hansen884a6e52021-09-02 14:59:09 -07003285 * This leaves migrate-on-reclaim transiently disabled between
3286 * the MEM_GOING_OFFLINE and MEM_OFFLINE events. This runs
3287 * whether reclaim-based migration is enabled or not, which
3288 * ensures that the user can turn reclaim-based migration at
3289 * any time without needing to recalculate migration targets.
3290 *
3291 * These callbacks already hold get_online_mems(). That is why
3292 * __set_migration_target_nodes() can be used as opposed to
3293 * set_migration_target_nodes().
3294 */
3295static int __meminit migrate_on_reclaim_callback(struct notifier_block *self,
Dave Hansen295be912021-10-18 15:15:29 -07003296 unsigned long action, void *_arg)
Dave Hansen884a6e52021-09-02 14:59:09 -07003297{
Dave Hansen295be912021-10-18 15:15:29 -07003298 struct memory_notify *arg = _arg;
3299
3300 /*
3301 * Only update the node migration order when a node is
3302 * changing status, like online->offline. This avoids
3303 * the overhead of synchronize_rcu() in most cases.
3304 */
3305 if (arg->status_change_nid < 0)
3306 return notifier_from_errno(0);
3307
Dave Hansen884a6e52021-09-02 14:59:09 -07003308 switch (action) {
3309 case MEM_GOING_OFFLINE:
3310 /*
3311 * Make sure there are not transient states where
3312 * an offline node is a migration target. This
3313 * will leave migration disabled until the offline
3314 * completes and the MEM_OFFLINE case below runs.
3315 */
3316 disable_all_migrate_targets();
3317 break;
3318 case MEM_OFFLINE:
3319 case MEM_ONLINE:
3320 /*
3321 * Recalculate the target nodes once the node
3322 * reaches its final state (online or offline).
3323 */
3324 __set_migration_target_nodes();
3325 break;
3326 case MEM_CANCEL_OFFLINE:
3327 /*
3328 * MEM_GOING_OFFLINE disabled all the migration
3329 * targets. Reenable them.
3330 */
3331 __set_migration_target_nodes();
3332 break;
3333 case MEM_GOING_ONLINE:
3334 case MEM_CANCEL_ONLINE:
3335 break;
3336 }
3337
3338 return notifier_from_errno(0);
3339}
3340
Dave Hansen76af6a02021-10-18 15:15:32 -07003341/*
3342 * React to hotplug events that might affect the migration targets
3343 * like events that online or offline NUMA nodes.
3344 *
3345 * The ordering is also currently dependent on which nodes have
3346 * CPUs. That means we need CPU on/offline notification too.
3347 */
3348static int migration_online_cpu(unsigned int cpu)
3349{
3350 set_migration_target_nodes();
3351 return 0;
3352}
3353
3354static int migration_offline_cpu(unsigned int cpu)
3355{
3356 set_migration_target_nodes();
3357 return 0;
3358}
3359
Dave Hansen884a6e52021-09-02 14:59:09 -07003360static int __init migrate_on_reclaim_init(void)
3361{
3362 int ret;
3363
Huang Yinga6a02512021-10-18 15:15:35 -07003364 ret = cpuhp_setup_state_nocalls(CPUHP_MM_DEMOTION_DEAD, "mm/demotion:offline",
3365 NULL, migration_offline_cpu);
Dave Hansen884a6e52021-09-02 14:59:09 -07003366 /*
3367 * In the unlikely case that this fails, the automatic
3368 * migration targets may become suboptimal for nodes
3369 * where N_CPU changes. With such a small impact in a
3370 * rare case, do not bother trying to do anything special.
3371 */
3372 WARN_ON(ret < 0);
Huang Yinga6a02512021-10-18 15:15:35 -07003373 ret = cpuhp_setup_state(CPUHP_AP_MM_DEMOTION_ONLINE, "mm/demotion:online",
3374 migration_online_cpu, NULL);
3375 WARN_ON(ret < 0);
Dave Hansen884a6e52021-09-02 14:59:09 -07003376
3377 hotplug_memory_notifier(migrate_on_reclaim_callback, 100);
3378 return 0;
3379}
3380late_initcall(migrate_on_reclaim_init);
Dave Hansen76af6a02021-10-18 15:15:32 -07003381#endif /* CONFIG_HOTPLUG_CPU */