blob: c89a3271fa9a6e54f76ff1f9fec3747aec90bfcb [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
Christoph Lameterb20a3502006-03-22 00:09:12 -080057
Michal Nazarewicz0d1836c2010-12-21 17:24:26 -080058#include <asm/tlbflush.h>
59
Mel Gorman7b2a2d42012-10-19 14:07:31 +010060#define CREATE_TRACE_POINTS
61#include <trace/events/migrate.h>
62
qinglin.lia1a623f2024-06-21 14:36:34 +080063#undef CREATE_TRACE_POINTS
64#include <trace/hooks/mm.h>
65
Christoph Lameterb20a3502006-03-22 00:09:12 -080066#include "internal.h"
67
Yisheng Xie9e5bcd62017-02-24 14:57:29 -080068int isolate_movable_page(struct page *page, isolate_mode_t mode)
Minchan Kimbda807d2016-07-26 15:23:05 -070069{
70 struct address_space *mapping;
71
72 /*
73 * Avoid burning cycles with pages that are yet under __free_pages(),
74 * or just got freed under us.
75 *
76 * In case we 'win' a race for a movable page being freed under us and
77 * raise its refcount preventing __free_pages() from doing its job
78 * the put_page() at the end of this block will take care of
79 * release this page, thus avoiding a nasty leakage.
80 */
81 if (unlikely(!get_page_unless_zero(page)))
82 goto out;
83
84 /*
85 * Check PageMovable before holding a PG_lock because page's owner
86 * assumes anybody doesn't touch PG_lock of newly allocated page
Wei Yang8bb4e7a2019-03-05 15:46:22 -080087 * so unconditionally grabbing the lock ruins page's owner side.
Minchan Kimbda807d2016-07-26 15:23:05 -070088 */
89 if (unlikely(!__PageMovable(page)))
90 goto out_putpage;
91 /*
92 * As movable pages are not isolated from LRU lists, concurrent
93 * compaction threads can race against page migration functions
94 * as well as race against the releasing a page.
95 *
96 * In order to avoid having an already isolated movable page
97 * being (wrongly) re-isolated while it is under migration,
98 * or to avoid attempting to isolate pages being released,
99 * lets be sure we have the page lock
100 * before proceeding with the movable page isolation steps.
101 */
102 if (unlikely(!trylock_page(page)))
103 goto out_putpage;
104
105 if (!PageMovable(page) || PageIsolated(page))
106 goto out_no_isolated;
107
108 mapping = page_mapping(page);
109 VM_BUG_ON_PAGE(!mapping, page);
110
111 if (!mapping->a_ops->isolate_page(page, mode))
112 goto out_no_isolated;
113
114 /* Driver shouldn't use PG_isolated bit of page->flags */
115 WARN_ON_ONCE(PageIsolated(page));
andrew.yang0d8a8362022-03-15 16:58:34 +1100116 SetPageIsolated(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700117 unlock_page(page);
118
Yisheng Xie9e5bcd62017-02-24 14:57:29 -0800119 return 0;
Minchan Kimbda807d2016-07-26 15:23:05 -0700120
121out_no_isolated:
122 unlock_page(page);
123out_putpage:
124 put_page(page);
125out:
Yisheng Xie9e5bcd62017-02-24 14:57:29 -0800126 return -EBUSY;
Minchan Kimbda807d2016-07-26 15:23:05 -0700127}
128
Miaohe Lin606a6f72021-05-04 18:37:04 -0700129static void putback_movable_page(struct page *page)
Minchan Kimbda807d2016-07-26 15:23:05 -0700130{
131 struct address_space *mapping;
132
Minchan Kimbda807d2016-07-26 15:23:05 -0700133 mapping = page_mapping(page);
134 mapping->a_ops->putback_page(page);
andrew.yang0d8a8362022-03-15 16:58:34 +1100135 ClearPageIsolated(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700136}
137
Christoph Lameterb20a3502006-03-22 00:09:12 -0800138/*
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800139 * Put previously isolated pages back onto the appropriate lists
140 * from where they were once taken off for compaction/migration.
141 *
Joonsoo Kim59c82b72014-01-21 15:51:17 -0800142 * This function shall be used whenever the isolated pageset has been
143 * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range()
Miaohe Lin072e7412022-05-30 19:30:15 +0800144 * and isolate_hugetlb().
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800145 */
146void putback_movable_pages(struct list_head *l)
147{
148 struct page *page;
149 struct page *page2;
150
151 list_for_each_entry_safe(page, page2, l, lru) {
Naoya Horiguchi31caf662013-09-11 14:21:59 -0700152 if (unlikely(PageHuge(page))) {
153 putback_active_hugepage(page);
154 continue;
155 }
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800156 list_del(&page->lru);
Minchan Kimbda807d2016-07-26 15:23:05 -0700157 /*
158 * We isolated non-lru movable page so here we can use
159 * __PageMovable because LRU page's mapping cannot have
160 * PAGE_MAPPING_MOVABLE.
161 */
Minchan Kimb1123ea62016-07-26 15:23:09 -0700162 if (unlikely(__PageMovable(page))) {
Minchan Kimbda807d2016-07-26 15:23:05 -0700163 VM_BUG_ON_PAGE(!PageIsolated(page), page);
164 lock_page(page);
165 if (PageMovable(page))
166 putback_movable_page(page);
167 else
andrew.yang0d8a8362022-03-15 16:58:34 +1100168 ClearPageIsolated(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700169 unlock_page(page);
170 put_page(page);
171 } else {
Naoya Horiguchie8db67e2017-09-08 16:11:12 -0700172 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -0700173 page_is_file_lru(page), -thp_nr_pages(page));
Rabin Vincentfc280fe2017-04-20 14:37:46 -0700174 putback_lru_page(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700175 }
Christoph Lameterb20a3502006-03-22 00:09:12 -0800176 }
Christoph Lameterb20a3502006-03-22 00:09:12 -0800177}
Charan Teja Reddyf47b8522021-02-16 13:59:45 +0530178EXPORT_SYMBOL_GPL(putback_movable_pages);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800179
Christoph Lameter06972122006-06-23 02:03:35 -0700180/*
181 * Restore a potential migration pte to a working pte entry
182 */
Minchan Kime4b82222017-05-03 14:54:27 -0700183static bool remove_migration_pte(struct page *page, struct vm_area_struct *vma,
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800184 unsigned long addr, void *old)
Christoph Lameter06972122006-06-23 02:03:35 -0700185{
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800186 struct page_vma_mapped_walk pvmw = {
187 .page = old,
188 .vma = vma,
189 .address = addr,
190 .flags = PVMW_SYNC | PVMW_MIGRATION,
191 };
192 struct page *new;
193 pte_t pte;
Christoph Lameter06972122006-06-23 02:03:35 -0700194 swp_entry_t entry;
Christoph Lameter06972122006-06-23 02:03:35 -0700195
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800196 VM_BUG_ON_PAGE(PageTail(page), page);
197 while (page_vma_mapped_walk(&pvmw)) {
Naoya Horiguchi4b0ece62017-03-31 15:11:44 -0700198 if (PageKsm(page))
199 new = page;
200 else
201 new = page - pvmw.page->index +
202 linear_page_index(vma, pvmw.address);
Christoph Lameter06972122006-06-23 02:03:35 -0700203
Zi Yan616b8372017-09-08 16:10:57 -0700204#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
205 /* PMD-mapped THP migration entry */
206 if (!pvmw.pte) {
207 VM_BUG_ON_PAGE(PageHuge(page) || !PageTransCompound(page), page);
208 remove_migration_pmd(&pvmw, new);
209 continue;
210 }
211#endif
212
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800213 get_page(new);
214 pte = pte_mkold(mk_pte(new, READ_ONCE(vma->vm_page_prot)));
215 if (pte_swp_soft_dirty(*pvmw.pte))
216 pte = pte_mksoft_dirty(pte);
Christoph Lameter06972122006-06-23 02:03:35 -0700217
Hugh Dickins486cf462011-10-19 12:50:35 -0700218 /*
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800219 * Recheck VMA as permissions can change since migration started
Hugh Dickins486cf462011-10-19 12:50:35 -0700220 */
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800221 entry = pte_to_swp_entry(*pvmw.pte);
Alistair Popple4dd845b2021-06-30 18:54:09 -0700222 if (is_writable_migration_entry(entry))
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800223 pte = maybe_mkwrite(pte, vma);
Peter Xuf45ec5f2020-04-06 20:06:01 -0700224 else if (pte_swp_uffd_wp(*pvmw.pte))
225 pte = pte_mkuffd_wp(pte);
Mel Gormand3cb8bf2014-10-02 19:47:41 +0100226
Ralph Campbell61287632020-09-04 16:36:04 -0700227 if (unlikely(is_device_private_page(new))) {
Alistair Popple4dd845b2021-06-30 18:54:09 -0700228 if (pte_write(pte))
229 entry = make_writable_device_private_entry(
230 page_to_pfn(new));
231 else
232 entry = make_readable_device_private_entry(
233 page_to_pfn(new));
Ralph Campbell61287632020-09-04 16:36:04 -0700234 pte = swp_entry_to_pte(entry);
Ralph Campbell3d321bf82020-09-04 16:36:07 -0700235 if (pte_swp_soft_dirty(*pvmw.pte))
236 pte = pte_swp_mksoft_dirty(pte);
Ralph Campbell61287632020-09-04 16:36:04 -0700237 if (pte_swp_uffd_wp(*pvmw.pte))
238 pte = pte_swp_mkuffd_wp(pte);
Lars Perssond2b2c6dd2019-03-28 20:44:28 -0700239 }
Jérôme Glissea5430dd2017-09-08 16:12:17 -0700240
Andi Kleen3ef8fd72010-10-11 16:03:21 +0200241#ifdef CONFIG_HUGETLB_PAGE
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800242 if (PageHuge(new)) {
Christophe Leroy79c1c592021-06-30 18:48:00 -0700243 unsigned int shift = huge_page_shift(hstate_vma(vma));
244
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800245 pte = pte_mkhuge(pte);
Christophe Leroy79c1c592021-06-30 18:48:00 -0700246 pte = arch_make_huge_pte(pte, shift, vma->vm_flags);
Aneesh Kumar K.V383321a2017-07-06 15:38:41 -0700247 set_huge_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800248 if (PageAnon(new))
249 hugepage_add_anon_rmap(new, vma, pvmw.address);
250 else
251 page_dup_rmap(new, true);
Aneesh Kumar K.V383321a2017-07-06 15:38:41 -0700252 } else
253#endif
254 {
255 set_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700256
Aneesh Kumar K.V383321a2017-07-06 15:38:41 -0700257 if (PageAnon(new))
258 page_add_anon_rmap(new, vma, pvmw.address, false);
259 else
260 page_add_file_rmap(new, false);
261 }
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800262 if (vma->vm_flags & VM_LOCKED && !PageTransCompound(new))
263 mlock_vma_page(new);
Hugh Dickins51afb122015-11-05 18:49:37 -0800264
Kirill A. Shutemove125fe42018-10-05 15:51:41 -0700265 if (PageTransHuge(page) && PageMlocked(page))
266 clear_page_mlock(page);
267
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800268 /* No need to invalidate - it was non-present before */
269 update_mmu_cache(vma, pvmw.address, pvmw.pte);
270 }
271
Minchan Kime4b82222017-05-03 14:54:27 -0700272 return true;
Christoph Lameter06972122006-06-23 02:03:35 -0700273}
274
275/*
Christoph Lameter04e62a22006-06-23 02:03:38 -0700276 * Get rid of all migration entries and replace them by
277 * references to the indicated page.
278 */
Kirill A. Shutemove3884662016-03-17 14:20:07 -0700279void remove_migration_ptes(struct page *old, struct page *new, bool locked)
Christoph Lameter04e62a22006-06-23 02:03:38 -0700280{
Joonsoo Kim051ac832014-01-21 15:49:48 -0800281 struct rmap_walk_control rwc = {
282 .rmap_one = remove_migration_pte,
283 .arg = old,
284 };
285
Kirill A. Shutemove3884662016-03-17 14:20:07 -0700286 if (locked)
287 rmap_walk_locked(new, &rwc);
288 else
289 rmap_walk(new, &rwc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700290}
291
292/*
Christoph Lameter06972122006-06-23 02:03:35 -0700293 * Something used the pte of a page under migration. We need to
294 * get to the page and wait until migration is finished.
295 * When we return from this function the fault will be retried.
Christoph Lameter06972122006-06-23 02:03:35 -0700296 */
Naoya Horiguchie66f17f2015-02-11 15:25:22 -0800297void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700298 spinlock_t *ptl)
Christoph Lameter06972122006-06-23 02:03:35 -0700299{
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700300 pte_t pte;
Christoph Lameter06972122006-06-23 02:03:35 -0700301 swp_entry_t entry;
302 struct page *page;
Tao Zeng54f845e2022-01-26 16:04:08 +0800303#ifdef CONFIG_AMLOGIC_CMA
304 bool need_wait = 0;
305#endif
Christoph Lameter06972122006-06-23 02:03:35 -0700306
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700307 spin_lock(ptl);
Christoph Lameter06972122006-06-23 02:03:35 -0700308 pte = *ptep;
309 if (!is_swap_pte(pte))
310 goto out;
311
312 entry = pte_to_swp_entry(pte);
313 if (!is_migration_entry(entry))
314 goto out;
315
Alistair Poppleaf5cdaf2021-06-30 18:54:06 -0700316 page = pfn_swap_entry_to_page(entry);
Tao Zeng54f845e2022-01-26 16:04:08 +0800317#ifdef CONFIG_AMLOGIC_CMA
318 /* This page is under cma allocating, do not increase it ref */
319 if (in_cma_allocating(page)) {
320 pr_debug("%s, Page:%lx, flags:%lx, m:%d, c:%d, map:%p\n",
321 __func__, page_to_pfn(page), page->flags,
322 page_mapcount(page), page_count(page),
323 page->mapping);
324 need_wait = 1;
325 goto out;
326 }
327#endif
Xu Yuffc90cb2021-06-15 18:23:42 -0700328 page = compound_head(page);
Christoph Lameter06972122006-06-23 02:03:35 -0700329
Nick Piggine2867812008-07-25 19:45:30 -0700330 /*
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500331 * Once page cache replacement of page migration started, page_count
Hugh Dickins9a1ea432018-12-28 00:36:14 -0800332 * is zero; but we must not call put_and_wait_on_page_locked() without
333 * a ref. Use get_page_unless_zero(), and just fault again if it fails.
Nick Piggine2867812008-07-25 19:45:30 -0700334 */
335 if (!get_page_unless_zero(page))
336 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700337 pte_unmap_unlock(ptep, ptl);
Matthew Wilcox (Oracle)48054622021-02-24 12:02:02 -0800338 put_and_wait_on_page_locked(page, TASK_UNINTERRUPTIBLE);
Christoph Lameter06972122006-06-23 02:03:35 -0700339 return;
340out:
341 pte_unmap_unlock(ptep, ptl);
Tao Zeng54f845e2022-01-26 16:04:08 +0800342#ifdef CONFIG_AMLOGIC_CMA
343 if (need_wait)
344 usleep_range(1000, 1100);
345#endif
Christoph Lameter06972122006-06-23 02:03:35 -0700346}
347
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700348void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
349 unsigned long address)
350{
351 spinlock_t *ptl = pte_lockptr(mm, pmd);
352 pte_t *ptep = pte_offset_map(pmd, address);
353 __migration_entry_wait(mm, ptep, ptl);
354}
355
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -0800356void migration_entry_wait_huge(struct vm_area_struct *vma,
357 struct mm_struct *mm, pte_t *pte)
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700358{
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -0800359 spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte);
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700360 __migration_entry_wait(mm, pte, ptl);
361}
362
Zi Yan616b8372017-09-08 16:10:57 -0700363#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
364void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd)
365{
366 spinlock_t *ptl;
367 struct page *page;
368
369 ptl = pmd_lock(mm, pmd);
370 if (!is_pmd_migration_entry(*pmd))
371 goto unlock;
Alistair Poppleaf5cdaf2021-06-30 18:54:06 -0700372 page = pfn_swap_entry_to_page(pmd_to_swp_entry(*pmd));
Zi Yan616b8372017-09-08 16:10:57 -0700373 if (!get_page_unless_zero(page))
374 goto unlock;
375 spin_unlock(ptl);
Matthew Wilcox (Oracle)48054622021-02-24 12:02:02 -0800376 put_and_wait_on_page_locked(page, TASK_UNINTERRUPTIBLE);
Zi Yan616b8372017-09-08 16:10:57 -0700377 return;
378unlock:
379 spin_unlock(ptl);
380}
381#endif
382
Jan Karaf9004822019-03-05 15:48:46 -0800383static int expected_page_refs(struct address_space *mapping, struct page *page)
Jan Kara0b3901b2018-12-28 00:39:01 -0800384{
385 int expected_count = 1;
386
387 /*
Ralph Campbellf1f4f3a2020-10-13 16:58:42 -0700388 * Device private pages have an extra refcount as they are
Jan Kara0b3901b2018-12-28 00:39:01 -0800389 * ZONE_DEVICE pages.
390 */
391 expected_count += is_device_private_page(page);
Jan Karaf9004822019-03-05 15:48:46 -0800392 if (mapping)
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -0700393 expected_count += thp_nr_pages(page) + page_has_private(page);
Jan Kara0b3901b2018-12-28 00:39:01 -0800394
395 return expected_count;
396}
397
Christoph Lameterb20a3502006-03-22 00:09:12 -0800398/*
Christoph Lameterc3fcf8a2006-06-23 02:03:32 -0700399 * Replace the page in the mapping.
Christoph Lameter5b5c7122006-06-23 02:03:29 -0700400 *
401 * The number of remaining references must be:
402 * 1 for anonymous pages without a mapping
403 * 2 for pages with a mapping
David Howells266cf652009-04-03 16:42:36 +0100404 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800405 */
Gu Zheng36bc08c2013-07-16 17:56:16 +0800406int migrate_page_move_mapping(struct address_space *mapping,
Keith Busch37109692019-07-18 15:58:46 -0700407 struct page *newpage, struct page *page, int extra_count)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800408{
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500409 XA_STATE(xas, &mapping->i_pages, page_index(page));
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800410 struct zone *oldzone, *newzone;
411 int dirty;
Jan Karaf9004822019-03-05 15:48:46 -0800412 int expected_count = expected_page_refs(mapping, page) + extra_count;
Shakeel Butt5c447d22021-01-23 21:01:15 -0800413 int nr = thp_nr_pages(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -0700414
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700415 if (!mapping) {
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700416 /* Anonymous page without mapping */
Tao Zeng54f845e2022-01-26 16:04:08 +0800417 #ifdef CONFIG_AMLOGIC_CMA
418 if (page_count(page) != expected_count)
419 cma_debug(2, page, " anon page cnt miss match, e:%d\n",
420 expected_count);
421 #endif
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500422 if (page_count(page) != expected_count)
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700423 return -EAGAIN;
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800424
425 /* No turning back from here */
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800426 newpage->index = page->index;
427 newpage->mapping = page->mapping;
428 if (PageSwapBacked(page))
Hugh Dickinsfa9949d2016-05-19 17:12:41 -0700429 __SetPageSwapBacked(newpage);
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800430
Rafael Aquini78bd5202012-12-11 16:02:31 -0800431 return MIGRATEPAGE_SUCCESS;
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700432 }
433
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800434 oldzone = page_zone(page);
435 newzone = page_zone(newpage);
436
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500437 xas_lock_irq(&xas);
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500438 if (page_count(page) != expected_count || xas_load(&xas) != page) {
439 xas_unlock_irq(&xas);
Tao Zeng54f845e2022-01-26 16:04:08 +0800440 #ifdef CONFIG_AMLOGIC_CMA
441 cma_debug(2, page, " anon page cnt miss match, e:%d, p:%d\n",
442 expected_count, page_has_private(page));
443 #endif
Christoph Lametere23ca002006-04-10 22:52:57 -0700444 return -EAGAIN;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800445 }
446
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700447 if (!page_ref_freeze(page, expected_count)) {
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500448 xas_unlock_irq(&xas);
Tao Zeng54f845e2022-01-26 16:04:08 +0800449 #ifdef CONFIG_AMLOGIC_CMA
450 cma_debug(2, page, " page free fail, e:%d, p:%d\n",
451 expected_count, page_has_private(page));
452 #endif
Nick Piggine2867812008-07-25 19:45:30 -0700453 return -EAGAIN;
454 }
455
Christoph Lameterb20a3502006-03-22 00:09:12 -0800456 /*
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800457 * Now we know that no one else is looking at the page:
458 * no turning back from here.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800459 */
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800460 newpage->index = page->index;
461 newpage->mapping = page->mapping;
Shakeel Butt5c447d22021-01-23 21:01:15 -0800462 page_ref_add(newpage, nr); /* add cache reference */
Nicholas Piggin6326fec2016-12-25 13:00:29 +1000463 if (PageSwapBacked(page)) {
464 __SetPageSwapBacked(newpage);
465 if (PageSwapCache(page)) {
466 SetPageSwapCache(newpage);
467 set_page_private(newpage, page_private(page));
468 }
469 } else {
470 VM_BUG_ON_PAGE(PageSwapCache(page), page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800471 }
472
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800473 /* Move dirty while page refs frozen and newpage not yet exposed */
474 dirty = PageDirty(page);
475 if (dirty) {
476 ClearPageDirty(page);
477 SetPageDirty(newpage);
478 }
479
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500480 xas_store(&xas, newpage);
Naoya Horiguchie71769a2018-04-20 14:55:45 -0700481 if (PageTransHuge(page)) {
482 int i;
Naoya Horiguchie71769a2018-04-20 14:55:45 -0700483
Shakeel Butt5c447d22021-01-23 21:01:15 -0800484 for (i = 1; i < nr; i++) {
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500485 xas_next(&xas);
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700486 xas_store(&xas, newpage);
Naoya Horiguchie71769a2018-04-20 14:55:45 -0700487 }
Naoya Horiguchie71769a2018-04-20 14:55:45 -0700488 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800489
490 /*
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800491 * Drop cache reference from old page by unfreezing
492 * to one less reference.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800493 * We know this isn't the last reference.
494 */
Shakeel Butt5c447d22021-01-23 21:01:15 -0800495 page_ref_unfreeze(page, expected_count - nr);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800496
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500497 xas_unlock(&xas);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800498 /* Leave irq disabled to prevent preemption while updating stats */
499
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700500 /*
501 * If moved to a different zone then also account
502 * the page for that zone. Other VM counters will be
503 * taken care of when we establish references to the
504 * new page and drop references to the old page.
505 *
506 * Note that anonymous pages are accounted for
Mel Gorman4b9d0fa2016-07-28 15:46:17 -0700507 * via NR_FILE_PAGES and NR_ANON_MAPPED if they
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700508 * are mapped to swap space.
509 */
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800510 if (newzone != oldzone) {
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700511 struct lruvec *old_lruvec, *new_lruvec;
512 struct mem_cgroup *memcg;
513
514 memcg = page_memcg(page);
515 old_lruvec = mem_cgroup_lruvec(memcg, oldzone->zone_pgdat);
516 new_lruvec = mem_cgroup_lruvec(memcg, newzone->zone_pgdat);
517
Shakeel Butt5c447d22021-01-23 21:01:15 -0800518 __mod_lruvec_state(old_lruvec, NR_FILE_PAGES, -nr);
519 __mod_lruvec_state(new_lruvec, NR_FILE_PAGES, nr);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800520 if (PageSwapBacked(page) && !PageSwapCache(page)) {
Shakeel Butt5c447d22021-01-23 21:01:15 -0800521 __mod_lruvec_state(old_lruvec, NR_SHMEM, -nr);
522 __mod_lruvec_state(new_lruvec, NR_SHMEM, nr);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800523 }
Shakeel Buttb6038942021-02-24 12:03:55 -0800524#ifdef CONFIG_SWAP
525 if (PageSwapCache(page)) {
526 __mod_lruvec_state(old_lruvec, NR_SWAPCACHE, -nr);
527 __mod_lruvec_state(new_lruvec, NR_SWAPCACHE, nr);
528 }
529#endif
Christoph Hellwigf56753a2020-09-24 08:51:40 +0200530 if (dirty && mapping_can_writeback(mapping)) {
Shakeel Butt5c447d22021-01-23 21:01:15 -0800531 __mod_lruvec_state(old_lruvec, NR_FILE_DIRTY, -nr);
532 __mod_zone_page_state(oldzone, NR_ZONE_WRITE_PENDING, -nr);
533 __mod_lruvec_state(new_lruvec, NR_FILE_DIRTY, nr);
534 __mod_zone_page_state(newzone, NR_ZONE_WRITE_PENDING, nr);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800535 }
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700536 }
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800537 local_irq_enable();
Christoph Lameterb20a3502006-03-22 00:09:12 -0800538
Rafael Aquini78bd5202012-12-11 16:02:31 -0800539 return MIGRATEPAGE_SUCCESS;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800540}
Richard Weinberger1118dce2016-06-16 23:26:14 +0200541EXPORT_SYMBOL(migrate_page_move_mapping);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800542
543/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900544 * The expected number of remaining references is the same as that
545 * of migrate_page_move_mapping().
546 */
547int migrate_huge_page_move_mapping(struct address_space *mapping,
548 struct page *newpage, struct page *page)
549{
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500550 XA_STATE(xas, &mapping->i_pages, page_index(page));
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900551 int expected_count;
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900552
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500553 xas_lock_irq(&xas);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900554 expected_count = 2 + page_has_private(page);
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500555 if (page_count(page) != expected_count || xas_load(&xas) != page) {
556 xas_unlock_irq(&xas);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900557 return -EAGAIN;
558 }
559
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700560 if (!page_ref_freeze(page, expected_count)) {
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500561 xas_unlock_irq(&xas);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900562 return -EAGAIN;
563 }
564
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800565 newpage->index = page->index;
566 newpage->mapping = page->mapping;
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700567
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900568 get_page(newpage);
569
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500570 xas_store(&xas, newpage);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900571
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700572 page_ref_unfreeze(page, expected_count - 1);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900573
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500574 xas_unlock_irq(&xas);
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700575
Rafael Aquini78bd5202012-12-11 16:02:31 -0800576 return MIGRATEPAGE_SUCCESS;
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900577}
578
579/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800580 * Copy the page to its new location
581 */
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700582void migrate_page_states(struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800583{
Rik van Riel7851a452013-10-07 11:29:23 +0100584 int cpupid;
585
Christoph Lameterb20a3502006-03-22 00:09:12 -0800586 if (PageError(page))
587 SetPageError(newpage);
588 if (PageReferenced(page))
589 SetPageReferenced(newpage);
590 if (PageUptodate(page))
591 SetPageUptodate(newpage);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700592 if (TestClearPageActive(page)) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800593 VM_BUG_ON_PAGE(PageUnevictable(page), page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800594 SetPageActive(newpage);
Lee Schermerhorn418b27e2009-12-14 17:59:54 -0800595 } else if (TestClearPageUnevictable(page))
596 SetPageUnevictable(newpage);
Johannes Weiner1899ad12018-10-26 15:06:04 -0700597 if (PageWorkingset(page))
598 SetPageWorkingset(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800599 if (PageChecked(page))
600 SetPageChecked(newpage);
601 if (PageMappedToDisk(page))
602 SetPageMappedToDisk(newpage);
603
qinglin.lia1a623f2024-06-21 14:36:34 +0800604 trace_android_vh_look_around_migrate_page(page, newpage);
605
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800606 /* Move dirty on pages not done by migrate_page_move_mapping() */
607 if (PageDirty(page))
608 SetPageDirty(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800609
Vladimir Davydov33c3fc72015-09-09 15:35:45 -0700610 if (page_is_young(page))
611 set_page_young(newpage);
612 if (page_is_idle(page))
613 set_page_idle(newpage);
614
Rik van Riel7851a452013-10-07 11:29:23 +0100615 /*
616 * Copy NUMA information to the new page, to prevent over-eager
617 * future migrations of this same page.
618 */
619 cpupid = page_cpupid_xchg_last(page, -1);
620 page_cpupid_xchg_last(newpage, cpupid);
621
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800622 ksm_migrate_page(newpage, page);
Hugh Dickinsc8d65532013-02-22 16:35:10 -0800623 /*
624 * Please do not reorder this without considering how mm/ksm.c's
625 * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache().
626 */
Naoya Horiguchib3b3a992015-04-15 16:13:15 -0700627 if (PageSwapCache(page))
628 ClearPageSwapCache(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800629 ClearPagePrivate(page);
Muchun Songad2fa372021-06-30 18:47:21 -0700630
631 /* page->private contains hugetlb specific flags */
632 if (!PageHuge(page))
633 set_page_private(page, 0);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800634
635 /*
636 * If any waiters have accumulated on the new page then
637 * wake them up.
638 */
639 if (PageWriteback(newpage))
640 end_page_writeback(newpage);
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700641
Yang Shi6aeff242020-04-06 20:04:21 -0700642 /*
643 * PG_readahead shares the same bit with PG_reclaim. The above
644 * end_page_writeback() may clear PG_readahead mistakenly, so set the
645 * bit after that.
646 */
647 if (PageReadahead(page))
648 SetPageReadahead(newpage);
649
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700650 copy_page_owner(page, newpage);
Johannes Weiner74485cf2016-03-15 14:57:54 -0700651
Hugh Dickinsa333e3e2020-09-18 21:20:06 -0700652 if (!PageHuge(page))
653 mem_cgroup_migrate(page, newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800654}
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700655EXPORT_SYMBOL(migrate_page_states);
656
657void migrate_page_copy(struct page *newpage, struct page *page)
658{
659 if (PageHuge(page) || PageTransHuge(page))
660 copy_huge_page(newpage, page);
661 else
662 copy_highpage(newpage, page);
663
664 migrate_page_states(newpage, page);
665}
Richard Weinberger1118dce2016-06-16 23:26:14 +0200666EXPORT_SYMBOL(migrate_page_copy);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800667
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700668/************************************************************
669 * Migration functions
670 ***********************************************************/
671
Christoph Lameterb20a3502006-03-22 00:09:12 -0800672/*
Minchan Kimbda807d2016-07-26 15:23:05 -0700673 * Common logic to directly migrate a single LRU page suitable for
David Howells266cf652009-04-03 16:42:36 +0100674 * pages that do not use PagePrivate/PagePrivate2.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800675 *
676 * Pages are locked upon entry and exit.
677 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700678int migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800679 struct page *newpage, struct page *page,
680 enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800681{
682 int rc;
683
684 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
685
Keith Busch37109692019-07-18 15:58:46 -0700686 rc = migrate_page_move_mapping(mapping, newpage, page, 0);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800687
Rafael Aquini78bd5202012-12-11 16:02:31 -0800688 if (rc != MIGRATEPAGE_SUCCESS)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800689 return rc;
690
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700691 if (mode != MIGRATE_SYNC_NO_COPY)
692 migrate_page_copy(newpage, page);
693 else
694 migrate_page_states(newpage, page);
Rafael Aquini78bd5202012-12-11 16:02:31 -0800695 return MIGRATEPAGE_SUCCESS;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800696}
697EXPORT_SYMBOL(migrate_page);
698
David Howells93614012006-09-30 20:45:40 +0200699#ifdef CONFIG_BLOCK
Jan Kara84ade7c2018-12-28 00:39:09 -0800700/* Returns true if all buffers are successfully locked */
701static bool buffer_migrate_lock_buffers(struct buffer_head *head,
702 enum migrate_mode mode)
703{
704 struct buffer_head *bh = head;
705
706 /* Simple case, sync compaction */
707 if (mode != MIGRATE_ASYNC) {
708 do {
Jan Kara84ade7c2018-12-28 00:39:09 -0800709 lock_buffer(bh);
710 bh = bh->b_this_page;
711
712 } while (bh != head);
713
714 return true;
715 }
716
717 /* async case, we cannot block on lock_buffer so use trylock_buffer */
718 do {
Jan Kara84ade7c2018-12-28 00:39:09 -0800719 if (!trylock_buffer(bh)) {
720 /*
721 * We failed to lock the buffer and cannot stall in
722 * async migration. Release the taken locks
723 */
724 struct buffer_head *failed_bh = bh;
Jan Kara84ade7c2018-12-28 00:39:09 -0800725 bh = head;
726 while (bh != failed_bh) {
727 unlock_buffer(bh);
Jan Kara84ade7c2018-12-28 00:39:09 -0800728 bh = bh->b_this_page;
729 }
730 return false;
731 }
732
733 bh = bh->b_this_page;
734 } while (bh != head);
735 return true;
736}
737
Jan Kara89cb0882018-12-28 00:39:12 -0800738static int __buffer_migrate_page(struct address_space *mapping,
739 struct page *newpage, struct page *page, enum migrate_mode mode,
740 bool check_refs)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700741{
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700742 struct buffer_head *bh, *head;
743 int rc;
Jan Karacc4f11e2018-12-28 00:39:05 -0800744 int expected_count;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700745
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700746 if (!page_has_buffers(page))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800747 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700748
Jan Karacc4f11e2018-12-28 00:39:05 -0800749 /* Check whether page does not have extra refs before we do more work */
Jan Karaf9004822019-03-05 15:48:46 -0800750 expected_count = expected_page_refs(mapping, page);
Jan Karacc4f11e2018-12-28 00:39:05 -0800751 if (page_count(page) != expected_count)
752 return -EAGAIN;
753
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700754 head = page_buffers(page);
Jan Karacc4f11e2018-12-28 00:39:05 -0800755 if (!buffer_migrate_lock_buffers(head, mode))
756 return -EAGAIN;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700757
Jan Kara89cb0882018-12-28 00:39:12 -0800758 if (check_refs) {
759 bool busy;
760 bool invalidated = false;
761
762recheck_buffers:
763 busy = false;
764 spin_lock(&mapping->private_lock);
765 bh = head;
766 do {
767 if (atomic_read(&bh->b_count)) {
768 busy = true;
769 break;
770 }
771 bh = bh->b_this_page;
772 } while (bh != head);
Jan Kara89cb0882018-12-28 00:39:12 -0800773 if (busy) {
774 if (invalidated) {
775 rc = -EAGAIN;
776 goto unlock_buffers;
777 }
Jan Karaebdf4de2019-08-02 21:48:47 -0700778 spin_unlock(&mapping->private_lock);
Jan Kara89cb0882018-12-28 00:39:12 -0800779 invalidate_bh_lrus();
780 invalidated = true;
781 goto recheck_buffers;
782 }
783 }
784
Keith Busch37109692019-07-18 15:58:46 -0700785 rc = migrate_page_move_mapping(mapping, newpage, page, 0);
Rafael Aquini78bd5202012-12-11 16:02:31 -0800786 if (rc != MIGRATEPAGE_SUCCESS)
Jan Karacc4f11e2018-12-28 00:39:05 -0800787 goto unlock_buffers;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700788
Guoqing Jiangcd0f3712020-06-01 21:48:06 -0700789 attach_page_private(newpage, detach_page_private(page));
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700790
791 bh = head;
792 do {
793 set_bh_page(bh, newpage, bh_offset(bh));
794 bh = bh->b_this_page;
795
796 } while (bh != head);
797
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700798 if (mode != MIGRATE_SYNC_NO_COPY)
799 migrate_page_copy(newpage, page);
800 else
801 migrate_page_states(newpage, page);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700802
Jan Karacc4f11e2018-12-28 00:39:05 -0800803 rc = MIGRATEPAGE_SUCCESS;
804unlock_buffers:
Jan Karaebdf4de2019-08-02 21:48:47 -0700805 if (check_refs)
806 spin_unlock(&mapping->private_lock);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700807 bh = head;
808 do {
809 unlock_buffer(bh);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700810 bh = bh->b_this_page;
811
812 } while (bh != head);
813
Jan Karacc4f11e2018-12-28 00:39:05 -0800814 return rc;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700815}
Jan Kara89cb0882018-12-28 00:39:12 -0800816
817/*
818 * Migration function for pages with buffers. This function can only be used
819 * if the underlying filesystem guarantees that no other references to "page"
820 * exist. For example attached buffer heads are accessed only under page lock.
821 */
822int buffer_migrate_page(struct address_space *mapping,
823 struct page *newpage, struct page *page, enum migrate_mode mode)
824{
825 return __buffer_migrate_page(mapping, newpage, page, mode, false);
826}
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700827EXPORT_SYMBOL(buffer_migrate_page);
Jan Kara89cb0882018-12-28 00:39:12 -0800828
829/*
830 * Same as above except that this variant is more careful and checks that there
831 * are also no buffer head references. This function is the right one for
832 * mappings where buffer heads are directly looked up and referenced (such as
833 * block device mappings).
834 */
835int buffer_migrate_page_norefs(struct address_space *mapping,
836 struct page *newpage, struct page *page, enum migrate_mode mode)
837{
838 return __buffer_migrate_page(mapping, newpage, page, mode, true);
839}
David Howells93614012006-09-30 20:45:40 +0200840#endif
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700841
Christoph Lameter04e62a22006-06-23 02:03:38 -0700842/*
843 * Writeback a page to clean the dirty state
844 */
845static int writeout(struct address_space *mapping, struct page *page)
846{
847 struct writeback_control wbc = {
848 .sync_mode = WB_SYNC_NONE,
849 .nr_to_write = 1,
850 .range_start = 0,
851 .range_end = LLONG_MAX,
Christoph Lameter04e62a22006-06-23 02:03:38 -0700852 .for_reclaim = 1
853 };
854 int rc;
855
856 if (!mapping->a_ops->writepage)
857 /* No write method for the address space */
858 return -EINVAL;
859
860 if (!clear_page_dirty_for_io(page))
861 /* Someone else already triggered a write */
862 return -EAGAIN;
863
864 /*
865 * A dirty page may imply that the underlying filesystem has
866 * the page on some queue. So the page must be clean for
867 * migration. Writeout may mean we loose the lock and the
868 * page state is no longer what we checked for earlier.
869 * At this point we know that the migration attempt cannot
870 * be successful.
871 */
Kirill A. Shutemove3884662016-03-17 14:20:07 -0700872 remove_migration_ptes(page, page, false);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700873
874 rc = mapping->a_ops->writepage(page, &wbc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700875
876 if (rc != AOP_WRITEPAGE_ACTIVATE)
877 /* unlocked. Relock */
878 lock_page(page);
879
Hugh Dickinsbda85502008-11-19 15:36:36 -0800880 return (rc < 0) ? -EIO : -EAGAIN;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700881}
882
883/*
884 * Default handling if a filesystem does not provide a migration function.
885 */
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700886static int fallback_migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800887 struct page *newpage, struct page *page, enum migrate_mode mode)
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700888{
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800889 if (PageDirty(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800890 /* Only writeback pages in full synchronous migration */
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700891 switch (mode) {
892 case MIGRATE_SYNC:
893 case MIGRATE_SYNC_NO_COPY:
894 break;
895 default:
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800896 return -EBUSY;
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700897 }
Christoph Lameter04e62a22006-06-23 02:03:38 -0700898 return writeout(mapping, page);
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800899 }
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700900
901 /*
902 * Buffers may be managed in a filesystem specific way.
903 * We must have no buffers or drop them.
904 */
David Howells266cf652009-04-03 16:42:36 +0100905 if (page_has_private(page) &&
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700906 !try_to_release_page(page, GFP_KERNEL))
Mel Gorman806031b2019-03-05 15:44:43 -0800907 return mode == MIGRATE_SYNC ? -EAGAIN : -EBUSY;
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700908
Mel Gormana6bc32b2012-01-12 17:19:43 -0800909 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700910}
911
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700912/*
Christoph Lametere24f0b82006-06-23 02:03:51 -0700913 * Move a page to a newly allocated page
914 * The page is locked and all ptes have been successfully removed.
915 *
916 * The new page will have replaced the old page if this function
917 * is successful.
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700918 *
919 * Return value:
920 * < 0 - error code
Rafael Aquini78bd5202012-12-11 16:02:31 -0800921 * MIGRATEPAGE_SUCCESS - success
Christoph Lametere24f0b82006-06-23 02:03:51 -0700922 */
Mel Gorman3fe20112010-05-24 14:32:20 -0700923static int move_to_new_page(struct page *newpage, struct page *page,
Hugh Dickins5c3f9a62015-11-05 18:49:53 -0800924 enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700925{
926 struct address_space *mapping;
Minchan Kimbda807d2016-07-26 15:23:05 -0700927 int rc = -EAGAIN;
928 bool is_lru = !__PageMovable(page);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700929
Hugh Dickins7db76712015-11-05 18:49:49 -0800930 VM_BUG_ON_PAGE(!PageLocked(page), page);
931 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700932
Christoph Lametere24f0b82006-06-23 02:03:51 -0700933 mapping = page_mapping(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700934
935 if (likely(is_lru)) {
936 if (!mapping)
937 rc = migrate_page(mapping, newpage, page, mode);
938 else if (mapping->a_ops->migratepage)
939 /*
940 * Most pages have a mapping and most filesystems
941 * provide a migratepage callback. Anonymous pages
942 * are part of swap space which also has its own
943 * migratepage callback. This is the most common path
944 * for page migration.
945 */
946 rc = mapping->a_ops->migratepage(mapping, newpage,
947 page, mode);
948 else
949 rc = fallback_migrate_page(mapping, newpage,
950 page, mode);
951 } else {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700952 /*
Minchan Kimbda807d2016-07-26 15:23:05 -0700953 * In case of non-lru page, it could be released after
954 * isolation step. In that case, we shouldn't try migration.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700955 */
Minchan Kimbda807d2016-07-26 15:23:05 -0700956 VM_BUG_ON_PAGE(!PageIsolated(page), page);
957 if (!PageMovable(page)) {
958 rc = MIGRATEPAGE_SUCCESS;
andrew.yang0d8a8362022-03-15 16:58:34 +1100959 ClearPageIsolated(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700960 goto out;
961 }
962
963 rc = mapping->a_ops->migratepage(mapping, newpage,
964 page, mode);
965 WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS &&
966 !PageIsolated(page));
967 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700968
Hugh Dickins5c3f9a62015-11-05 18:49:53 -0800969 /*
970 * When successful, old pagecache page->mapping must be cleared before
971 * page is freed; but stats require that PageAnon be left as PageAnon.
972 */
973 if (rc == MIGRATEPAGE_SUCCESS) {
Minchan Kimbda807d2016-07-26 15:23:05 -0700974 if (__PageMovable(page)) {
975 VM_BUG_ON_PAGE(!PageIsolated(page), page);
976
977 /*
978 * We clear PG_movable under page_lock so any compactor
979 * cannot try to migrate this page.
980 */
andrew.yang0d8a8362022-03-15 16:58:34 +1100981 ClearPageIsolated(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700982 }
983
984 /*
Ralph Campbellc23a0c92020-01-30 22:14:41 -0800985 * Anonymous and movable page->mapping will be cleared by
Minchan Kimbda807d2016-07-26 15:23:05 -0700986 * free_pages_prepare so don't reset it here for keeping
987 * the type to work PageAnon, for example.
988 */
989 if (!PageMappingFlags(page))
Hugh Dickins5c3f9a62015-11-05 18:49:53 -0800990 page->mapping = NULL;
Lars Perssond2b2c6dd2019-03-28 20:44:28 -0700991
Muchun Song97a9f802022-03-22 14:41:56 -0700992 if (likely(!is_zone_device_page(newpage))) {
993 int i, nr = compound_nr(newpage);
Lars Perssond2b2c6dd2019-03-28 20:44:28 -0700994
Muchun Song97a9f802022-03-22 14:41:56 -0700995 for (i = 0; i < nr; i++)
996 flush_dcache_page(newpage + i);
997 }
Mel Gorman3fe20112010-05-24 14:32:20 -0700998 }
Minchan Kimbda807d2016-07-26 15:23:05 -0700999out:
Christoph Lametere24f0b82006-06-23 02:03:51 -07001000 return rc;
1001}
1002
Minchan Kim0dabec92011-10-31 17:06:57 -07001003static int __unmap_and_move(struct page *page, struct page *newpage,
Hugh Dickins9c620e22013-02-22 16:35:14 -08001004 int force, enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -07001005{
Minchan Kim0dabec92011-10-31 17:06:57 -07001006 int rc = -EAGAIN;
Baolin Wang213ecb32021-09-08 15:18:06 -07001007 bool page_was_mapped = false;
Mel Gorman3f6c8272010-05-24 14:32:17 -07001008 struct anon_vma *anon_vma = NULL;
Minchan Kimbda807d2016-07-26 15:23:05 -07001009 bool is_lru = !__PageMovable(page);
Christoph Lameter95a402c2006-06-23 02:03:53 -07001010
Nick Piggin529ae9a2008-08-02 12:01:03 +02001011 if (!trylock_page(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -08001012 if (!force || mode == MIGRATE_ASYNC)
Minchan Kim0dabec92011-10-31 17:06:57 -07001013 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -08001014
1015 /*
1016 * It's not safe for direct compaction to call lock_page.
1017 * For example, during page readahead pages are added locked
1018 * to the LRU. Later, when the IO completes the pages are
1019 * marked uptodate and unlocked. However, the queueing
1020 * could be merging multiple pages for one bio (e.g.
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -07001021 * mpage_readahead). If an allocation happens for the
Mel Gorman3e7d3442011-01-13 15:45:56 -08001022 * second or third page, the process can end up locking
1023 * the same page twice and deadlocking. Rather than
1024 * trying to be clever about what pages can be locked,
1025 * avoid the use of lock_page for direct compaction
1026 * altogether.
1027 */
1028 if (current->flags & PF_MEMALLOC)
Minchan Kim0dabec92011-10-31 17:06:57 -07001029 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -08001030
Christoph Lametere24f0b82006-06-23 02:03:51 -07001031 lock_page(page);
1032 }
1033
1034 if (PageWriteback(page)) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -07001035 /*
Jianguo Wufed5b642013-04-29 15:07:58 -07001036 * Only in the case of a full synchronous migration is it
Mel Gormana6bc32b2012-01-12 17:19:43 -08001037 * necessary to wait for PageWriteback. In the async case,
1038 * the retry loop is too short and in the sync-light case,
1039 * the overhead of stalling is too much
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -07001040 */
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07001041 switch (mode) {
1042 case MIGRATE_SYNC:
1043 case MIGRATE_SYNC_NO_COPY:
1044 break;
1045 default:
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -07001046 rc = -EBUSY;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07001047 goto out_unlock;
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -07001048 }
1049 if (!force)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07001050 goto out_unlock;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001051 wait_on_page_writeback(page);
1052 }
Hugh Dickins03f15c82015-11-05 18:49:56 -08001053
Christoph Lametere24f0b82006-06-23 02:03:51 -07001054 /*
Baolin Wang68a98432021-09-08 15:18:03 -07001055 * By try_to_migrate(), page->mapcount goes down to 0 here. In this case,
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001056 * we cannot notice that anon_vma is freed while we migrates a page.
Hugh Dickins1ce82b62011-01-13 15:47:30 -08001057 * This get_anon_vma() delays freeing anon_vma pointer until the end
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001058 * of migration. File cache pages are no problem because of page_lock()
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -07001059 * File Caches may use write_page() or lock_page() in migration, then,
1060 * just care Anon page here.
Hugh Dickins03f15c82015-11-05 18:49:56 -08001061 *
1062 * Only page_get_anon_vma() understands the subtleties of
1063 * getting a hold on an anon_vma from outside one of its mms.
1064 * But if we cannot get anon_vma, then we won't need it anyway,
1065 * because that implies that the anon page is no longer mapped
1066 * (and cannot be remapped so long as we hold the page lock).
Christoph Lametere24f0b82006-06-23 02:03:51 -07001067 */
Hugh Dickins03f15c82015-11-05 18:49:56 -08001068 if (PageAnon(page) && !PageKsm(page))
Peter Zijlstra746b18d2011-05-24 17:12:10 -07001069 anon_vma = page_get_anon_vma(page);
Shaohua Li62e1c552008-02-04 22:29:33 -08001070
Hugh Dickins7db76712015-11-05 18:49:49 -08001071 /*
1072 * Block others from accessing the new page when we get around to
1073 * establishing additional references. We are usually the only one
1074 * holding a reference to newpage at this point. We used to have a BUG
1075 * here if trylock_page(newpage) fails, but would like to allow for
1076 * cases where there might be a race with the previous use of newpage.
1077 * This is much like races on refcount of oldpage: just don't BUG().
1078 */
1079 if (unlikely(!trylock_page(newpage)))
1080 goto out_unlock;
1081
Minchan Kimbda807d2016-07-26 15:23:05 -07001082 if (unlikely(!is_lru)) {
1083 rc = move_to_new_page(newpage, page, mode);
1084 goto out_unlock_both;
1085 }
1086
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001087 /*
Shaohua Li62e1c552008-02-04 22:29:33 -08001088 * Corner case handling:
1089 * 1. When a new swap-cache page is read into, it is added to the LRU
1090 * and treated as swapcache but it has no rmap yet.
1091 * Calling try_to_unmap() against a page->mapping==NULL page will
1092 * trigger a BUG. So handle it here.
Yang Shid12b8952020-12-14 19:13:02 -08001093 * 2. An orphaned page (see truncate_cleanup_page) might have
Shaohua Li62e1c552008-02-04 22:29:33 -08001094 * fs-private metadata. The page can be picked up due to memory
1095 * offlining. Everywhere else except page reclaim, the page is
1096 * invisible to the vm, so the page can not be migrated. So try to
1097 * free the metadata, so the page can be freed.
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001098 */
Shaohua Li62e1c552008-02-04 22:29:33 -08001099 if (!page->mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -08001100 VM_BUG_ON_PAGE(PageAnon(page), page);
Hugh Dickins1ce82b62011-01-13 15:47:30 -08001101 if (page_has_private(page)) {
Shaohua Li62e1c552008-02-04 22:29:33 -08001102 try_to_free_buffers(page);
Hugh Dickins7db76712015-11-05 18:49:49 -08001103 goto out_unlock_both;
Shaohua Li62e1c552008-02-04 22:29:33 -08001104 }
Hugh Dickins7db76712015-11-05 18:49:49 -08001105 } else if (page_mapped(page)) {
1106 /* Establish migration ptes */
Hugh Dickins03f15c82015-11-05 18:49:56 -08001107 VM_BUG_ON_PAGE(PageAnon(page) && !PageKsm(page) && !anon_vma,
1108 page);
Alistair Popplea98a2f02021-06-30 18:54:16 -07001109 try_to_migrate(page, 0);
Baolin Wang213ecb32021-09-08 15:18:06 -07001110 page_was_mapped = true;
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001111 }
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001112
Christoph Lametere6a15302006-06-25 05:46:49 -07001113 if (!page_mapped(page))
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001114 rc = move_to_new_page(newpage, page, mode);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001115
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001116 if (page_was_mapped)
1117 remove_migration_ptes(page,
Kirill A. Shutemove3884662016-03-17 14:20:07 -07001118 rc == MIGRATEPAGE_SUCCESS ? newpage : page, false);
Mel Gorman3f6c8272010-05-24 14:32:17 -07001119
Hugh Dickins7db76712015-11-05 18:49:49 -08001120out_unlock_both:
1121 unlock_page(newpage);
1122out_unlock:
Mel Gorman3f6c8272010-05-24 14:32:17 -07001123 /* Drop an anon_vma reference if we took one */
Rik van Riel76545062010-08-09 17:18:41 -07001124 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -07001125 put_anon_vma(anon_vma);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001126 unlock_page(page);
Minchan Kim0dabec92011-10-31 17:06:57 -07001127out:
Minchan Kimc6c919e2016-07-26 15:23:02 -07001128 /*
1129 * If migration is successful, decrease refcount of the newpage
1130 * which will not free the page because new page owner increased
1131 * refcounter. As well, if it is LRU page, add the page to LRU
David Hildenbrande0a352f2019-02-01 14:21:19 -08001132 * list in here. Use the old state of the isolated source page to
1133 * determine if we migrated a LRU page. newpage was already unlocked
1134 * and possibly modified by its owner - don't rely on the page
1135 * state.
Minchan Kimc6c919e2016-07-26 15:23:02 -07001136 */
1137 if (rc == MIGRATEPAGE_SUCCESS) {
David Hildenbrande0a352f2019-02-01 14:21:19 -08001138 if (unlikely(!is_lru))
Minchan Kimc6c919e2016-07-26 15:23:02 -07001139 put_page(newpage);
1140 else
1141 putback_lru_page(newpage);
1142 }
1143
Minchan Kim0dabec92011-10-31 17:06:57 -07001144 return rc;
1145}
Christoph Lameter95a402c2006-06-23 02:03:53 -07001146
Dave Hansen79c28a42021-09-02 14:59:06 -07001147
1148/*
1149 * node_demotion[] example:
1150 *
1151 * Consider a system with two sockets. Each socket has
1152 * three classes of memory attached: fast, medium and slow.
1153 * Each memory class is placed in its own NUMA node. The
1154 * CPUs are placed in the node with the "fast" memory. The
1155 * 6 NUMA nodes (0-5) might be split among the sockets like
1156 * this:
1157 *
1158 * Socket A: 0, 1, 2
1159 * Socket B: 3, 4, 5
1160 *
1161 * When Node 0 fills up, its memory should be migrated to
1162 * Node 1. When Node 1 fills up, it should be migrated to
1163 * Node 2. The migration path start on the nodes with the
1164 * processors (since allocations default to this node) and
1165 * fast memory, progress through medium and end with the
1166 * slow memory:
1167 *
1168 * 0 -> 1 -> 2 -> stop
1169 * 3 -> 4 -> 5 -> stop
1170 *
1171 * This is represented in the node_demotion[] like this:
1172 *
1173 * { 1, // Node 0 migrates to 1
1174 * 2, // Node 1 migrates to 2
1175 * -1, // Node 2 does not migrate
1176 * 4, // Node 3 migrates to 4
1177 * 5, // Node 4 migrates to 5
1178 * -1} // Node 5 does not migrate
1179 */
1180
1181/*
1182 * Writes to this array occur without locking. Cycles are
1183 * not allowed: Node X demotes to Y which demotes to X...
1184 *
1185 * If multiple reads are performed, a single rcu_read_lock()
1186 * must be held over all reads to ensure that no cycles are
1187 * observed.
1188 */
1189static int node_demotion[MAX_NUMNODES] __read_mostly =
1190 {[0 ... MAX_NUMNODES - 1] = NUMA_NO_NODE};
1191
1192/**
1193 * next_demotion_node() - Get the next node in the demotion path
1194 * @node: The starting node to lookup the next node
1195 *
Randy Dunlapc9bd7d12021-09-02 15:00:36 -07001196 * Return: node id for next memory node in the demotion path hierarchy
Dave Hansen79c28a42021-09-02 14:59:06 -07001197 * from @node; NUMA_NO_NODE if @node is terminal. This does not keep
1198 * @node online or guarantee that it *continues* to be the next demotion
1199 * target.
1200 */
1201int next_demotion_node(int node)
1202{
1203 int target;
1204
1205 /*
1206 * node_demotion[] is updated without excluding this
1207 * function from running. RCU doesn't provide any
1208 * compiler barriers, so the READ_ONCE() is required
1209 * to avoid compiler reordering or read merging.
1210 *
1211 * Make sure to use RCU over entire code blocks if
1212 * node_demotion[] reads need to be consistent.
1213 */
1214 rcu_read_lock();
1215 target = READ_ONCE(node_demotion[node]);
1216 rcu_read_unlock();
1217
1218 return target;
1219}
1220
Minchan Kim0dabec92011-10-31 17:06:57 -07001221/*
1222 * Obtain the lock on page, remove all ptes and migrate the page
1223 * to the newly allocated page in newpage.
1224 */
Linus Torvalds6ec44762020-07-08 10:48:35 -07001225static int unmap_and_move(new_page_t get_new_page,
Geert Uytterhoevenef2a5152015-04-14 15:44:22 -07001226 free_page_t put_new_page,
1227 unsigned long private, struct page *page,
Naoya Horiguchiadd05ce2015-06-24 16:56:50 -07001228 int force, enum migrate_mode mode,
Yang Shidd4ae782020-12-14 19:13:06 -08001229 enum migrate_reason reason,
1230 struct list_head *ret)
Minchan Kim0dabec92011-10-31 17:06:57 -07001231{
Hugh Dickins2def7422015-11-05 18:49:46 -08001232 int rc = MIGRATEPAGE_SUCCESS;
Yang Shi74d4a572019-11-30 17:57:12 -08001233 struct page *newpage = NULL;
Minchan Kim0dabec92011-10-31 17:06:57 -07001234
Michal Hocko94723aa2018-04-10 16:30:07 -07001235 if (!thp_migration_supported() && PageTransHuge(page))
Yang Shid532e2e2020-12-14 19:13:16 -08001236 return -ENOSYS;
Michal Hocko94723aa2018-04-10 16:30:07 -07001237
Minchan Kim0dabec92011-10-31 17:06:57 -07001238 if (page_count(page) == 1) {
1239 /* page was freed from under us. So we are done. */
Minchan Kimc6c919e2016-07-26 15:23:02 -07001240 ClearPageActive(page);
1241 ClearPageUnevictable(page);
Minchan Kimbda807d2016-07-26 15:23:05 -07001242 if (unlikely(__PageMovable(page))) {
1243 lock_page(page);
1244 if (!PageMovable(page))
andrew.yang0d8a8362022-03-15 16:58:34 +11001245 ClearPageIsolated(page);
Minchan Kimbda807d2016-07-26 15:23:05 -07001246 unlock_page(page);
1247 }
Minchan Kim0dabec92011-10-31 17:06:57 -07001248 goto out;
1249 }
1250
Yang Shi74d4a572019-11-30 17:57:12 -08001251 newpage = get_new_page(page, private);
1252 if (!newpage)
1253 return -ENOMEM;
1254
Hugh Dickins9c620e22013-02-22 16:35:14 -08001255 rc = __unmap_and_move(page, newpage, force, mode);
Minchan Kimc6c919e2016-07-26 15:23:02 -07001256 if (rc == MIGRATEPAGE_SUCCESS)
Vlastimil Babka7cd12b42016-03-15 14:56:18 -07001257 set_page_owner_migrate_reason(newpage, reason);
Rafael Aquinibf6bddf12012-12-11 16:02:42 -08001258
Minchan Kim0dabec92011-10-31 17:06:57 -07001259out:
Christoph Lametere24f0b82006-06-23 02:03:51 -07001260 if (rc != -EAGAIN) {
Minchan Kim0dabec92011-10-31 17:06:57 -07001261 /*
1262 * A page that has been migrated has all references
1263 * removed and will be freed. A page that has not been
Ralph Campbellc23a0c92020-01-30 22:14:41 -08001264 * migrated will have kept its references and be restored.
Minchan Kim0dabec92011-10-31 17:06:57 -07001265 */
1266 list_del(&page->lru);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001267 }
David Rientjes68711a72014-06-04 16:08:25 -07001268
Christoph Lameter95a402c2006-06-23 02:03:53 -07001269 /*
Minchan Kimc6c919e2016-07-26 15:23:02 -07001270 * If migration is successful, releases reference grabbed during
1271 * isolation. Otherwise, restore the page to right list unless
1272 * we want to retry.
Christoph Lameter95a402c2006-06-23 02:03:53 -07001273 */
Minchan Kimc6c919e2016-07-26 15:23:02 -07001274 if (rc == MIGRATEPAGE_SUCCESS) {
Yang Shidd4ae782020-12-14 19:13:06 -08001275 /*
1276 * Compaction can migrate also non-LRU pages which are
1277 * not accounted to NR_ISOLATED_*. They can be recognized
1278 * as __PageMovable
1279 */
1280 if (likely(!__PageMovable(page)))
1281 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
1282 page_is_file_lru(page), -thp_nr_pages(page));
1283
Oscar Salvador79f5f8f2020-10-15 20:07:09 -07001284 if (reason != MR_MEMORY_FAILURE)
Minchan Kimc6c919e2016-07-26 15:23:02 -07001285 /*
Oscar Salvador79f5f8f2020-10-15 20:07:09 -07001286 * We release the page in page_handle_poison.
Minchan Kimc6c919e2016-07-26 15:23:02 -07001287 */
Oscar Salvador79f5f8f2020-10-15 20:07:09 -07001288 put_page(page);
Minchan Kimc6c919e2016-07-26 15:23:02 -07001289 } else {
Yang Shidd4ae782020-12-14 19:13:06 -08001290 if (rc != -EAGAIN)
1291 list_add_tail(&page->lru, ret);
Minchan Kimbda807d2016-07-26 15:23:05 -07001292
Minchan Kimc6c919e2016-07-26 15:23:02 -07001293 if (put_new_page)
1294 put_new_page(newpage, private);
1295 else
1296 put_page(newpage);
1297 }
David Rientjes68711a72014-06-04 16:08:25 -07001298
Christoph Lametere24f0b82006-06-23 02:03:51 -07001299 return rc;
1300}
1301
1302/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001303 * Counterpart of unmap_and_move_page() for hugepage migration.
1304 *
1305 * This function doesn't wait the completion of hugepage I/O
1306 * because there is no race between I/O and migration for hugepage.
1307 * Note that currently hugepage I/O occurs only in direct I/O
1308 * where no lock is held and PG_writeback is irrelevant,
1309 * and writeback status of all subpages are counted in the reference
1310 * count of the head page (i.e. if all subpages of a 2MB hugepage are
1311 * under direct I/O, the reference of the head page is 512 and a bit more.)
1312 * This means that when we try to migrate hugepage whose subpages are
1313 * doing direct I/O, some references remain after try_to_unmap() and
1314 * hugepage migration fails without data corruption.
1315 *
1316 * There is also no race when direct I/O is issued on the page under migration,
1317 * because then pte is replaced with migration swap entry and direct I/O code
1318 * will wait in the page fault for migration to complete.
1319 */
1320static int unmap_and_move_huge_page(new_page_t get_new_page,
David Rientjes68711a72014-06-04 16:08:25 -07001321 free_page_t put_new_page, unsigned long private,
1322 struct page *hpage, int force,
Yang Shidd4ae782020-12-14 19:13:06 -08001323 enum migrate_mode mode, int reason,
1324 struct list_head *ret)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001325{
Hugh Dickins2def7422015-11-05 18:49:46 -08001326 int rc = -EAGAIN;
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001327 int page_was_mapped = 0;
Joonsoo Kim32665f22014-01-21 15:51:15 -08001328 struct page *new_hpage;
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001329 struct anon_vma *anon_vma = NULL;
Mike Kravetzc0d03812020-04-01 21:11:05 -07001330 struct address_space *mapping = NULL;
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001331
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001332 /*
Anshuman Khandual7ed2c312019-03-05 15:43:44 -08001333 * Migratability of hugepages depends on architectures and their size.
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001334 * This check is necessary because some callers of hugepage migration
1335 * like soft offline and memory hotremove don't walk through page
1336 * tables or check whether the hugepage is pmd-based or not before
1337 * kicking migration.
1338 */
Naoya Horiguchi100873d2014-06-04 16:10:56 -07001339 if (!hugepage_migration_supported(page_hstate(hpage))) {
Yang Shidd4ae782020-12-14 19:13:06 -08001340 list_move_tail(&hpage->lru, ret);
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001341 return -ENOSYS;
Joonsoo Kim32665f22014-01-21 15:51:15 -08001342 }
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001343
Muchun Song71a64f62021-02-04 18:32:17 -08001344 if (page_count(hpage) == 1) {
1345 /* page was freed from under us. So we are done. */
1346 putback_active_hugepage(hpage);
1347 return MIGRATEPAGE_SUCCESS;
1348 }
1349
Michal Hocko666feb22018-04-10 16:30:03 -07001350 new_hpage = get_new_page(hpage, private);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001351 if (!new_hpage)
1352 return -ENOMEM;
1353
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001354 if (!trylock_page(hpage)) {
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07001355 if (!force)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001356 goto out;
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07001357 switch (mode) {
1358 case MIGRATE_SYNC:
1359 case MIGRATE_SYNC_NO_COPY:
1360 break;
1361 default:
1362 goto out;
1363 }
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001364 lock_page(hpage);
1365 }
1366
Mike Kravetzcb6acd02019-02-28 16:22:02 -08001367 /*
1368 * Check for pages which are in the process of being freed. Without
1369 * page_mapping() set, hugetlbfs specific move page routine will not
1370 * be called and we could leak usage counts for subpools.
1371 */
Muchun Song6acfb5b2021-06-30 18:51:29 -07001372 if (hugetlb_page_subpool(hpage) && !page_mapping(hpage)) {
Mike Kravetzcb6acd02019-02-28 16:22:02 -08001373 rc = -EBUSY;
1374 goto out_unlock;
1375 }
1376
Peter Zijlstra746b18d2011-05-24 17:12:10 -07001377 if (PageAnon(hpage))
1378 anon_vma = page_get_anon_vma(hpage);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001379
Hugh Dickins7db76712015-11-05 18:49:49 -08001380 if (unlikely(!trylock_page(new_hpage)))
1381 goto put_anon;
1382
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001383 if (page_mapped(hpage)) {
Mike Kravetz336bf302020-11-13 22:52:16 -08001384 bool mapping_locked = false;
Alistair Popplea98a2f02021-06-30 18:54:16 -07001385 enum ttu_flags ttu = 0;
Mike Kravetzc0d03812020-04-01 21:11:05 -07001386
Mike Kravetz336bf302020-11-13 22:52:16 -08001387 if (!PageAnon(hpage)) {
1388 /*
1389 * In shared mappings, try_to_unmap could potentially
1390 * call huge_pmd_unshare. Because of this, take
1391 * semaphore in write mode here and set TTU_RMAP_LOCKED
1392 * to let lower levels know we have taken the lock.
1393 */
1394 mapping = hugetlb_page_mapping_lock_write(hpage);
1395 if (unlikely(!mapping))
1396 goto unlock_put_anon;
1397
1398 mapping_locked = true;
1399 ttu |= TTU_RMAP_LOCKED;
1400 }
1401
Alistair Popplea98a2f02021-06-30 18:54:16 -07001402 try_to_migrate(hpage, ttu);
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001403 page_was_mapped = 1;
Mike Kravetz336bf302020-11-13 22:52:16 -08001404
1405 if (mapping_locked)
1406 i_mmap_unlock_write(mapping);
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001407 }
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001408
1409 if (!page_mapped(hpage))
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001410 rc = move_to_new_page(new_hpage, hpage, mode);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001411
Mike Kravetz336bf302020-11-13 22:52:16 -08001412 if (page_was_mapped)
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001413 remove_migration_ptes(hpage,
Mike Kravetz336bf302020-11-13 22:52:16 -08001414 rc == MIGRATEPAGE_SUCCESS ? new_hpage : hpage, false);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001415
Mike Kravetzc0d03812020-04-01 21:11:05 -07001416unlock_put_anon:
Hugh Dickins7db76712015-11-05 18:49:49 -08001417 unlock_page(new_hpage);
1418
1419put_anon:
Hugh Dickinsfd4a4662011-01-13 15:47:31 -08001420 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -07001421 put_anon_vma(anon_vma);
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -07001422
Hugh Dickins2def7422015-11-05 18:49:46 -08001423 if (rc == MIGRATEPAGE_SUCCESS) {
Michal Hockoab5ac902018-01-31 16:20:48 -08001424 move_hugetlb_state(hpage, new_hpage, reason);
Hugh Dickins2def7422015-11-05 18:49:46 -08001425 put_new_page = NULL;
1426 }
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -07001427
Mike Kravetzcb6acd02019-02-28 16:22:02 -08001428out_unlock:
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001429 unlock_page(hpage);
Hillf Danton09761332011-12-08 14:34:20 -08001430out:
Yang Shidd4ae782020-12-14 19:13:06 -08001431 if (rc == MIGRATEPAGE_SUCCESS)
Naoya Horiguchib8ec1ce2013-09-11 14:22:01 -07001432 putback_active_hugepage(hpage);
Miaohe Lina04840c2021-05-04 18:37:07 -07001433 else if (rc != -EAGAIN)
Yang Shidd4ae782020-12-14 19:13:06 -08001434 list_move_tail(&hpage->lru, ret);
David Rientjes68711a72014-06-04 16:08:25 -07001435
1436 /*
1437 * If migration was not successful and there's a freeing callback, use
1438 * it. Otherwise, put_page() will drop the reference grabbed during
1439 * isolation.
1440 */
Hugh Dickins2def7422015-11-05 18:49:46 -08001441 if (put_new_page)
David Rientjes68711a72014-06-04 16:08:25 -07001442 put_new_page(new_hpage, private);
1443 else
Naoya Horiguchi3aaa76e2015-09-22 14:59:14 -07001444 putback_active_hugepage(new_hpage);
David Rientjes68711a72014-06-04 16:08:25 -07001445
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001446 return rc;
1447}
1448
Yang Shid532e2e2020-12-14 19:13:16 -08001449static inline int try_split_thp(struct page *page, struct page **page2,
1450 struct list_head *from)
1451{
1452 int rc = 0;
1453
1454 lock_page(page);
1455 rc = split_huge_page_to_list(page, from);
1456 unlock_page(page);
1457 if (!rc)
1458 list_safe_reset_next(page, *page2, lru);
1459
1460 return rc;
1461}
1462
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001463/*
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001464 * migrate_pages - migrate the pages specified in a list, to the free pages
1465 * supplied as the target for the page migration
Christoph Lameterb20a3502006-03-22 00:09:12 -08001466 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001467 * @from: The list of pages to be migrated.
1468 * @get_new_page: The function used to allocate free pages to be used
1469 * as the target of the page migration.
David Rientjes68711a72014-06-04 16:08:25 -07001470 * @put_new_page: The function used to free target pages if migration
1471 * fails, or NULL if no special handling is necessary.
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001472 * @private: Private data to be passed on to get_new_page()
1473 * @mode: The migration mode that specifies the constraints for
1474 * page migration, if any.
1475 * @reason: The reason for page migration.
Yang Shi5ac95882021-09-02 14:59:13 -07001476 * @ret_succeeded: Set to the number of pages migrated successfully if
1477 * the caller passes a non-NULL pointer.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001478 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001479 * The function returns after 10 attempts or if no pages are movable any more
1480 * because the list has become empty or no retryable pages exist any more.
Yang Shidd4ae782020-12-14 19:13:06 -08001481 * It is caller's responsibility to call putback_movable_pages() to return pages
1482 * to the LRU or free list only if ret != 0.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001483 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001484 * Returns the number of pages that were not migrated, or an error code.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001485 */
Hugh Dickins9c620e22013-02-22 16:35:14 -08001486int migrate_pages(struct list_head *from, new_page_t get_new_page,
David Rientjes68711a72014-06-04 16:08:25 -07001487 free_page_t put_new_page, unsigned long private,
Yang Shi5ac95882021-09-02 14:59:13 -07001488 enum migrate_mode mode, int reason, unsigned int *ret_succeeded)
Christoph Lameterb20a3502006-03-22 00:09:12 -08001489{
Christoph Lametere24f0b82006-06-23 02:03:51 -07001490 int retry = 1;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001491 int thp_retry = 1;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001492 int nr_failed = 0;
Mel Gorman5647bc22012-10-19 10:46:20 +01001493 int nr_succeeded = 0;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001494 int nr_thp_succeeded = 0;
1495 int nr_thp_failed = 0;
1496 int nr_thp_split = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001497 int pass = 0;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001498 bool is_thp = false;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001499 struct page *page;
1500 struct page *page2;
1501 int swapwrite = current->flags & PF_SWAPWRITE;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001502 int rc, nr_subpages;
Yang Shidd4ae782020-12-14 19:13:06 -08001503 LIST_HEAD(ret_pages);
Yang Shib0b515b2021-06-30 18:51:48 -07001504 bool nosplit = (reason == MR_NUMA_MISPLACED);
Christoph Lameterb20a3502006-03-22 00:09:12 -08001505
Liam Mark7bc1aec2021-05-04 18:37:25 -07001506 trace_mm_migrate_pages_start(mode, reason);
1507
Christoph Lameterb20a3502006-03-22 00:09:12 -08001508 if (!swapwrite)
1509 current->flags |= PF_SWAPWRITE;
1510
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001511 for (pass = 0; pass < 10 && (retry || thp_retry); pass++) {
Christoph Lametere24f0b82006-06-23 02:03:51 -07001512 retry = 0;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001513 thp_retry = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001514
Christoph Lametere24f0b82006-06-23 02:03:51 -07001515 list_for_each_entry_safe(page, page2, from, lru) {
Michal Hocko94723aa2018-04-10 16:30:07 -07001516retry:
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001517 /*
1518 * THP statistics is based on the source huge page.
1519 * Capture required information that might get lost
1520 * during migration.
1521 */
Zi Yan6c5c7b92020-09-25 21:19:14 -07001522 is_thp = PageTransHuge(page) && !PageHuge(page);
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -07001523 nr_subpages = thp_nr_pages(page);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001524 cond_resched();
Christoph Lameterb20a3502006-03-22 00:09:12 -08001525
Naoya Horiguchi31caf662013-09-11 14:21:59 -07001526 if (PageHuge(page))
1527 rc = unmap_and_move_huge_page(get_new_page,
David Rientjes68711a72014-06-04 16:08:25 -07001528 put_new_page, private, page,
Yang Shidd4ae782020-12-14 19:13:06 -08001529 pass > 2, mode, reason,
1530 &ret_pages);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07001531 else
David Rientjes68711a72014-06-04 16:08:25 -07001532 rc = unmap_and_move(get_new_page, put_new_page,
Naoya Horiguchiadd05ce2015-06-24 16:56:50 -07001533 private, page, pass > 2, mode,
Yang Shidd4ae782020-12-14 19:13:06 -08001534 reason, &ret_pages);
1535 /*
1536 * The rules are:
1537 * Success: non hugetlb page will be freed, hugetlb
1538 * page will be put back
1539 * -EAGAIN: stay on the from list
1540 * -ENOMEM: stay on the from list
1541 * Other errno: put on ret_pages list then splice to
1542 * from list
1543 */
Christoph Lametere24f0b82006-06-23 02:03:51 -07001544 switch(rc) {
Yang Shid532e2e2020-12-14 19:13:16 -08001545 /*
1546 * THP migration might be unsupported or the
1547 * allocation could've failed so we should
1548 * retry on the same page with the THP split
1549 * to base pages.
1550 *
1551 * Head page is retried immediately and tail
1552 * pages are added to the tail of the list so
1553 * we encounter them after the rest of the list
1554 * is processed.
1555 */
1556 case -ENOSYS:
1557 /* THP migration is unsupported */
1558 if (is_thp) {
1559 if (!try_split_thp(page, &page2, from)) {
1560 nr_thp_split++;
1561 goto retry;
1562 }
1563
1564 nr_thp_failed++;
1565 nr_failed += nr_subpages;
1566 break;
1567 }
1568
1569 /* Hugetlb migration is unsupported */
1570 nr_failed++;
Tao Zeng54f845e2022-01-26 16:04:08 +08001571 #ifdef CONFIG_AMLOGIC_CMA
1572 cma_debug(2, page, " NO SYS\n");
1573 #endif
Yang Shid532e2e2020-12-14 19:13:16 -08001574 break;
Christoph Lameter95a402c2006-06-23 02:03:53 -07001575 case -ENOMEM:
Michal Hocko94723aa2018-04-10 16:30:07 -07001576 /*
Yang Shid532e2e2020-12-14 19:13:16 -08001577 * When memory is low, don't bother to try to migrate
1578 * other pages, just exit.
Yang Shib0b515b2021-06-30 18:51:48 -07001579 * THP NUMA faulting doesn't split THP to retry.
Michal Hocko94723aa2018-04-10 16:30:07 -07001580 */
Yang Shib0b515b2021-06-30 18:51:48 -07001581 if (is_thp && !nosplit) {
Yang Shid532e2e2020-12-14 19:13:16 -08001582 if (!try_split_thp(page, &page2, from)) {
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001583 nr_thp_split++;
Michal Hocko94723aa2018-04-10 16:30:07 -07001584 goto retry;
1585 }
Zi Yan6c5c7b92020-09-25 21:19:14 -07001586
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001587 nr_thp_failed++;
1588 nr_failed += nr_subpages;
1589 goto out;
1590 }
David Rientjesdfef2ef2016-05-20 16:59:05 -07001591 nr_failed++;
Tao Zeng54f845e2022-01-26 16:04:08 +08001592 #ifdef CONFIG_AMLOGIC_CMA
1593 cma_debug(2, page, " NO MEM\n");
1594 #endif
Christoph Lameter95a402c2006-06-23 02:03:53 -07001595 goto out;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001596 case -EAGAIN:
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001597 if (is_thp) {
1598 thp_retry++;
1599 break;
1600 }
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001601 retry++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001602 break;
Rafael Aquini78bd5202012-12-11 16:02:31 -08001603 case MIGRATEPAGE_SUCCESS:
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001604 if (is_thp) {
1605 nr_thp_succeeded++;
1606 nr_succeeded += nr_subpages;
1607 break;
1608 }
Mel Gorman5647bc22012-10-19 10:46:20 +01001609 nr_succeeded++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001610 break;
1611 default:
Naoya Horiguchi354a3362014-01-21 15:51:14 -08001612 /*
Yang Shid532e2e2020-12-14 19:13:16 -08001613 * Permanent failure (-EBUSY, etc.):
Naoya Horiguchi354a3362014-01-21 15:51:14 -08001614 * unlike -EAGAIN case, the failed page is
1615 * removed from migration page list and not
1616 * retried in the next outer loop.
1617 */
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001618 if (is_thp) {
1619 nr_thp_failed++;
1620 nr_failed += nr_subpages;
1621 break;
1622 }
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001623 nr_failed++;
Tao Zeng54f845e2022-01-26 16:04:08 +08001624 #ifdef CONFIG_AMLOGIC_CMA
1625 cma_debug(2, page, " failed:%d\n", rc);
1626 #endif
Christoph Lametere24f0b82006-06-23 02:03:51 -07001627 break;
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001628 }
Christoph Lameterb20a3502006-03-22 00:09:12 -08001629 }
1630 }
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001631 nr_failed += retry + thp_retry;
1632 nr_thp_failed += thp_retry;
Vlastimil Babkaf2f81fb2015-11-05 18:47:03 -08001633 rc = nr_failed;
Christoph Lameter95a402c2006-06-23 02:03:53 -07001634out:
Yang Shidd4ae782020-12-14 19:13:06 -08001635 /*
1636 * Put the permanent failure page back to migration list, they
1637 * will be put back to the right list by the caller.
1638 */
1639 list_splice(&ret_pages, from);
1640
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001641 count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
1642 count_vm_events(PGMIGRATE_FAIL, nr_failed);
1643 count_vm_events(THP_MIGRATION_SUCCESS, nr_thp_succeeded);
1644 count_vm_events(THP_MIGRATION_FAIL, nr_thp_failed);
1645 count_vm_events(THP_MIGRATION_SPLIT, nr_thp_split);
1646 trace_mm_migrate_pages(nr_succeeded, nr_failed, nr_thp_succeeded,
1647 nr_thp_failed, nr_thp_split, mode, reason);
Mel Gorman7b2a2d42012-10-19 14:07:31 +01001648
Christoph Lameterb20a3502006-03-22 00:09:12 -08001649 if (!swapwrite)
1650 current->flags &= ~PF_SWAPWRITE;
1651
Yang Shi5ac95882021-09-02 14:59:13 -07001652 if (ret_succeeded)
1653 *ret_succeeded = nr_succeeded;
1654
Rafael Aquini78bd5202012-12-11 16:02:31 -08001655 return rc;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001656}
Charan Teja Reddyf47b8522021-02-16 13:59:45 +05301657EXPORT_SYMBOL_GPL(migrate_pages);
Christoph Lameterb20a3502006-03-22 00:09:12 -08001658
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001659struct page *alloc_migration_target(struct page *page, unsigned long private)
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001660{
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001661 struct migration_target_control *mtc;
1662 gfp_t gfp_mask;
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001663 unsigned int order = 0;
1664 struct page *new_page = NULL;
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001665 int nid;
1666 int zidx;
1667
1668 mtc = (struct migration_target_control *)private;
1669 gfp_mask = mtc->gfp_mask;
1670 nid = mtc->nid;
1671 if (nid == NUMA_NO_NODE)
1672 nid = page_to_nid(page);
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001673
Joonsoo Kimd92bbc22020-08-11 18:37:17 -07001674 if (PageHuge(page)) {
1675 struct hstate *h = page_hstate(compound_head(page));
1676
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001677 gfp_mask = htlb_modify_alloc_mask(h, gfp_mask);
1678 return alloc_huge_page_nodemask(h, nid, mtc->nmask, gfp_mask);
Joonsoo Kimd92bbc22020-08-11 18:37:17 -07001679 }
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001680
1681 if (PageTransHuge(page)) {
Joonsoo Kim9933a0c2020-08-11 18:37:20 -07001682 /*
1683 * clear __GFP_RECLAIM to make the migration callback
1684 * consistent with regular THP allocations.
1685 */
1686 gfp_mask &= ~__GFP_RECLAIM;
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001687 gfp_mask |= GFP_TRANSHUGE;
1688 order = HPAGE_PMD_ORDER;
1689 }
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001690 zidx = zone_idx(page_zone(page));
1691 if (is_highmem_idx(zidx) || zidx == ZONE_MOVABLE)
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001692 gfp_mask |= __GFP_HIGHMEM;
1693
Matthew Wilcox (Oracle)84172f42021-04-29 23:01:15 -07001694 new_page = __alloc_pages(gfp_mask, order, nid, mtc->nmask);
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001695
1696 if (new_page && PageTransHuge(new_page))
1697 prep_transhuge_page(new_page);
1698
1699 return new_page;
1700}
1701
Christoph Lameter742755a2006-06-23 02:03:55 -07001702#ifdef CONFIG_NUMA
Christoph Lameter742755a2006-06-23 02:03:55 -07001703
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001704static int store_status(int __user *status, int start, int value, int nr)
Christoph Lameter742755a2006-06-23 02:03:55 -07001705{
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001706 while (nr-- > 0) {
1707 if (put_user(value, status + start))
1708 return -EFAULT;
1709 start++;
1710 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001711
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001712 return 0;
1713}
Christoph Lameter742755a2006-06-23 02:03:55 -07001714
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001715static int do_move_pages_to_node(struct mm_struct *mm,
1716 struct list_head *pagelist, int node)
1717{
1718 int err;
Joonsoo Kima0976312020-08-11 18:37:28 -07001719 struct migration_target_control mtc = {
1720 .nid = node,
1721 .gfp_mask = GFP_HIGHUSER_MOVABLE | __GFP_THISNODE,
1722 };
Christoph Lameter742755a2006-06-23 02:03:55 -07001723
Joonsoo Kima0976312020-08-11 18:37:28 -07001724 err = migrate_pages(pagelist, alloc_migration_target, NULL,
Yang Shi5ac95882021-09-02 14:59:13 -07001725 (unsigned long)&mtc, MIGRATE_SYNC, MR_SYSCALL, NULL);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001726 if (err)
1727 putback_movable_pages(pagelist);
1728 return err;
Christoph Lameter742755a2006-06-23 02:03:55 -07001729}
1730
1731/*
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001732 * Resolves the given address to a struct page, isolates it from the LRU and
1733 * puts it to the given pagelist.
Yang Shie0153fc2020-01-04 12:59:46 -08001734 * Returns:
1735 * errno - if the page cannot be found/isolated
1736 * 0 - when it doesn't have to be migrated because it is already on the
1737 * target node
1738 * 1 - when it has been queued
Christoph Lameter742755a2006-06-23 02:03:55 -07001739 */
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001740static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
1741 int node, struct list_head *pagelist, bool migrate_all)
Christoph Lameter742755a2006-06-23 02:03:55 -07001742{
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001743 struct vm_area_struct *vma;
1744 struct page *page;
1745 unsigned int follflags;
Christoph Lameter742755a2006-06-23 02:03:55 -07001746 int err;
Christoph Lameter742755a2006-06-23 02:03:55 -07001747
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001748 mmap_read_lock(mm);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001749 err = -EFAULT;
1750 vma = find_vma(mm, addr);
1751 if (!vma || addr < vma->vm_start || !vma_migratable(vma))
1752 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001753
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001754 /* FOLL_DUMP to ignore special (like zero) pages */
1755 follflags = FOLL_GET | FOLL_DUMP;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001756 page = follow_page(vma, addr, follflags);
Christoph Lameter742755a2006-06-23 02:03:55 -07001757
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001758 err = PTR_ERR(page);
1759 if (IS_ERR(page))
1760 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001761
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001762 err = -ENOENT;
1763 if (!page)
1764 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001765
Brice Gogline78bbfa2008-10-18 20:27:15 -07001766 err = 0;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001767 if (page_to_nid(page) == node)
1768 goto out_putpage;
Christoph Lameter742755a2006-06-23 02:03:55 -07001769
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001770 err = -EACCES;
1771 if (page_mapcount(page) > 1 && !migrate_all)
1772 goto out_putpage;
1773
1774 if (PageHuge(page)) {
1775 if (PageHead(page)) {
Miaohe Lin072e7412022-05-30 19:30:15 +08001776 err = isolate_hugetlb(page, pagelist);
1777 if (!err)
1778 err = 1;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001779 }
1780 } else {
1781 struct page *head;
1782
1783 head = compound_head(page);
1784 err = isolate_lru_page(head);
1785 if (err)
1786 goto out_putpage;
1787
Yang Shie0153fc2020-01-04 12:59:46 -08001788 err = 1;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001789 list_add_tail(&head->lru, pagelist);
1790 mod_node_page_state(page_pgdat(head),
Huang Ying9de4f222020-04-06 20:04:41 -07001791 NR_ISOLATED_ANON + page_is_file_lru(head),
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -07001792 thp_nr_pages(head));
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001793 }
1794out_putpage:
1795 /*
1796 * Either remove the duplicate refcount from
1797 * isolate_lru_page() or drop the page ref if it was
1798 * not isolated.
1799 */
1800 put_page(page);
1801out:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001802 mmap_read_unlock(mm);
Christoph Lameter742755a2006-06-23 02:03:55 -07001803 return err;
1804}
1805
Wei Yang7ca87832020-04-06 20:04:12 -07001806static int move_pages_and_store_status(struct mm_struct *mm, int node,
1807 struct list_head *pagelist, int __user *status,
1808 int start, int i, unsigned long nr_pages)
1809{
1810 int err;
1811
Wei Yang5d7ae892020-04-06 20:04:15 -07001812 if (list_empty(pagelist))
1813 return 0;
1814
Wei Yang7ca87832020-04-06 20:04:12 -07001815 err = do_move_pages_to_node(mm, pagelist, node);
1816 if (err) {
1817 /*
1818 * Positive err means the number of failed
1819 * pages to migrate. Since we are going to
1820 * abort and return the number of non-migrated
Long Liab9dd4f2020-12-14 19:12:52 -08001821 * pages, so need to include the rest of the
Wei Yang7ca87832020-04-06 20:04:12 -07001822 * nr_pages that have not been attempted as
1823 * well.
1824 */
1825 if (err > 0)
1826 err += nr_pages - i - 1;
1827 return err;
1828 }
1829 return store_status(status, start, node, i - start);
1830}
1831
Christoph Lameter742755a2006-06-23 02:03:55 -07001832/*
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001833 * Migrate an array of page address onto an array of nodes and fill
1834 * the corresponding array of status.
1835 */
Christoph Lameter3268c632012-03-21 16:34:06 -07001836static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001837 unsigned long nr_pages,
1838 const void __user * __user *pages,
1839 const int __user *nodes,
1840 int __user *status, int flags)
1841{
Gregory Price556b68d2023-10-03 10:48:56 -04001842 compat_uptr_t __user *compat_pages = (void __user *)pages;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001843 int current_node = NUMA_NO_NODE;
1844 LIST_HEAD(pagelist);
1845 int start, i;
1846 int err = 0, err1;
Brice Goglin35282a22009-06-16 15:32:43 -07001847
Minchan Kim361a2a22021-05-04 18:36:57 -07001848 lru_cache_disable();
Brice Goglin35282a22009-06-16 15:32:43 -07001849
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001850 for (i = start = 0; i < nr_pages; i++) {
1851 const void __user *p;
1852 unsigned long addr;
1853 int node;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001854
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001855 err = -EFAULT;
Gregory Price556b68d2023-10-03 10:48:56 -04001856 if (in_compat_syscall()) {
1857 compat_uptr_t cp;
1858
1859 if (get_user(cp, compat_pages + i))
1860 goto out_flush;
1861
1862 p = compat_ptr(cp);
1863 } else {
1864 if (get_user(p, pages + i))
1865 goto out_flush;
1866 }
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001867 if (get_user(node, nodes + i))
1868 goto out_flush;
Andrey Konovalov057d33892019-09-25 16:48:30 -07001869 addr = (unsigned long)untagged_addr(p);
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001870
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001871 err = -ENODEV;
1872 if (node < 0 || node >= MAX_NUMNODES)
1873 goto out_flush;
1874 if (!node_state(node, N_MEMORY))
1875 goto out_flush;
Brice Goglin3140a222009-01-06 14:38:57 -08001876
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001877 err = -EACCES;
1878 if (!node_isset(node, task_nodes))
1879 goto out_flush;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001880
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001881 if (current_node == NUMA_NO_NODE) {
1882 current_node = node;
1883 start = i;
1884 } else if (node != current_node) {
Wei Yang7ca87832020-04-06 20:04:12 -07001885 err = move_pages_and_store_status(mm, current_node,
1886 &pagelist, status, start, i, nr_pages);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001887 if (err)
1888 goto out;
1889 start = i;
1890 current_node = node;
Brice Goglin3140a222009-01-06 14:38:57 -08001891 }
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001892
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001893 /*
1894 * Errors in the page lookup or isolation are not fatal and we simply
1895 * report them via status
1896 */
1897 err = add_page_for_migration(mm, addr, current_node,
1898 &pagelist, flags & MPOL_MF_MOVE_ALL);
Yang Shie0153fc2020-01-04 12:59:46 -08001899
Wei Yangd08221a2020-04-06 20:04:18 -07001900 if (err > 0) {
Yang Shie0153fc2020-01-04 12:59:46 -08001901 /* The page is successfully queued for migration */
1902 continue;
1903 }
Brice Goglin3140a222009-01-06 14:38:57 -08001904
Wei Yangd08221a2020-04-06 20:04:18 -07001905 /*
1906 * If the page is already on the target node (!err), store the
1907 * node, otherwise, store the err.
1908 */
1909 err = store_status(status, i, err ? : current_node, 1);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001910 if (err)
1911 goto out_flush;
Brice Goglin3140a222009-01-06 14:38:57 -08001912
Wei Yang7ca87832020-04-06 20:04:12 -07001913 err = move_pages_and_store_status(mm, current_node, &pagelist,
1914 status, start, i, nr_pages);
Wei Yang4afdace2020-04-06 20:04:09 -07001915 if (err)
1916 goto out;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001917 current_node = NUMA_NO_NODE;
Brice Goglin3140a222009-01-06 14:38:57 -08001918 }
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001919out_flush:
1920 /* Make sure we do not overwrite the existing error */
Wei Yang7ca87832020-04-06 20:04:12 -07001921 err1 = move_pages_and_store_status(mm, current_node, &pagelist,
1922 status, start, i, nr_pages);
Wei Yangdfe9aa22020-01-30 22:11:14 -08001923 if (err >= 0)
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001924 err = err1;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001925out:
Minchan Kim361a2a22021-05-04 18:36:57 -07001926 lru_cache_enable();
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001927 return err;
1928}
1929
1930/*
Brice Goglin2f007e72008-10-18 20:27:16 -07001931 * Determine the nodes of an array of pages and store it in an array of status.
Christoph Lameter742755a2006-06-23 02:03:55 -07001932 */
Brice Goglin80bba122008-12-09 13:14:23 -08001933static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1934 const void __user **pages, int *status)
Christoph Lameter742755a2006-06-23 02:03:55 -07001935{
Brice Goglin2f007e72008-10-18 20:27:16 -07001936 unsigned long i;
Brice Goglin2f007e72008-10-18 20:27:16 -07001937
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001938 mmap_read_lock(mm);
Christoph Lameter742755a2006-06-23 02:03:55 -07001939
Brice Goglin2f007e72008-10-18 20:27:16 -07001940 for (i = 0; i < nr_pages; i++) {
Brice Goglin80bba122008-12-09 13:14:23 -08001941 unsigned long addr = (unsigned long)(*pages);
Christoph Lameter742755a2006-06-23 02:03:55 -07001942 struct vm_area_struct *vma;
1943 struct page *page;
KOSAKI Motohiroc095adb2008-12-16 16:06:43 +09001944 int err = -EFAULT;
Brice Goglin2f007e72008-10-18 20:27:16 -07001945
Liam Howlett059b8b42021-06-28 19:39:44 -07001946 vma = vma_lookup(mm, addr);
1947 if (!vma)
Christoph Lameter742755a2006-06-23 02:03:55 -07001948 goto set_status;
1949
Kirill A. Shutemovd8998442015-09-04 15:47:53 -07001950 /* FOLL_DUMP to ignore special (like zero) pages */
1951 page = follow_page(vma, addr, FOLL_DUMP);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001952
1953 err = PTR_ERR(page);
1954 if (IS_ERR(page))
1955 goto set_status;
1956
Kirill A. Shutemovd8998442015-09-04 15:47:53 -07001957 err = page ? page_to_nid(page) : -ENOENT;
Christoph Lameter742755a2006-06-23 02:03:55 -07001958set_status:
Brice Goglin80bba122008-12-09 13:14:23 -08001959 *status = err;
1960
1961 pages++;
1962 status++;
1963 }
1964
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001965 mmap_read_unlock(mm);
Brice Goglin80bba122008-12-09 13:14:23 -08001966}
1967
Arnd Bergmann5b1b5612021-09-08 15:18:17 -07001968static int get_compat_pages_array(const void __user *chunk_pages[],
1969 const void __user * __user *pages,
1970 unsigned long chunk_nr)
1971{
1972 compat_uptr_t __user *pages32 = (compat_uptr_t __user *)pages;
1973 compat_uptr_t p;
1974 int i;
1975
1976 for (i = 0; i < chunk_nr; i++) {
1977 if (get_user(p, pages32 + i))
1978 return -EFAULT;
1979 chunk_pages[i] = compat_ptr(p);
1980 }
1981
1982 return 0;
1983}
1984
Brice Goglin80bba122008-12-09 13:14:23 -08001985/*
1986 * Determine the nodes of a user array of pages and store it in
1987 * a user array of status.
1988 */
1989static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1990 const void __user * __user *pages,
1991 int __user *status)
1992{
1993#define DO_PAGES_STAT_CHUNK_NR 16
1994 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1995 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
Brice Goglin80bba122008-12-09 13:14:23 -08001996
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001997 while (nr_pages) {
1998 unsigned long chunk_nr;
Brice Goglin80bba122008-12-09 13:14:23 -08001999
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08002000 chunk_nr = nr_pages;
2001 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
2002 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
2003
Arnd Bergmann5b1b5612021-09-08 15:18:17 -07002004 if (in_compat_syscall()) {
2005 if (get_compat_pages_array(chunk_pages, pages,
2006 chunk_nr))
2007 break;
2008 } else {
2009 if (copy_from_user(chunk_pages, pages,
2010 chunk_nr * sizeof(*chunk_pages)))
2011 break;
2012 }
Brice Goglin80bba122008-12-09 13:14:23 -08002013
2014 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
2015
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08002016 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
2017 break;
Christoph Lameter742755a2006-06-23 02:03:55 -07002018
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08002019 pages += chunk_nr;
2020 status += chunk_nr;
2021 nr_pages -= chunk_nr;
2022 }
2023 return nr_pages ? -EFAULT : 0;
Christoph Lameter742755a2006-06-23 02:03:55 -07002024}
2025
Miaohe Lin4dc200c2020-10-17 16:14:03 -07002026static struct mm_struct *find_mm_struct(pid_t pid, nodemask_t *mem_nodes)
2027{
2028 struct task_struct *task;
2029 struct mm_struct *mm;
2030
2031 /*
2032 * There is no need to check if current process has the right to modify
2033 * the specified process when they are same.
2034 */
2035 if (!pid) {
2036 mmget(current->mm);
2037 *mem_nodes = cpuset_mems_allowed(current);
2038 return current->mm;
2039 }
2040
2041 /* Find the mm_struct */
2042 rcu_read_lock();
2043 task = find_task_by_vpid(pid);
2044 if (!task) {
2045 rcu_read_unlock();
2046 return ERR_PTR(-ESRCH);
2047 }
2048 get_task_struct(task);
2049
2050 /*
2051 * Check if this process has the right to modify the specified
2052 * process. Use the regular "ptrace_may_access()" checks.
2053 */
2054 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
2055 rcu_read_unlock();
2056 mm = ERR_PTR(-EPERM);
2057 goto out;
2058 }
2059 rcu_read_unlock();
2060
2061 mm = ERR_PTR(security_task_movememory(task));
2062 if (IS_ERR(mm))
2063 goto out;
2064 *mem_nodes = cpuset_mems_allowed(task);
2065 mm = get_task_mm(task);
2066out:
2067 put_task_struct(task);
2068 if (!mm)
2069 mm = ERR_PTR(-EINVAL);
2070 return mm;
2071}
2072
Christoph Lameter742755a2006-06-23 02:03:55 -07002073/*
2074 * Move a list of pages in the address space of the currently executing
2075 * process.
2076 */
Dominik Brodowski7addf442018-03-17 16:08:03 +01002077static int kernel_move_pages(pid_t pid, unsigned long nr_pages,
2078 const void __user * __user *pages,
2079 const int __user *nodes,
2080 int __user *status, int flags)
Christoph Lameter742755a2006-06-23 02:03:55 -07002081{
Christoph Lameter742755a2006-06-23 02:03:55 -07002082 struct mm_struct *mm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07002083 int err;
Christoph Lameter3268c632012-03-21 16:34:06 -07002084 nodemask_t task_nodes;
Christoph Lameter742755a2006-06-23 02:03:55 -07002085
2086 /* Check flags */
2087 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
2088 return -EINVAL;
2089
2090 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
2091 return -EPERM;
2092
Miaohe Lin4dc200c2020-10-17 16:14:03 -07002093 mm = find_mm_struct(pid, &task_nodes);
2094 if (IS_ERR(mm))
2095 return PTR_ERR(mm);
Sasha Levin6e8b09e2012-04-25 16:01:53 -07002096
2097 if (nodes)
2098 err = do_pages_move(mm, task_nodes, nr_pages, pages,
2099 nodes, status, flags);
2100 else
2101 err = do_pages_stat(mm, nr_pages, pages, status);
Christoph Lameter3268c632012-03-21 16:34:06 -07002102
2103 mmput(mm);
2104 return err;
Christoph Lameter742755a2006-06-23 02:03:55 -07002105}
Christoph Lameter742755a2006-06-23 02:03:55 -07002106
Dominik Brodowski7addf442018-03-17 16:08:03 +01002107SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
2108 const void __user * __user *, pages,
2109 const int __user *, nodes,
2110 int __user *, status, int, flags)
2111{
2112 return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags);
2113}
2114
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002115#ifdef CONFIG_NUMA_BALANCING
2116/*
2117 * Returns true if this is a safe migration target node for misplaced NUMA
2118 * pages. Currently it only checks the watermarks which crude
2119 */
2120static bool migrate_balanced_pgdat(struct pglist_data *pgdat,
Mel Gorman3abef4e2013-02-22 16:34:27 -08002121 unsigned long nr_migrate_pages)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002122{
2123 int z;
Mel Gorman599d0c92016-07-28 15:45:31 -07002124
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002125 for (z = pgdat->nr_zones - 1; z >= 0; z--) {
2126 struct zone *zone = pgdat->node_zones + z;
2127
2128 if (!populated_zone(zone))
2129 continue;
2130
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002131 /* Avoid waking kswapd by allocating pages_to_migrate pages. */
2132 if (!zone_watermark_ok(zone, 0,
2133 high_wmark_pages(zone) +
2134 nr_migrate_pages,
Huang Yingbfe9d002019-11-30 17:57:28 -08002135 ZONE_MOVABLE, 0))
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002136 continue;
2137 return true;
2138 }
2139 return false;
2140}
2141
2142static struct page *alloc_misplaced_dst_page(struct page *page,
Michal Hocko666feb22018-04-10 16:30:03 -07002143 unsigned long data)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002144{
2145 int nid = (int) data;
2146 struct page *newpage;
2147
Vlastimil Babka96db8002015-09-08 15:03:50 -07002148 newpage = __alloc_pages_node(nid,
Johannes Weinere97ca8e52014-03-10 15:49:43 -07002149 (GFP_HIGHUSER_MOVABLE |
2150 __GFP_THISNODE | __GFP_NOMEMALLOC |
2151 __GFP_NORETRY | __GFP_NOWARN) &
Mel Gorman8479eba2016-02-26 15:19:31 -08002152 ~__GFP_RECLAIM, 0);
Hillf Dantonbac03822012-11-27 14:46:24 +00002153
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002154 return newpage;
2155}
2156
Yang Shic5b5a3d2021-06-30 18:51:42 -07002157static struct page *alloc_misplaced_dst_page_thp(struct page *page,
2158 unsigned long data)
2159{
2160 int nid = (int) data;
2161 struct page *newpage;
2162
2163 newpage = alloc_pages_node(nid, (GFP_TRANSHUGE_LIGHT | __GFP_THISNODE),
2164 HPAGE_PMD_ORDER);
2165 if (!newpage)
2166 goto out;
2167
2168 prep_transhuge_page(newpage);
2169
2170out:
2171 return newpage;
2172}
2173
Mel Gorman1c30e012014-01-21 15:50:58 -08002174static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
Mel Gormanb32967f2012-11-19 12:35:47 +00002175{
Hugh Dickins340ef392013-02-22 16:34:33 -08002176 int page_lru;
Baolin Wang2b9b6242021-09-08 15:18:01 -07002177 int nr_pages = thp_nr_pages(page);
Mel Gormanb32967f2012-11-19 12:35:47 +00002178
Sasha Levin309381fea2014-01-23 15:52:54 -08002179 VM_BUG_ON_PAGE(compound_order(page) && !PageTransHuge(page), page);
Mel Gorman3abef4e2013-02-22 16:34:27 -08002180
Yang Shi662aeea2021-06-30 18:51:51 -07002181 /* Do not migrate THP mapped by multiple processes */
2182 if (PageTransHuge(page) && total_mapcount(page) > 1)
2183 return 0;
2184
Mel Gormanb32967f2012-11-19 12:35:47 +00002185 /* Avoid migrating to a node that is nearly full */
Baolin Wang2b9b6242021-09-08 15:18:01 -07002186 if (!migrate_balanced_pgdat(pgdat, nr_pages))
Hugh Dickins340ef392013-02-22 16:34:33 -08002187 return 0;
Mel Gormanb32967f2012-11-19 12:35:47 +00002188
Hugh Dickins340ef392013-02-22 16:34:33 -08002189 if (isolate_lru_page(page))
2190 return 0;
Mel Gormanb32967f2012-11-19 12:35:47 +00002191
Huang Ying9de4f222020-04-06 20:04:41 -07002192 page_lru = page_is_file_lru(page);
Mel Gorman599d0c92016-07-28 15:45:31 -07002193 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_lru,
Baolin Wang2b9b6242021-09-08 15:18:01 -07002194 nr_pages);
Hugh Dickins340ef392013-02-22 16:34:33 -08002195
2196 /*
2197 * Isolating the page has taken another reference, so the
2198 * caller's reference can be safely dropped without the page
2199 * disappearing underneath us during migration.
Mel Gormanb32967f2012-11-19 12:35:47 +00002200 */
2201 put_page(page);
Hugh Dickins340ef392013-02-22 16:34:33 -08002202 return 1;
Mel Gormanb32967f2012-11-19 12:35:47 +00002203}
2204
Mel Gormana8f60772012-11-14 21:41:46 +00002205/*
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002206 * Attempt to migrate a misplaced page to the specified destination
2207 * node. Caller is expected to have an elevated reference count on
2208 * the page that will be dropped by this function before returning.
2209 */
Mel Gorman1bc115d2013-10-07 11:29:05 +01002210int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma,
2211 int node)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002212{
Mel Gormana8f60772012-11-14 21:41:46 +00002213 pg_data_t *pgdat = NODE_DATA(node);
Hugh Dickins340ef392013-02-22 16:34:33 -08002214 int isolated;
Mel Gormanb32967f2012-11-19 12:35:47 +00002215 int nr_remaining;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002216 LIST_HEAD(migratepages);
Yang Shic5b5a3d2021-06-30 18:51:42 -07002217 new_page_t *new;
2218 bool compound;
Aneesh Kumar K.Vb5916c02021-07-29 14:53:47 -07002219 int nr_pages = thp_nr_pages(page);
Yang Shic5b5a3d2021-06-30 18:51:42 -07002220
2221 /*
2222 * PTE mapped THP or HugeTLB page can't reach here so the page could
2223 * be either base page or THP. And it must be head page if it is
2224 * THP.
2225 */
2226 compound = PageTransHuge(page);
2227
2228 if (compound)
2229 new = alloc_misplaced_dst_page_thp;
2230 else
2231 new = alloc_misplaced_dst_page;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002232
2233 /*
Mel Gorman1bc115d2013-10-07 11:29:05 +01002234 * Don't migrate file pages that are mapped in multiple processes
2235 * with execute permissions as they are probably shared libraries.
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002236 */
Miaohe Lin7ee820e2021-05-04 18:37:16 -07002237 if (page_mapcount(page) != 1 && page_is_file_lru(page) &&
2238 (vma->vm_flags & VM_EXEC))
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002239 goto out;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002240
Mel Gormana8f60772012-11-14 21:41:46 +00002241 /*
Mel Gorman09a913a2018-04-10 16:29:20 -07002242 * Also do not migrate dirty pages as not all filesystems can move
2243 * dirty pages in MIGRATE_ASYNC mode which is a waste of cycles.
2244 */
Huang Ying9de4f222020-04-06 20:04:41 -07002245 if (page_is_file_lru(page) && PageDirty(page))
Mel Gorman09a913a2018-04-10 16:29:20 -07002246 goto out;
2247
Mel Gormanb32967f2012-11-19 12:35:47 +00002248 isolated = numamigrate_isolate_page(pgdat, page);
2249 if (!isolated)
2250 goto out;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002251
Mel Gormanb32967f2012-11-19 12:35:47 +00002252 list_add(&page->lru, &migratepages);
Yang Shic5b5a3d2021-06-30 18:51:42 -07002253 nr_remaining = migrate_pages(&migratepages, *new, NULL, node,
Yang Shi5ac95882021-09-02 14:59:13 -07002254 MIGRATE_ASYNC, MR_NUMA_MISPLACED, NULL);
Mel Gormanb32967f2012-11-19 12:35:47 +00002255 if (nr_remaining) {
Joonsoo Kim59c82b72014-01-21 15:51:17 -08002256 if (!list_empty(&migratepages)) {
2257 list_del(&page->lru);
Yang Shic5fc5c32021-06-30 18:51:45 -07002258 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
2259 page_is_file_lru(page), -nr_pages);
Joonsoo Kim59c82b72014-01-21 15:51:17 -08002260 putback_lru_page(page);
2261 }
Mel Gormanb32967f2012-11-19 12:35:47 +00002262 isolated = 0;
2263 } else
Yang Shic5fc5c32021-06-30 18:51:45 -07002264 count_vm_numa_events(NUMA_PAGE_MIGRATE, nr_pages);
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002265 BUG_ON(!list_empty(&migratepages));
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002266 return isolated;
Hugh Dickins340ef392013-02-22 16:34:33 -08002267
2268out:
2269 put_page(page);
2270 return 0;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002271}
Mel Gorman220018d2012-12-05 09:32:56 +00002272#endif /* CONFIG_NUMA_BALANCING */
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002273#endif /* CONFIG_NUMA */
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002274
Christoph Hellwig9b2ed9c2019-08-14 09:59:28 +02002275#ifdef CONFIG_DEVICE_PRIVATE
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002276static int migrate_vma_collect_skip(unsigned long start,
2277 unsigned long end,
2278 struct mm_walk *walk)
2279{
2280 struct migrate_vma *migrate = walk->private;
2281 unsigned long addr;
2282
Ralph Campbell872ea702020-01-30 22:14:38 -08002283 for (addr = start; addr < end; addr += PAGE_SIZE) {
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002284 migrate->dst[migrate->npages] = 0;
2285 migrate->src[migrate->npages++] = 0;
2286 }
2287
2288 return 0;
2289}
2290
Miaohe Lin843e1be2021-05-04 18:37:13 -07002291static int migrate_vma_collect_hole(unsigned long start,
2292 unsigned long end,
2293 __always_unused int depth,
2294 struct mm_walk *walk)
2295{
2296 struct migrate_vma *migrate = walk->private;
2297 unsigned long addr;
2298
2299 /* Only allow populating anonymous memory. */
2300 if (!vma_is_anonymous(walk->vma))
2301 return migrate_vma_collect_skip(start, end, walk);
2302
2303 for (addr = start; addr < end; addr += PAGE_SIZE) {
2304 migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE;
2305 migrate->dst[migrate->npages] = 0;
2306 migrate->npages++;
2307 migrate->cpages++;
2308 }
2309
2310 return 0;
2311}
2312
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002313static int migrate_vma_collect_pmd(pmd_t *pmdp,
2314 unsigned long start,
2315 unsigned long end,
2316 struct mm_walk *walk)
2317{
2318 struct migrate_vma *migrate = walk->private;
2319 struct vm_area_struct *vma = walk->vma;
2320 struct mm_struct *mm = vma->vm_mm;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002321 unsigned long addr = start, unmapped = 0;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002322 spinlock_t *ptl;
2323 pte_t *ptep;
2324
2325again:
2326 if (pmd_none(*pmdp))
Steven Priceb7a16c72020-02-03 17:36:03 -08002327 return migrate_vma_collect_hole(start, end, -1, walk);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002328
2329 if (pmd_trans_huge(*pmdp)) {
2330 struct page *page;
2331
2332 ptl = pmd_lock(mm, pmdp);
2333 if (unlikely(!pmd_trans_huge(*pmdp))) {
2334 spin_unlock(ptl);
2335 goto again;
2336 }
2337
2338 page = pmd_page(*pmdp);
2339 if (is_huge_zero_page(page)) {
2340 spin_unlock(ptl);
2341 split_huge_pmd(vma, pmdp, addr);
2342 if (pmd_trans_unstable(pmdp))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002343 return migrate_vma_collect_skip(start, end,
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002344 walk);
2345 } else {
2346 int ret;
2347
2348 get_page(page);
2349 spin_unlock(ptl);
2350 if (unlikely(!trylock_page(page)))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002351 return migrate_vma_collect_skip(start, end,
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002352 walk);
2353 ret = split_huge_page(page);
2354 unlock_page(page);
2355 put_page(page);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002356 if (ret)
2357 return migrate_vma_collect_skip(start, end,
2358 walk);
2359 if (pmd_none(*pmdp))
Steven Priceb7a16c72020-02-03 17:36:03 -08002360 return migrate_vma_collect_hole(start, end, -1,
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002361 walk);
2362 }
2363 }
2364
2365 if (unlikely(pmd_bad(*pmdp)))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002366 return migrate_vma_collect_skip(start, end, walk);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002367
2368 ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002369 arch_enter_lazy_mmu_mode();
2370
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002371 for (; addr < end; addr += PAGE_SIZE, ptep++) {
Christoph Hellwig800bb1c2020-03-16 20:32:14 +01002372 unsigned long mpfn = 0, pfn;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002373 struct page *page;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002374 swp_entry_t entry;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002375 pte_t pte;
2376
2377 pte = *ptep;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002378
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002379 if (pte_none(pte)) {
Ralph Campbell0744f282020-08-11 18:31:41 -07002380 if (vma_is_anonymous(vma)) {
2381 mpfn = MIGRATE_PFN_MIGRATE;
2382 migrate->cpages++;
2383 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002384 goto next;
2385 }
2386
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002387 if (!pte_present(pte)) {
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002388 /*
2389 * Only care about unaddressable device page special
2390 * page table entry. Other special swap entries are not
2391 * migratable, and we ignore regular swapped page.
2392 */
2393 entry = pte_to_swp_entry(pte);
2394 if (!is_device_private_entry(entry))
2395 goto next;
2396
Alistair Poppleaf5cdaf2021-06-30 18:54:06 -07002397 page = pfn_swap_entry_to_page(entry);
Ralph Campbell51431922020-07-23 15:30:00 -07002398 if (!(migrate->flags &
2399 MIGRATE_VMA_SELECT_DEVICE_PRIVATE) ||
2400 page->pgmap->owner != migrate->pgmap_owner)
Christoph Hellwig800bb1c2020-03-16 20:32:14 +01002401 goto next;
2402
Christoph Hellwig06d462b2019-08-14 09:59:27 +02002403 mpfn = migrate_pfn(page_to_pfn(page)) |
2404 MIGRATE_PFN_MIGRATE;
Alistair Popple4dd845b2021-06-30 18:54:09 -07002405 if (is_writable_device_private_entry(entry))
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002406 mpfn |= MIGRATE_PFN_WRITE;
2407 } else {
Ralph Campbell51431922020-07-23 15:30:00 -07002408 if (!(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM))
Christoph Hellwig800bb1c2020-03-16 20:32:14 +01002409 goto next;
Pingfan Liu276f7562019-09-23 15:37:38 -07002410 pfn = pte_pfn(pte);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002411 if (is_zero_pfn(pfn)) {
2412 mpfn = MIGRATE_PFN_MIGRATE;
2413 migrate->cpages++;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002414 goto next;
2415 }
Christoph Hellwig25b29952019-06-13 22:50:49 +02002416 page = vm_normal_page(migrate->vma, addr, pte);
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002417 mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
2418 mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0;
2419 }
2420
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002421 /* FIXME support THP */
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002422 if (!page || !page->mapping || PageTransCompound(page)) {
Pingfan Liu276f7562019-09-23 15:37:38 -07002423 mpfn = 0;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002424 goto next;
2425 }
2426
2427 /*
2428 * By getting a reference on the page we pin it and that blocks
2429 * any kind of migration. Side effect is that it "freezes" the
2430 * pte.
2431 *
2432 * We drop this reference after isolating the page from the lru
2433 * for non device page (device page are not on the lru and thus
2434 * can't be dropped from it).
2435 */
2436 get_page(page);
2437 migrate->cpages++;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002438
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002439 /*
2440 * Optimize for the common case where page is only mapped once
2441 * in one process. If we can lock the page, then we can safely
2442 * set up a special migration page table entry now.
2443 */
2444 if (trylock_page(page)) {
2445 pte_t swp_pte;
2446
2447 mpfn |= MIGRATE_PFN_LOCKED;
2448 ptep_get_and_clear(mm, addr, ptep);
2449
2450 /* Setup special migration page table entry */
Alistair Popple4dd845b2021-06-30 18:54:09 -07002451 if (mpfn & MIGRATE_PFN_WRITE)
2452 entry = make_writable_migration_entry(
2453 page_to_pfn(page));
2454 else
2455 entry = make_readable_migration_entry(
2456 page_to_pfn(page));
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002457 swp_pte = swp_entry_to_pte(entry);
Alistair Popplead7df762020-09-04 16:36:01 -07002458 if (pte_present(pte)) {
2459 if (pte_soft_dirty(pte))
2460 swp_pte = pte_swp_mksoft_dirty(swp_pte);
2461 if (pte_uffd_wp(pte))
2462 swp_pte = pte_swp_mkuffd_wp(swp_pte);
2463 } else {
2464 if (pte_swp_soft_dirty(pte))
2465 swp_pte = pte_swp_mksoft_dirty(swp_pte);
2466 if (pte_swp_uffd_wp(pte))
2467 swp_pte = pte_swp_mkuffd_wp(swp_pte);
2468 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002469 set_pte_at(mm, addr, ptep, swp_pte);
2470
2471 /*
2472 * This is like regular unmap: we remove the rmap and
2473 * drop page refcount. Page won't be freed, as we took
2474 * a reference just above.
2475 */
2476 page_remove_rmap(page, false);
2477 put_page(page);
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002478
2479 if (pte_present(pte))
2480 unmapped++;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002481 }
2482
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002483next:
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002484 migrate->dst[migrate->npages] = 0;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002485 migrate->src[migrate->npages++] = mpfn;
2486 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002487
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002488 /* Only flush the TLB if we actually modified any entries */
2489 if (unmapped)
2490 flush_tlb_range(walk->vma, start, end);
2491
Alistair Popple1299c112022-09-02 10:35:51 +10002492 arch_leave_lazy_mmu_mode();
2493 pte_unmap_unlock(ptep - 1, ptl);
2494
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002495 return 0;
2496}
2497
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02002498static const struct mm_walk_ops migrate_vma_walk_ops = {
2499 .pmd_entry = migrate_vma_collect_pmd,
2500 .pte_hole = migrate_vma_collect_hole,
2501};
2502
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002503/*
2504 * migrate_vma_collect() - collect pages over a range of virtual addresses
2505 * @migrate: migrate struct containing all migration information
2506 *
2507 * This will walk the CPU page table. For each virtual address backed by a
2508 * valid page, it updates the src array and takes a reference on the page, in
2509 * order to pin the page until we lock it and unmap it.
2510 */
2511static void migrate_vma_collect(struct migrate_vma *migrate)
2512{
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002513 struct mmu_notifier_range range;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002514
Ralph Campbell998427b2020-07-23 15:30:01 -07002515 /*
2516 * Note that the pgmap_owner is passed to the mmu notifier callback so
2517 * that the registered device driver can skip invalidating device
2518 * private page mappings that won't be migrated.
2519 */
Alistair Popple6b49bf62021-06-30 18:54:19 -07002520 mmu_notifier_range_init_owner(&range, MMU_NOTIFY_MIGRATE, 0,
2521 migrate->vma, migrate->vma->vm_mm, migrate->start, migrate->end,
Ralph Campbellc1a06df2020-08-06 23:17:09 -07002522 migrate->pgmap_owner);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002523 mmu_notifier_invalidate_range_start(&range);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002524
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02002525 walk_page_range(migrate->vma->vm_mm, migrate->start, migrate->end,
2526 &migrate_vma_walk_ops, migrate);
2527
2528 mmu_notifier_invalidate_range_end(&range);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002529 migrate->end = migrate->start + (migrate->npages << PAGE_SHIFT);
2530}
2531
2532/*
2533 * migrate_vma_check_page() - check if page is pinned or not
2534 * @page: struct page to check
2535 *
2536 * Pinned pages cannot be migrated. This is the same test as in
2537 * migrate_page_move_mapping(), except that here we allow migration of a
2538 * ZONE_DEVICE page.
2539 */
2540static bool migrate_vma_check_page(struct page *page)
2541{
2542 /*
2543 * One extra ref because caller holds an extra reference, either from
2544 * isolate_lru_page() for a regular page, or migrate_vma_collect() for
2545 * a device page.
2546 */
2547 int extra = 1;
2548
2549 /*
2550 * FIXME support THP (transparent huge page), it is bit more complex to
2551 * check them than regular pages, because they can be mapped with a pmd
2552 * or with a pte (split pte mapping).
2553 */
2554 if (PageCompound(page))
2555 return false;
2556
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002557 /* Page from ZONE_DEVICE have one extra reference */
2558 if (is_zone_device_page(page)) {
2559 /*
2560 * Private page can never be pin as they have no valid pte and
2561 * GUP will fail for those. Yet if there is a pending migration
2562 * a thread might try to wait on the pte migration entry and
2563 * will bump the page reference count. Sadly there is no way to
2564 * differentiate a regular pin from migration wait. Hence to
2565 * avoid 2 racing thread trying to migrate back to CPU to enter
Haitao Shi8958b242020-12-15 20:47:26 -08002566 * infinite loop (one stopping migration because the other is
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002567 * waiting on pte migration entry). We always return true here.
2568 *
2569 * FIXME proper solution is to rework migration_entry_wait() so
2570 * it does not need to take a reference on page.
2571 */
Christoph Hellwig25b29952019-06-13 22:50:49 +02002572 return is_device_private_page(page);
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002573 }
2574
Jérôme Glissedf6ad692017-09-08 16:12:24 -07002575 /* For file back page */
2576 if (page_mapping(page))
2577 extra += 1 + page_has_private(page);
2578
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002579 if ((page_count(page) - extra) > page_mapcount(page))
2580 return false;
2581
2582 return true;
2583}
2584
2585/*
2586 * migrate_vma_prepare() - lock pages and isolate them from the lru
2587 * @migrate: migrate struct containing all migration information
2588 *
2589 * This locks pages that have been collected by migrate_vma_collect(). Once each
2590 * page is locked it is isolated from the lru (for non-device pages). Finally,
2591 * the ref taken by migrate_vma_collect() is dropped, as locked pages cannot be
2592 * migrated by concurrent kernel threads.
2593 */
2594static void migrate_vma_prepare(struct migrate_vma *migrate)
2595{
2596 const unsigned long npages = migrate->npages;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002597 const unsigned long start = migrate->start;
2598 unsigned long addr, i, restore = 0;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002599 bool allow_drain = true;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002600
2601 lru_add_drain();
2602
2603 for (i = 0; (i < npages) && migrate->cpages; i++) {
2604 struct page *page = migrate_pfn_to_page(migrate->src[i]);
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002605 bool remap = true;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002606
2607 if (!page)
2608 continue;
2609
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002610 if (!(migrate->src[i] & MIGRATE_PFN_LOCKED)) {
2611 /*
2612 * Because we are migrating several pages there can be
2613 * a deadlock between 2 concurrent migration where each
2614 * are waiting on each other page lock.
2615 *
2616 * Make migrate_vma() a best effort thing and backoff
2617 * for any page we can not lock right away.
2618 */
2619 if (!trylock_page(page)) {
2620 migrate->src[i] = 0;
2621 migrate->cpages--;
2622 put_page(page);
2623 continue;
2624 }
2625 remap = false;
2626 migrate->src[i] |= MIGRATE_PFN_LOCKED;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002627 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002628
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002629 /* ZONE_DEVICE pages are not on LRU */
2630 if (!is_zone_device_page(page)) {
2631 if (!PageLRU(page) && allow_drain) {
2632 /* Drain CPU's pagevec */
2633 lru_add_drain_all();
2634 allow_drain = false;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002635 }
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002636
2637 if (isolate_lru_page(page)) {
2638 if (remap) {
2639 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2640 migrate->cpages--;
2641 restore++;
2642 } else {
2643 migrate->src[i] = 0;
2644 unlock_page(page);
2645 migrate->cpages--;
2646 put_page(page);
2647 }
2648 continue;
2649 }
2650
2651 /* Drop the reference we took in collect */
2652 put_page(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002653 }
2654
2655 if (!migrate_vma_check_page(page)) {
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002656 if (remap) {
2657 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2658 migrate->cpages--;
2659 restore++;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002660
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002661 if (!is_zone_device_page(page)) {
2662 get_page(page);
2663 putback_lru_page(page);
2664 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002665 } else {
2666 migrate->src[i] = 0;
2667 unlock_page(page);
2668 migrate->cpages--;
2669
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002670 if (!is_zone_device_page(page))
2671 putback_lru_page(page);
2672 else
2673 put_page(page);
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002674 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002675 }
2676 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002677
2678 for (i = 0, addr = start; i < npages && restore; i++, addr += PAGE_SIZE) {
2679 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2680
2681 if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE))
2682 continue;
2683
2684 remove_migration_pte(page, migrate->vma, addr, page);
2685
2686 migrate->src[i] = 0;
2687 unlock_page(page);
2688 put_page(page);
2689 restore--;
2690 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002691}
2692
2693/*
2694 * migrate_vma_unmap() - replace page mapping with special migration pte entry
2695 * @migrate: migrate struct containing all migration information
2696 *
2697 * Replace page mapping (CPU page table pte) with a special migration pte entry
2698 * and check again if it has been pinned. Pinned pages are restored because we
2699 * cannot migrate them.
2700 *
2701 * This is the last step before we call the device driver callback to allocate
2702 * destination memory and copy contents of original page over to new page.
2703 */
2704static void migrate_vma_unmap(struct migrate_vma *migrate)
2705{
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002706 const unsigned long npages = migrate->npages;
2707 const unsigned long start = migrate->start;
2708 unsigned long addr, i, restore = 0;
2709
2710 for (i = 0; i < npages; i++) {
2711 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2712
2713 if (!page || !(migrate->src[i] & MIGRATE_PFN_MIGRATE))
2714 continue;
2715
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002716 if (page_mapped(page)) {
Alistair Popplea98a2f02021-06-30 18:54:16 -07002717 try_to_migrate(page, 0);
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002718 if (page_mapped(page))
2719 goto restore;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002720 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002721
2722 if (migrate_vma_check_page(page))
2723 continue;
2724
2725restore:
2726 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2727 migrate->cpages--;
2728 restore++;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002729 }
2730
2731 for (addr = start, i = 0; i < npages && restore; addr += PAGE_SIZE, i++) {
2732 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2733
2734 if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE))
2735 continue;
2736
2737 remove_migration_ptes(page, page, false);
2738
2739 migrate->src[i] = 0;
2740 unlock_page(page);
2741 restore--;
2742
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002743 if (is_zone_device_page(page))
2744 put_page(page);
2745 else
2746 putback_lru_page(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002747 }
2748}
2749
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002750/**
2751 * migrate_vma_setup() - prepare to migrate a range of memory
Randy Dunlapeaf444d2020-08-11 18:33:08 -07002752 * @args: contains the vma, start, and pfns arrays for the migration
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002753 *
2754 * Returns: negative errno on failures, 0 when 0 or more pages were migrated
2755 * without an error.
2756 *
2757 * Prepare to migrate a range of memory virtual address range by collecting all
2758 * the pages backing each virtual address in the range, saving them inside the
2759 * src array. Then lock those pages and unmap them. Once the pages are locked
2760 * and unmapped, check whether each page is pinned or not. Pages that aren't
2761 * pinned have the MIGRATE_PFN_MIGRATE flag set (by this function) in the
2762 * corresponding src array entry. Then restores any pages that are pinned, by
2763 * remapping and unlocking those pages.
2764 *
2765 * The caller should then allocate destination memory and copy source memory to
2766 * it for all those entries (ie with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE
2767 * flag set). Once these are allocated and copied, the caller must update each
2768 * corresponding entry in the dst array with the pfn value of the destination
2769 * page and with the MIGRATE_PFN_VALID and MIGRATE_PFN_LOCKED flags set
2770 * (destination pages must have their struct pages locked, via lock_page()).
2771 *
2772 * Note that the caller does not have to migrate all the pages that are marked
2773 * with MIGRATE_PFN_MIGRATE flag in src array unless this is a migration from
2774 * device memory to system memory. If the caller cannot migrate a device page
2775 * back to system memory, then it must return VM_FAULT_SIGBUS, which has severe
2776 * consequences for the userspace process, so it must be avoided if at all
2777 * possible.
2778 *
2779 * For empty entries inside CPU page table (pte_none() or pmd_none() is true) we
2780 * do set MIGRATE_PFN_MIGRATE flag inside the corresponding source array thus
Ingo Molnarf0953a12021-05-06 18:06:47 -07002781 * allowing the caller to allocate device memory for those unbacked virtual
2782 * addresses. For this the caller simply has to allocate device memory and
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002783 * properly set the destination entry like for regular migration. Note that
Ingo Molnarf0953a12021-05-06 18:06:47 -07002784 * this can still fail, and thus inside the device driver you must check if the
2785 * migration was successful for those entries after calling migrate_vma_pages(),
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002786 * just like for regular migration.
2787 *
2788 * After that, the callers must call migrate_vma_pages() to go over each entry
2789 * in the src array that has the MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag
2790 * set. If the corresponding entry in dst array has MIGRATE_PFN_VALID flag set,
2791 * then migrate_vma_pages() to migrate struct page information from the source
2792 * struct page to the destination struct page. If it fails to migrate the
2793 * struct page information, then it clears the MIGRATE_PFN_MIGRATE flag in the
2794 * src array.
2795 *
2796 * At this point all successfully migrated pages have an entry in the src
2797 * array with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag set and the dst
2798 * array entry with MIGRATE_PFN_VALID flag set.
2799 *
2800 * Once migrate_vma_pages() returns the caller may inspect which pages were
2801 * successfully migrated, and which were not. Successfully migrated pages will
2802 * have the MIGRATE_PFN_MIGRATE flag set for their src array entry.
2803 *
2804 * It is safe to update device page table after migrate_vma_pages() because
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002805 * both destination and source page are still locked, and the mmap_lock is held
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002806 * in read mode (hence no one can unmap the range being migrated).
2807 *
2808 * Once the caller is done cleaning up things and updating its page table (if it
2809 * chose to do so, this is not an obligation) it finally calls
2810 * migrate_vma_finalize() to update the CPU page table to point to new pages
2811 * for successfully migrated pages or otherwise restore the CPU page table to
2812 * point to the original source pages.
2813 */
2814int migrate_vma_setup(struct migrate_vma *args)
2815{
2816 long nr_pages = (args->end - args->start) >> PAGE_SHIFT;
2817
2818 args->start &= PAGE_MASK;
2819 args->end &= PAGE_MASK;
2820 if (!args->vma || is_vm_hugetlb_page(args->vma) ||
2821 (args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma))
2822 return -EINVAL;
2823 if (nr_pages <= 0)
2824 return -EINVAL;
2825 if (args->start < args->vma->vm_start ||
2826 args->start >= args->vma->vm_end)
2827 return -EINVAL;
2828 if (args->end <= args->vma->vm_start || args->end > args->vma->vm_end)
2829 return -EINVAL;
2830 if (!args->src || !args->dst)
2831 return -EINVAL;
2832
2833 memset(args->src, 0, sizeof(*args->src) * nr_pages);
2834 args->cpages = 0;
2835 args->npages = 0;
2836
2837 migrate_vma_collect(args);
2838
2839 if (args->cpages)
2840 migrate_vma_prepare(args);
2841 if (args->cpages)
2842 migrate_vma_unmap(args);
2843
2844 /*
2845 * At this point pages are locked and unmapped, and thus they have
2846 * stable content and can safely be copied to destination memory that
2847 * is allocated by the drivers.
2848 */
2849 return 0;
2850
2851}
2852EXPORT_SYMBOL(migrate_vma_setup);
2853
Ralph Campbell34290e22020-01-30 22:14:44 -08002854/*
2855 * This code closely matches the code in:
2856 * __handle_mm_fault()
2857 * handle_pte_fault()
2858 * do_anonymous_page()
2859 * to map in an anonymous zero page but the struct page will be a ZONE_DEVICE
2860 * private page.
2861 */
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002862static void migrate_vma_insert_page(struct migrate_vma *migrate,
2863 unsigned long addr,
2864 struct page *page,
Stephen Zhangd85c6db2020-12-14 19:13:20 -08002865 unsigned long *src)
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002866{
2867 struct vm_area_struct *vma = migrate->vma;
2868 struct mm_struct *mm = vma->vm_mm;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002869 bool flush = false;
2870 spinlock_t *ptl;
2871 pte_t entry;
2872 pgd_t *pgdp;
2873 p4d_t *p4dp;
2874 pud_t *pudp;
2875 pmd_t *pmdp;
2876 pte_t *ptep;
2877
2878 /* Only allow populating anonymous memory */
2879 if (!vma_is_anonymous(vma))
2880 goto abort;
2881
2882 pgdp = pgd_offset(mm, addr);
2883 p4dp = p4d_alloc(mm, pgdp, addr);
2884 if (!p4dp)
2885 goto abort;
2886 pudp = pud_alloc(mm, p4dp, addr);
2887 if (!pudp)
2888 goto abort;
2889 pmdp = pmd_alloc(mm, pudp, addr);
2890 if (!pmdp)
2891 goto abort;
2892
2893 if (pmd_trans_huge(*pmdp) || pmd_devmap(*pmdp))
2894 goto abort;
2895
2896 /*
2897 * Use pte_alloc() instead of pte_alloc_map(). We can't run
2898 * pte_offset_map() on pmds where a huge pmd might be created
2899 * from a different thread.
2900 *
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002901 * pte_alloc_map() is safe to use under mmap_write_lock(mm) or when
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002902 * parallel threads are excluded by other means.
2903 *
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002904 * Here we only have mmap_read_lock(mm).
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002905 */
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -08002906 if (pte_alloc(mm, pmdp))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002907 goto abort;
2908
2909 /* See the comment in pte_alloc_one_map() */
2910 if (unlikely(pmd_trans_unstable(pmdp)))
2911 goto abort;
2912
2913 if (unlikely(anon_vma_prepare(vma)))
2914 goto abort;
Johannes Weinerd9eb1ea2020-06-03 16:02:24 -07002915 if (mem_cgroup_charge(page, vma->vm_mm, GFP_KERNEL))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002916 goto abort;
2917
2918 /*
2919 * The memory barrier inside __SetPageUptodate makes sure that
2920 * preceding stores to the page contents become visible before
2921 * the set_pte_at() write.
2922 */
2923 __SetPageUptodate(page);
2924
Jérôme Glissedf6ad692017-09-08 16:12:24 -07002925 if (is_zone_device_page(page)) {
2926 if (is_device_private_page(page)) {
2927 swp_entry_t swp_entry;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002928
Alistair Popple4dd845b2021-06-30 18:54:09 -07002929 if (vma->vm_flags & VM_WRITE)
2930 swp_entry = make_writable_device_private_entry(
2931 page_to_pfn(page));
2932 else
2933 swp_entry = make_readable_device_private_entry(
2934 page_to_pfn(page));
Jérôme Glissedf6ad692017-09-08 16:12:24 -07002935 entry = swp_entry_to_pte(swp_entry);
Miaohe Lin34f5e9b2021-05-04 18:37:10 -07002936 } else {
2937 /*
2938 * For now we only support migrating to un-addressable
2939 * device memory.
2940 */
2941 pr_warn_once("Unsupported ZONE_DEVICE page type.\n");
2942 goto abort;
Jérôme Glissedf6ad692017-09-08 16:12:24 -07002943 }
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002944 } else {
2945 entry = mk_pte(page, vma->vm_page_prot);
2946 if (vma->vm_flags & VM_WRITE)
2947 entry = pte_mkwrite(pte_mkdirty(entry));
2948 }
2949
2950 ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
2951
Ralph Campbell34290e22020-01-30 22:14:44 -08002952 if (check_stable_address_space(mm))
2953 goto unlock_abort;
2954
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002955 if (pte_present(*ptep)) {
2956 unsigned long pfn = pte_pfn(*ptep);
2957
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002958 if (!is_zero_pfn(pfn))
2959 goto unlock_abort;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002960 flush = true;
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002961 } else if (!pte_none(*ptep))
2962 goto unlock_abort;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002963
2964 /*
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002965 * Check for userfaultfd but do not deliver the fault. Instead,
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002966 * just back off.
2967 */
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002968 if (userfaultfd_missing(vma))
2969 goto unlock_abort;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002970
2971 inc_mm_counter(mm, MM_ANONPAGES);
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07002972 page_add_new_anon_rmap(page, vma, addr, false);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002973 if (!is_zone_device_page(page))
Joonsoo Kimb5181542020-08-11 18:30:40 -07002974 lru_cache_add_inactive_or_unevictable(page, vma);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002975 get_page(page);
2976
2977 if (flush) {
2978 flush_cache_page(vma, addr, pte_pfn(*ptep));
2979 ptep_clear_flush_notify(vma, addr, ptep);
2980 set_pte_at_notify(mm, addr, ptep, entry);
2981 update_mmu_cache(vma, addr, ptep);
2982 } else {
2983 /* No need to invalidate - it was non-present before */
2984 set_pte_at(mm, addr, ptep, entry);
2985 update_mmu_cache(vma, addr, ptep);
2986 }
2987
2988 pte_unmap_unlock(ptep, ptl);
2989 *src = MIGRATE_PFN_MIGRATE;
2990 return;
2991
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002992unlock_abort:
2993 pte_unmap_unlock(ptep, ptl);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002994abort:
2995 *src &= ~MIGRATE_PFN_MIGRATE;
2996}
2997
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002998/**
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002999 * migrate_vma_pages() - migrate meta-data from src page to dst page
3000 * @migrate: migrate struct containing all migration information
3001 *
3002 * This migrates struct page meta-data from source struct page to destination
3003 * struct page. This effectively finishes the migration from source page to the
3004 * destination page.
3005 */
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003006void migrate_vma_pages(struct migrate_vma *migrate)
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003007{
3008 const unsigned long npages = migrate->npages;
3009 const unsigned long start = migrate->start;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003010 struct mmu_notifier_range range;
3011 unsigned long addr, i;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003012 bool notified = false;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003013
3014 for (i = 0, addr = start; i < npages; addr += PAGE_SIZE, i++) {
3015 struct page *newpage = migrate_pfn_to_page(migrate->dst[i]);
3016 struct page *page = migrate_pfn_to_page(migrate->src[i]);
3017 struct address_space *mapping;
3018 int r;
3019
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003020 if (!newpage) {
3021 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003022 continue;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003023 }
3024
3025 if (!page) {
Ralph Campbellc23a0c92020-01-30 22:14:41 -08003026 if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003027 continue;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003028 if (!notified) {
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003029 notified = true;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003030
Alistair Popple6b49bf62021-06-30 18:54:19 -07003031 mmu_notifier_range_init_owner(&range,
3032 MMU_NOTIFY_MIGRATE, 0, migrate->vma,
3033 migrate->vma->vm_mm, addr, migrate->end,
Ralph Campbell5e5dda82020-12-14 19:12:55 -08003034 migrate->pgmap_owner);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003035 mmu_notifier_invalidate_range_start(&range);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003036 }
3037 migrate_vma_insert_page(migrate, addr, newpage,
Stephen Zhangd85c6db2020-12-14 19:13:20 -08003038 &migrate->src[i]);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003039 continue;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003040 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003041
3042 mapping = page_mapping(page);
3043
Jérôme Glissea5430dd2017-09-08 16:12:17 -07003044 if (is_zone_device_page(newpage)) {
3045 if (is_device_private_page(newpage)) {
3046 /*
3047 * For now only support private anonymous when
3048 * migrating to un-addressable device memory.
3049 */
3050 if (mapping) {
3051 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
3052 continue;
3053 }
Christoph Hellwig25b29952019-06-13 22:50:49 +02003054 } else {
Jérôme Glissea5430dd2017-09-08 16:12:17 -07003055 /*
3056 * Other types of ZONE_DEVICE page are not
3057 * supported.
3058 */
3059 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
3060 continue;
3061 }
3062 }
3063
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003064 r = migrate_page(mapping, newpage, page, MIGRATE_SYNC_NO_COPY);
3065 if (r != MIGRATEPAGE_SUCCESS)
3066 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
3067 }
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003068
Jérôme Glisse4645b9f2017-11-15 17:34:11 -08003069 /*
3070 * No need to double call mmu_notifier->invalidate_range() callback as
3071 * the above ptep_clear_flush_notify() inside migrate_vma_insert_page()
3072 * did already call it.
3073 */
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003074 if (notified)
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003075 mmu_notifier_invalidate_range_only_end(&range);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003076}
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003077EXPORT_SYMBOL(migrate_vma_pages);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003078
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003079/**
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003080 * migrate_vma_finalize() - restore CPU page table entry
3081 * @migrate: migrate struct containing all migration information
3082 *
3083 * This replaces the special migration pte entry with either a mapping to the
3084 * new page if migration was successful for that page, or to the original page
3085 * otherwise.
3086 *
3087 * This also unlocks the pages and puts them back on the lru, or drops the extra
3088 * refcount, for device pages.
3089 */
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003090void migrate_vma_finalize(struct migrate_vma *migrate)
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003091{
3092 const unsigned long npages = migrate->npages;
3093 unsigned long i;
3094
3095 for (i = 0; i < npages; i++) {
3096 struct page *newpage = migrate_pfn_to_page(migrate->dst[i]);
3097 struct page *page = migrate_pfn_to_page(migrate->src[i]);
3098
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003099 if (!page) {
3100 if (newpage) {
3101 unlock_page(newpage);
3102 put_page(newpage);
3103 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003104 continue;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003105 }
3106
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003107 if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE) || !newpage) {
3108 if (newpage) {
3109 unlock_page(newpage);
3110 put_page(newpage);
3111 }
3112 newpage = page;
3113 }
3114
3115 remove_migration_ptes(page, newpage, false);
3116 unlock_page(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003117
Jérôme Glissea5430dd2017-09-08 16:12:17 -07003118 if (is_zone_device_page(page))
3119 put_page(page);
3120 else
3121 putback_lru_page(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003122
3123 if (newpage != page) {
3124 unlock_page(newpage);
Jérôme Glissea5430dd2017-09-08 16:12:17 -07003125 if (is_zone_device_page(newpage))
3126 put_page(newpage);
3127 else
3128 putback_lru_page(newpage);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003129 }
3130 }
3131}
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003132EXPORT_SYMBOL(migrate_vma_finalize);
Christoph Hellwig9b2ed9c2019-08-14 09:59:28 +02003133#endif /* CONFIG_DEVICE_PRIVATE */
Dave Hansen79c28a42021-09-02 14:59:06 -07003134
Dave Hansen76af6a02021-10-18 15:15:32 -07003135#if defined(CONFIG_HOTPLUG_CPU)
Dave Hansen79c28a42021-09-02 14:59:06 -07003136/* Disable reclaim-based migration. */
3137static void __disable_all_migrate_targets(void)
3138{
3139 int node;
3140
3141 for_each_online_node(node)
3142 node_demotion[node] = NUMA_NO_NODE;
3143}
3144
3145static void disable_all_migrate_targets(void)
3146{
3147 __disable_all_migrate_targets();
3148
3149 /*
3150 * Ensure that the "disable" is visible across the system.
3151 * Readers will see either a combination of before+disable
3152 * state or disable+after. They will never see before and
3153 * after state together.
3154 *
3155 * The before+after state together might have cycles and
3156 * could cause readers to do things like loop until this
3157 * function finishes. This ensures they can only see a
3158 * single "bad" read and would, for instance, only loop
3159 * once.
3160 */
3161 synchronize_rcu();
3162}
3163
3164/*
3165 * Find an automatic demotion target for 'node'.
3166 * Failing here is OK. It might just indicate
3167 * being at the end of a chain.
3168 */
3169static int establish_migrate_target(int node, nodemask_t *used)
3170{
3171 int migration_target;
3172
3173 /*
3174 * Can not set a migration target on a
3175 * node with it already set.
3176 *
3177 * No need for READ_ONCE() here since this
3178 * in the write path for node_demotion[].
3179 * This should be the only thread writing.
3180 */
3181 if (node_demotion[node] != NUMA_NO_NODE)
3182 return NUMA_NO_NODE;
3183
3184 migration_target = find_next_best_node(node, used);
3185 if (migration_target == NUMA_NO_NODE)
3186 return NUMA_NO_NODE;
3187
3188 node_demotion[node] = migration_target;
3189
3190 return migration_target;
3191}
3192
3193/*
3194 * When memory fills up on a node, memory contents can be
3195 * automatically migrated to another node instead of
3196 * discarded at reclaim.
3197 *
3198 * Establish a "migration path" which will start at nodes
3199 * with CPUs and will follow the priorities used to build the
3200 * page allocator zonelists.
3201 *
3202 * The difference here is that cycles must be avoided. If
3203 * node0 migrates to node1, then neither node1, nor anything
3204 * node1 migrates to can migrate to node0.
3205 *
3206 * This function can run simultaneously with readers of
3207 * node_demotion[]. However, it can not run simultaneously
3208 * with itself. Exclusion is provided by memory hotplug events
3209 * being single-threaded.
3210 */
3211static void __set_migration_target_nodes(void)
3212{
3213 nodemask_t next_pass = NODE_MASK_NONE;
3214 nodemask_t this_pass = NODE_MASK_NONE;
3215 nodemask_t used_targets = NODE_MASK_NONE;
3216 int node;
3217
3218 /*
3219 * Avoid any oddities like cycles that could occur
3220 * from changes in the topology. This will leave
3221 * a momentary gap when migration is disabled.
3222 */
3223 disable_all_migrate_targets();
3224
3225 /*
3226 * Allocations go close to CPUs, first. Assume that
3227 * the migration path starts at the nodes with CPUs.
3228 */
3229 next_pass = node_states[N_CPU];
3230again:
3231 this_pass = next_pass;
3232 next_pass = NODE_MASK_NONE;
3233 /*
3234 * To avoid cycles in the migration "graph", ensure
3235 * that migration sources are not future targets by
3236 * setting them in 'used_targets'. Do this only
3237 * once per pass so that multiple source nodes can
3238 * share a target node.
3239 *
3240 * 'used_targets' will become unavailable in future
3241 * passes. This limits some opportunities for
3242 * multiple source nodes to share a destination.
3243 */
3244 nodes_or(used_targets, used_targets, this_pass);
3245 for_each_node_mask(node, this_pass) {
3246 int target_node = establish_migrate_target(node, &used_targets);
3247
3248 if (target_node == NUMA_NO_NODE)
3249 continue;
3250
3251 /*
3252 * Visit targets from this pass in the next pass.
3253 * Eventually, every node will have been part of
3254 * a pass, and will become set in 'used_targets'.
3255 */
3256 node_set(target_node, next_pass);
3257 }
3258 /*
3259 * 'next_pass' contains nodes which became migration
3260 * targets in this pass. Make additional passes until
3261 * no more migrations targets are available.
3262 */
3263 if (!nodes_empty(next_pass))
3264 goto again;
3265}
3266
3267/*
3268 * For callers that do not hold get_online_mems() already.
3269 */
Dave Hansen79c28a42021-09-02 14:59:06 -07003270static void set_migration_target_nodes(void)
3271{
3272 get_online_mems();
3273 __set_migration_target_nodes();
3274 put_online_mems();
3275}
Dave Hansen884a6e52021-09-02 14:59:09 -07003276
3277/*
Dave Hansen884a6e52021-09-02 14:59:09 -07003278 * This leaves migrate-on-reclaim transiently disabled between
3279 * the MEM_GOING_OFFLINE and MEM_OFFLINE events. This runs
3280 * whether reclaim-based migration is enabled or not, which
3281 * ensures that the user can turn reclaim-based migration at
3282 * any time without needing to recalculate migration targets.
3283 *
3284 * These callbacks already hold get_online_mems(). That is why
3285 * __set_migration_target_nodes() can be used as opposed to
3286 * set_migration_target_nodes().
3287 */
3288static int __meminit migrate_on_reclaim_callback(struct notifier_block *self,
Dave Hansen295be912021-10-18 15:15:29 -07003289 unsigned long action, void *_arg)
Dave Hansen884a6e52021-09-02 14:59:09 -07003290{
Dave Hansen295be912021-10-18 15:15:29 -07003291 struct memory_notify *arg = _arg;
3292
3293 /*
3294 * Only update the node migration order when a node is
3295 * changing status, like online->offline. This avoids
3296 * the overhead of synchronize_rcu() in most cases.
3297 */
3298 if (arg->status_change_nid < 0)
3299 return notifier_from_errno(0);
3300
Dave Hansen884a6e52021-09-02 14:59:09 -07003301 switch (action) {
3302 case MEM_GOING_OFFLINE:
3303 /*
3304 * Make sure there are not transient states where
3305 * an offline node is a migration target. This
3306 * will leave migration disabled until the offline
3307 * completes and the MEM_OFFLINE case below runs.
3308 */
3309 disable_all_migrate_targets();
3310 break;
3311 case MEM_OFFLINE:
3312 case MEM_ONLINE:
3313 /*
3314 * Recalculate the target nodes once the node
3315 * reaches its final state (online or offline).
3316 */
3317 __set_migration_target_nodes();
3318 break;
3319 case MEM_CANCEL_OFFLINE:
3320 /*
3321 * MEM_GOING_OFFLINE disabled all the migration
3322 * targets. Reenable them.
3323 */
3324 __set_migration_target_nodes();
3325 break;
3326 case MEM_GOING_ONLINE:
3327 case MEM_CANCEL_ONLINE:
3328 break;
3329 }
3330
3331 return notifier_from_errno(0);
3332}
3333
Dave Hansen76af6a02021-10-18 15:15:32 -07003334/*
3335 * React to hotplug events that might affect the migration targets
3336 * like events that online or offline NUMA nodes.
3337 *
3338 * The ordering is also currently dependent on which nodes have
3339 * CPUs. That means we need CPU on/offline notification too.
3340 */
3341static int migration_online_cpu(unsigned int cpu)
3342{
3343 set_migration_target_nodes();
3344 return 0;
3345}
3346
3347static int migration_offline_cpu(unsigned int cpu)
3348{
3349 set_migration_target_nodes();
3350 return 0;
3351}
3352
Dave Hansen884a6e52021-09-02 14:59:09 -07003353static int __init migrate_on_reclaim_init(void)
3354{
3355 int ret;
3356
Huang Yinga6a02512021-10-18 15:15:35 -07003357 ret = cpuhp_setup_state_nocalls(CPUHP_MM_DEMOTION_DEAD, "mm/demotion:offline",
3358 NULL, migration_offline_cpu);
Dave Hansen884a6e52021-09-02 14:59:09 -07003359 /*
3360 * In the unlikely case that this fails, the automatic
3361 * migration targets may become suboptimal for nodes
3362 * where N_CPU changes. With such a small impact in a
3363 * rare case, do not bother trying to do anything special.
3364 */
3365 WARN_ON(ret < 0);
Huang Yinga6a02512021-10-18 15:15:35 -07003366 ret = cpuhp_setup_state(CPUHP_AP_MM_DEMOTION_ONLINE, "mm/demotion:online",
3367 migration_online_cpu, NULL);
3368 WARN_ON(ret < 0);
Dave Hansen884a6e52021-09-02 14:59:09 -07003369
3370 hotplug_memory_notifier(migrate_on_reclaim_callback, 100);
3371 return 0;
3372}
3373late_initcall(migrate_on_reclaim_init);
Dave Hansen76af6a02021-10-18 15:15:32 -07003374#endif /* CONFIG_HOTPLUG_CPU */