blob: c5974fff33957551e8030f18731a02cac327ea0c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Christoph Lameterb20a3502006-03-22 00:09:12 -08002/*
Hugh Dickins14e0f9b2015-11-05 18:49:43 -08003 * Memory Migration functionality - linux/mm/migrate.c
Christoph Lameterb20a3502006-03-22 00:09:12 -08004 *
5 * Copyright (C) 2006 Silicon Graphics, Inc., Christoph Lameter
6 *
7 * Page migration was first developed in the context of the memory hotplug
8 * project. The main authors of the migration code are:
9 *
10 * IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
11 * Hirokazu Takahashi <taka@valinux.co.jp>
12 * Dave Hansen <haveblue@us.ibm.com>
Christoph Lametercde53532008-07-04 09:59:22 -070013 * Christoph Lameter
Christoph Lameterb20a3502006-03-22 00:09:12 -080014 */
15
16#include <linux/migrate.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040017#include <linux/export.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080018#include <linux/swap.h>
Christoph Lameter06972122006-06-23 02:03:35 -070019#include <linux/swapops.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080020#include <linux/pagemap.h>
Christoph Lametere23ca002006-04-10 22:52:57 -070021#include <linux/buffer_head.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080022#include <linux/mm_inline.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070023#include <linux/nsproxy.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080024#include <linux/pagevec.h>
Hugh Dickinse9995ef2009-12-14 17:59:31 -080025#include <linux/ksm.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080026#include <linux/rmap.h>
27#include <linux/topology.h>
28#include <linux/cpu.h>
29#include <linux/cpuset.h>
Christoph Lameter04e62a22006-06-23 02:03:38 -070030#include <linux/writeback.h>
Christoph Lameter742755a2006-06-23 02:03:55 -070031#include <linux/mempolicy.h>
32#include <linux/vmalloc.h>
David Quigley86c3a762006-06-23 02:04:02 -070033#include <linux/security.h>
Hugh Dickins42cb14b2015-11-05 18:50:05 -080034#include <linux/backing-dev.h>
Minchan Kimbda807d2016-07-26 15:23:05 -070035#include <linux/compaction.h>
Adrian Bunk4f5ca262008-07-23 21:27:02 -070036#include <linux/syscalls.h>
Dominik Brodowski7addf442018-03-17 16:08:03 +010037#include <linux/compat.h>
Naoya Horiguchi290408d2010-09-08 10:19:35 +090038#include <linux/hugetlb.h>
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -070039#include <linux/hugetlb_cgroup.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/gfp.h>
Christoph Hellwiga5201102019-08-28 16:19:53 +020041#include <linux/pagewalk.h>
Jérôme Glissedf6ad692017-09-08 16:12:24 -070042#include <linux/pfn_t.h>
Jérôme Glissea5430dd2017-09-08 16:12:17 -070043#include <linux/memremap.h>
Jérôme Glisse8315ada2017-09-08 16:12:21 -070044#include <linux/userfaultfd_k.h>
Rafael Aquinibf6bddf12012-12-11 16:02:42 -080045#include <linux/balloon_compaction.h>
Mel Gormanf714f4f2013-12-18 17:08:33 -080046#include <linux/mmu_notifier.h>
Vladimir Davydov33c3fc72015-09-09 15:35:45 -070047#include <linux/page_idle.h>
Vlastimil Babkad435edc2016-03-15 14:56:15 -070048#include <linux/page_owner.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010049#include <linux/sched/mm.h>
Linus Torvalds197e7e52017-08-20 13:26:27 -070050#include <linux/ptrace.h>
Ralph Campbell34290e22020-01-30 22:14:44 -080051#include <linux/oom.h>
Dave Hansen884a6e52021-09-02 14:59:09 -070052#include <linux/memory.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080053
Michal Nazarewicz0d1836c2010-12-21 17:24:26 -080054#include <asm/tlbflush.h>
55
Mel Gorman7b2a2d42012-10-19 14:07:31 +010056#define CREATE_TRACE_POINTS
57#include <trace/events/migrate.h>
58
Christoph Lameterb20a3502006-03-22 00:09:12 -080059#include "internal.h"
60
Yisheng Xie9e5bcd62017-02-24 14:57:29 -080061int isolate_movable_page(struct page *page, isolate_mode_t mode)
Minchan Kimbda807d2016-07-26 15:23:05 -070062{
63 struct address_space *mapping;
64
65 /*
66 * Avoid burning cycles with pages that are yet under __free_pages(),
67 * or just got freed under us.
68 *
69 * In case we 'win' a race for a movable page being freed under us and
70 * raise its refcount preventing __free_pages() from doing its job
71 * the put_page() at the end of this block will take care of
72 * release this page, thus avoiding a nasty leakage.
73 */
74 if (unlikely(!get_page_unless_zero(page)))
75 goto out;
76
77 /*
78 * Check PageMovable before holding a PG_lock because page's owner
79 * assumes anybody doesn't touch PG_lock of newly allocated page
Wei Yang8bb4e7a2019-03-05 15:46:22 -080080 * so unconditionally grabbing the lock ruins page's owner side.
Minchan Kimbda807d2016-07-26 15:23:05 -070081 */
82 if (unlikely(!__PageMovable(page)))
83 goto out_putpage;
84 /*
85 * As movable pages are not isolated from LRU lists, concurrent
86 * compaction threads can race against page migration functions
87 * as well as race against the releasing a page.
88 *
89 * In order to avoid having an already isolated movable page
90 * being (wrongly) re-isolated while it is under migration,
91 * or to avoid attempting to isolate pages being released,
92 * lets be sure we have the page lock
93 * before proceeding with the movable page isolation steps.
94 */
95 if (unlikely(!trylock_page(page)))
96 goto out_putpage;
97
98 if (!PageMovable(page) || PageIsolated(page))
99 goto out_no_isolated;
100
101 mapping = page_mapping(page);
102 VM_BUG_ON_PAGE(!mapping, page);
103
104 if (!mapping->a_ops->isolate_page(page, mode))
105 goto out_no_isolated;
106
107 /* Driver shouldn't use PG_isolated bit of page->flags */
108 WARN_ON_ONCE(PageIsolated(page));
109 __SetPageIsolated(page);
110 unlock_page(page);
111
Yisheng Xie9e5bcd62017-02-24 14:57:29 -0800112 return 0;
Minchan Kimbda807d2016-07-26 15:23:05 -0700113
114out_no_isolated:
115 unlock_page(page);
116out_putpage:
117 put_page(page);
118out:
Yisheng Xie9e5bcd62017-02-24 14:57:29 -0800119 return -EBUSY;
Minchan Kimbda807d2016-07-26 15:23:05 -0700120}
121
Miaohe Lin606a6f72021-05-04 18:37:04 -0700122static void putback_movable_page(struct page *page)
Minchan Kimbda807d2016-07-26 15:23:05 -0700123{
124 struct address_space *mapping;
125
Minchan Kimbda807d2016-07-26 15:23:05 -0700126 mapping = page_mapping(page);
127 mapping->a_ops->putback_page(page);
128 __ClearPageIsolated(page);
129}
130
Christoph Lameterb20a3502006-03-22 00:09:12 -0800131/*
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800132 * Put previously isolated pages back onto the appropriate lists
133 * from where they were once taken off for compaction/migration.
134 *
Joonsoo Kim59c82b72014-01-21 15:51:17 -0800135 * This function shall be used whenever the isolated pageset has been
136 * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range()
137 * and isolate_huge_page().
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800138 */
139void putback_movable_pages(struct list_head *l)
140{
141 struct page *page;
142 struct page *page2;
143
144 list_for_each_entry_safe(page, page2, l, lru) {
Naoya Horiguchi31caf662013-09-11 14:21:59 -0700145 if (unlikely(PageHuge(page))) {
146 putback_active_hugepage(page);
147 continue;
148 }
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800149 list_del(&page->lru);
Minchan Kimbda807d2016-07-26 15:23:05 -0700150 /*
151 * We isolated non-lru movable page so here we can use
152 * __PageMovable because LRU page's mapping cannot have
153 * PAGE_MAPPING_MOVABLE.
154 */
Minchan Kimb1123ea62016-07-26 15:23:09 -0700155 if (unlikely(__PageMovable(page))) {
Minchan Kimbda807d2016-07-26 15:23:05 -0700156 VM_BUG_ON_PAGE(!PageIsolated(page), page);
157 lock_page(page);
158 if (PageMovable(page))
159 putback_movable_page(page);
160 else
161 __ClearPageIsolated(page);
162 unlock_page(page);
163 put_page(page);
164 } else {
Naoya Horiguchie8db67e2017-09-08 16:11:12 -0700165 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -0700166 page_is_file_lru(page), -thp_nr_pages(page));
Rabin Vincentfc280fe2017-04-20 14:37:46 -0700167 putback_lru_page(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700168 }
Christoph Lameterb20a3502006-03-22 00:09:12 -0800169 }
Christoph Lameterb20a3502006-03-22 00:09:12 -0800170}
Charan Teja Reddyf47b8522021-02-16 13:59:45 +0530171EXPORT_SYMBOL_GPL(putback_movable_pages);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800172
Christoph Lameter06972122006-06-23 02:03:35 -0700173/*
174 * Restore a potential migration pte to a working pte entry
175 */
Minchan Kime4b82222017-05-03 14:54:27 -0700176static bool remove_migration_pte(struct page *page, struct vm_area_struct *vma,
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800177 unsigned long addr, void *old)
Christoph Lameter06972122006-06-23 02:03:35 -0700178{
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800179 struct page_vma_mapped_walk pvmw = {
180 .page = old,
181 .vma = vma,
182 .address = addr,
183 .flags = PVMW_SYNC | PVMW_MIGRATION,
184 };
185 struct page *new;
186 pte_t pte;
Christoph Lameter06972122006-06-23 02:03:35 -0700187 swp_entry_t entry;
Christoph Lameter06972122006-06-23 02:03:35 -0700188
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800189 VM_BUG_ON_PAGE(PageTail(page), page);
190 while (page_vma_mapped_walk(&pvmw)) {
Naoya Horiguchi4b0ece62017-03-31 15:11:44 -0700191 if (PageKsm(page))
192 new = page;
193 else
194 new = page - pvmw.page->index +
195 linear_page_index(vma, pvmw.address);
Christoph Lameter06972122006-06-23 02:03:35 -0700196
Zi Yan616b8372017-09-08 16:10:57 -0700197#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
198 /* PMD-mapped THP migration entry */
199 if (!pvmw.pte) {
200 VM_BUG_ON_PAGE(PageHuge(page) || !PageTransCompound(page), page);
201 remove_migration_pmd(&pvmw, new);
202 continue;
203 }
204#endif
205
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800206 get_page(new);
207 pte = pte_mkold(mk_pte(new, READ_ONCE(vma->vm_page_prot)));
208 if (pte_swp_soft_dirty(*pvmw.pte))
209 pte = pte_mksoft_dirty(pte);
Christoph Lameter06972122006-06-23 02:03:35 -0700210
Hugh Dickins486cf462011-10-19 12:50:35 -0700211 /*
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800212 * Recheck VMA as permissions can change since migration started
Hugh Dickins486cf462011-10-19 12:50:35 -0700213 */
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800214 entry = pte_to_swp_entry(*pvmw.pte);
Alistair Popple4dd845b2021-06-30 18:54:09 -0700215 if (is_writable_migration_entry(entry))
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800216 pte = maybe_mkwrite(pte, vma);
Peter Xuf45ec5f2020-04-06 20:06:01 -0700217 else if (pte_swp_uffd_wp(*pvmw.pte))
218 pte = pte_mkuffd_wp(pte);
Mel Gormand3cb8bf2014-10-02 19:47:41 +0100219
Ralph Campbell61287632020-09-04 16:36:04 -0700220 if (unlikely(is_device_private_page(new))) {
Alistair Popple4dd845b2021-06-30 18:54:09 -0700221 if (pte_write(pte))
222 entry = make_writable_device_private_entry(
223 page_to_pfn(new));
224 else
225 entry = make_readable_device_private_entry(
226 page_to_pfn(new));
Ralph Campbell61287632020-09-04 16:36:04 -0700227 pte = swp_entry_to_pte(entry);
Ralph Campbell3d321bf82020-09-04 16:36:07 -0700228 if (pte_swp_soft_dirty(*pvmw.pte))
229 pte = pte_swp_mksoft_dirty(pte);
Ralph Campbell61287632020-09-04 16:36:04 -0700230 if (pte_swp_uffd_wp(*pvmw.pte))
231 pte = pte_swp_mkuffd_wp(pte);
Lars Perssond2b2c6dd2019-03-28 20:44:28 -0700232 }
Jérôme Glissea5430dd2017-09-08 16:12:17 -0700233
Andi Kleen3ef8fd72010-10-11 16:03:21 +0200234#ifdef CONFIG_HUGETLB_PAGE
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800235 if (PageHuge(new)) {
Christophe Leroy79c1c592021-06-30 18:48:00 -0700236 unsigned int shift = huge_page_shift(hstate_vma(vma));
237
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800238 pte = pte_mkhuge(pte);
Christophe Leroy79c1c592021-06-30 18:48:00 -0700239 pte = arch_make_huge_pte(pte, shift, vma->vm_flags);
Aneesh Kumar K.V383321a2017-07-06 15:38:41 -0700240 set_huge_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800241 if (PageAnon(new))
242 hugepage_add_anon_rmap(new, vma, pvmw.address);
243 else
244 page_dup_rmap(new, true);
Aneesh Kumar K.V383321a2017-07-06 15:38:41 -0700245 } else
246#endif
247 {
248 set_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700249
Aneesh Kumar K.V383321a2017-07-06 15:38:41 -0700250 if (PageAnon(new))
251 page_add_anon_rmap(new, vma, pvmw.address, false);
252 else
253 page_add_file_rmap(new, false);
254 }
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800255 if (vma->vm_flags & VM_LOCKED && !PageTransCompound(new))
256 mlock_vma_page(new);
Hugh Dickins51afb122015-11-05 18:49:37 -0800257
Kirill A. Shutemove125fe42018-10-05 15:51:41 -0700258 if (PageTransHuge(page) && PageMlocked(page))
259 clear_page_mlock(page);
260
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800261 /* No need to invalidate - it was non-present before */
262 update_mmu_cache(vma, pvmw.address, pvmw.pte);
263 }
264
Minchan Kime4b82222017-05-03 14:54:27 -0700265 return true;
Christoph Lameter06972122006-06-23 02:03:35 -0700266}
267
268/*
Christoph Lameter04e62a22006-06-23 02:03:38 -0700269 * Get rid of all migration entries and replace them by
270 * references to the indicated page.
271 */
Kirill A. Shutemove3884662016-03-17 14:20:07 -0700272void remove_migration_ptes(struct page *old, struct page *new, bool locked)
Christoph Lameter04e62a22006-06-23 02:03:38 -0700273{
Joonsoo Kim051ac832014-01-21 15:49:48 -0800274 struct rmap_walk_control rwc = {
275 .rmap_one = remove_migration_pte,
276 .arg = old,
277 };
278
Kirill A. Shutemove3884662016-03-17 14:20:07 -0700279 if (locked)
280 rmap_walk_locked(new, &rwc);
281 else
282 rmap_walk(new, &rwc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700283}
284
285/*
Christoph Lameter06972122006-06-23 02:03:35 -0700286 * Something used the pte of a page under migration. We need to
287 * get to the page and wait until migration is finished.
288 * When we return from this function the fault will be retried.
Christoph Lameter06972122006-06-23 02:03:35 -0700289 */
Naoya Horiguchie66f17f2015-02-11 15:25:22 -0800290void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700291 spinlock_t *ptl)
Christoph Lameter06972122006-06-23 02:03:35 -0700292{
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700293 pte_t pte;
Christoph Lameter06972122006-06-23 02:03:35 -0700294 swp_entry_t entry;
295 struct page *page;
296
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700297 spin_lock(ptl);
Christoph Lameter06972122006-06-23 02:03:35 -0700298 pte = *ptep;
299 if (!is_swap_pte(pte))
300 goto out;
301
302 entry = pte_to_swp_entry(pte);
303 if (!is_migration_entry(entry))
304 goto out;
305
Alistair Poppleaf5cdaf2021-06-30 18:54:06 -0700306 page = pfn_swap_entry_to_page(entry);
Xu Yuffc90cb2021-06-15 18:23:42 -0700307 page = compound_head(page);
Christoph Lameter06972122006-06-23 02:03:35 -0700308
Nick Piggine2867812008-07-25 19:45:30 -0700309 /*
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500310 * Once page cache replacement of page migration started, page_count
Hugh Dickins9a1ea432018-12-28 00:36:14 -0800311 * is zero; but we must not call put_and_wait_on_page_locked() without
312 * a ref. Use get_page_unless_zero(), and just fault again if it fails.
Nick Piggine2867812008-07-25 19:45:30 -0700313 */
314 if (!get_page_unless_zero(page))
315 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700316 pte_unmap_unlock(ptep, ptl);
Matthew Wilcox (Oracle)48054622021-02-24 12:02:02 -0800317 put_and_wait_on_page_locked(page, TASK_UNINTERRUPTIBLE);
Christoph Lameter06972122006-06-23 02:03:35 -0700318 return;
319out:
320 pte_unmap_unlock(ptep, ptl);
321}
322
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700323void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
324 unsigned long address)
325{
326 spinlock_t *ptl = pte_lockptr(mm, pmd);
327 pte_t *ptep = pte_offset_map(pmd, address);
328 __migration_entry_wait(mm, ptep, ptl);
329}
330
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -0800331void migration_entry_wait_huge(struct vm_area_struct *vma,
332 struct mm_struct *mm, pte_t *pte)
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700333{
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -0800334 spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte);
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700335 __migration_entry_wait(mm, pte, ptl);
336}
337
Zi Yan616b8372017-09-08 16:10:57 -0700338#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
339void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd)
340{
341 spinlock_t *ptl;
342 struct page *page;
343
344 ptl = pmd_lock(mm, pmd);
345 if (!is_pmd_migration_entry(*pmd))
346 goto unlock;
Alistair Poppleaf5cdaf2021-06-30 18:54:06 -0700347 page = pfn_swap_entry_to_page(pmd_to_swp_entry(*pmd));
Zi Yan616b8372017-09-08 16:10:57 -0700348 if (!get_page_unless_zero(page))
349 goto unlock;
350 spin_unlock(ptl);
Matthew Wilcox (Oracle)48054622021-02-24 12:02:02 -0800351 put_and_wait_on_page_locked(page, TASK_UNINTERRUPTIBLE);
Zi Yan616b8372017-09-08 16:10:57 -0700352 return;
353unlock:
354 spin_unlock(ptl);
355}
356#endif
357
Jan Karaf9004822019-03-05 15:48:46 -0800358static int expected_page_refs(struct address_space *mapping, struct page *page)
Jan Kara0b3901b2018-12-28 00:39:01 -0800359{
360 int expected_count = 1;
361
362 /*
Ralph Campbellf1f4f3a2020-10-13 16:58:42 -0700363 * Device private pages have an extra refcount as they are
Jan Kara0b3901b2018-12-28 00:39:01 -0800364 * ZONE_DEVICE pages.
365 */
366 expected_count += is_device_private_page(page);
Jan Karaf9004822019-03-05 15:48:46 -0800367 if (mapping)
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -0700368 expected_count += thp_nr_pages(page) + page_has_private(page);
Jan Kara0b3901b2018-12-28 00:39:01 -0800369
370 return expected_count;
371}
372
Christoph Lameterb20a3502006-03-22 00:09:12 -0800373/*
Christoph Lameterc3fcf8a2006-06-23 02:03:32 -0700374 * Replace the page in the mapping.
Christoph Lameter5b5c7122006-06-23 02:03:29 -0700375 *
376 * The number of remaining references must be:
377 * 1 for anonymous pages without a mapping
378 * 2 for pages with a mapping
David Howells266cf652009-04-03 16:42:36 +0100379 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800380 */
Gu Zheng36bc08c2013-07-16 17:56:16 +0800381int migrate_page_move_mapping(struct address_space *mapping,
Keith Busch37109692019-07-18 15:58:46 -0700382 struct page *newpage, struct page *page, int extra_count)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800383{
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500384 XA_STATE(xas, &mapping->i_pages, page_index(page));
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800385 struct zone *oldzone, *newzone;
386 int dirty;
Jan Karaf9004822019-03-05 15:48:46 -0800387 int expected_count = expected_page_refs(mapping, page) + extra_count;
Shakeel Butt5c447d22021-01-23 21:01:15 -0800388 int nr = thp_nr_pages(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -0700389
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700390 if (!mapping) {
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700391 /* Anonymous page without mapping */
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500392 if (page_count(page) != expected_count)
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700393 return -EAGAIN;
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800394
395 /* No turning back from here */
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800396 newpage->index = page->index;
397 newpage->mapping = page->mapping;
398 if (PageSwapBacked(page))
Hugh Dickinsfa9949d2016-05-19 17:12:41 -0700399 __SetPageSwapBacked(newpage);
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800400
Rafael Aquini78bd5202012-12-11 16:02:31 -0800401 return MIGRATEPAGE_SUCCESS;
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700402 }
403
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800404 oldzone = page_zone(page);
405 newzone = page_zone(newpage);
406
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500407 xas_lock_irq(&xas);
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500408 if (page_count(page) != expected_count || xas_load(&xas) != page) {
409 xas_unlock_irq(&xas);
Christoph Lametere23ca002006-04-10 22:52:57 -0700410 return -EAGAIN;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800411 }
412
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700413 if (!page_ref_freeze(page, expected_count)) {
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500414 xas_unlock_irq(&xas);
Nick Piggine2867812008-07-25 19:45:30 -0700415 return -EAGAIN;
416 }
417
Christoph Lameterb20a3502006-03-22 00:09:12 -0800418 /*
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800419 * Now we know that no one else is looking at the page:
420 * no turning back from here.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800421 */
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800422 newpage->index = page->index;
423 newpage->mapping = page->mapping;
Shakeel Butt5c447d22021-01-23 21:01:15 -0800424 page_ref_add(newpage, nr); /* add cache reference */
Nicholas Piggin6326fec2016-12-25 13:00:29 +1000425 if (PageSwapBacked(page)) {
426 __SetPageSwapBacked(newpage);
427 if (PageSwapCache(page)) {
428 SetPageSwapCache(newpage);
429 set_page_private(newpage, page_private(page));
430 }
431 } else {
432 VM_BUG_ON_PAGE(PageSwapCache(page), page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800433 }
434
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800435 /* Move dirty while page refs frozen and newpage not yet exposed */
436 dirty = PageDirty(page);
437 if (dirty) {
438 ClearPageDirty(page);
439 SetPageDirty(newpage);
440 }
441
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500442 xas_store(&xas, newpage);
Naoya Horiguchie71769a2018-04-20 14:55:45 -0700443 if (PageTransHuge(page)) {
444 int i;
Naoya Horiguchie71769a2018-04-20 14:55:45 -0700445
Shakeel Butt5c447d22021-01-23 21:01:15 -0800446 for (i = 1; i < nr; i++) {
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500447 xas_next(&xas);
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700448 xas_store(&xas, newpage);
Naoya Horiguchie71769a2018-04-20 14:55:45 -0700449 }
Naoya Horiguchie71769a2018-04-20 14:55:45 -0700450 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800451
452 /*
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800453 * Drop cache reference from old page by unfreezing
454 * to one less reference.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800455 * We know this isn't the last reference.
456 */
Shakeel Butt5c447d22021-01-23 21:01:15 -0800457 page_ref_unfreeze(page, expected_count - nr);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800458
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500459 xas_unlock(&xas);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800460 /* Leave irq disabled to prevent preemption while updating stats */
461
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700462 /*
463 * If moved to a different zone then also account
464 * the page for that zone. Other VM counters will be
465 * taken care of when we establish references to the
466 * new page and drop references to the old page.
467 *
468 * Note that anonymous pages are accounted for
Mel Gorman4b9d0fa2016-07-28 15:46:17 -0700469 * via NR_FILE_PAGES and NR_ANON_MAPPED if they
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700470 * are mapped to swap space.
471 */
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800472 if (newzone != oldzone) {
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700473 struct lruvec *old_lruvec, *new_lruvec;
474 struct mem_cgroup *memcg;
475
476 memcg = page_memcg(page);
477 old_lruvec = mem_cgroup_lruvec(memcg, oldzone->zone_pgdat);
478 new_lruvec = mem_cgroup_lruvec(memcg, newzone->zone_pgdat);
479
Shakeel Butt5c447d22021-01-23 21:01:15 -0800480 __mod_lruvec_state(old_lruvec, NR_FILE_PAGES, -nr);
481 __mod_lruvec_state(new_lruvec, NR_FILE_PAGES, nr);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800482 if (PageSwapBacked(page) && !PageSwapCache(page)) {
Shakeel Butt5c447d22021-01-23 21:01:15 -0800483 __mod_lruvec_state(old_lruvec, NR_SHMEM, -nr);
484 __mod_lruvec_state(new_lruvec, NR_SHMEM, nr);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800485 }
Shakeel Buttb6038942021-02-24 12:03:55 -0800486#ifdef CONFIG_SWAP
487 if (PageSwapCache(page)) {
488 __mod_lruvec_state(old_lruvec, NR_SWAPCACHE, -nr);
489 __mod_lruvec_state(new_lruvec, NR_SWAPCACHE, nr);
490 }
491#endif
Christoph Hellwigf56753a2020-09-24 08:51:40 +0200492 if (dirty && mapping_can_writeback(mapping)) {
Shakeel Butt5c447d22021-01-23 21:01:15 -0800493 __mod_lruvec_state(old_lruvec, NR_FILE_DIRTY, -nr);
494 __mod_zone_page_state(oldzone, NR_ZONE_WRITE_PENDING, -nr);
495 __mod_lruvec_state(new_lruvec, NR_FILE_DIRTY, nr);
496 __mod_zone_page_state(newzone, NR_ZONE_WRITE_PENDING, nr);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800497 }
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700498 }
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800499 local_irq_enable();
Christoph Lameterb20a3502006-03-22 00:09:12 -0800500
Rafael Aquini78bd5202012-12-11 16:02:31 -0800501 return MIGRATEPAGE_SUCCESS;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800502}
Richard Weinberger1118dce2016-06-16 23:26:14 +0200503EXPORT_SYMBOL(migrate_page_move_mapping);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800504
505/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900506 * The expected number of remaining references is the same as that
507 * of migrate_page_move_mapping().
508 */
509int migrate_huge_page_move_mapping(struct address_space *mapping,
510 struct page *newpage, struct page *page)
511{
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500512 XA_STATE(xas, &mapping->i_pages, page_index(page));
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900513 int expected_count;
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900514
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500515 xas_lock_irq(&xas);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900516 expected_count = 2 + page_has_private(page);
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500517 if (page_count(page) != expected_count || xas_load(&xas) != page) {
518 xas_unlock_irq(&xas);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900519 return -EAGAIN;
520 }
521
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700522 if (!page_ref_freeze(page, expected_count)) {
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500523 xas_unlock_irq(&xas);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900524 return -EAGAIN;
525 }
526
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800527 newpage->index = page->index;
528 newpage->mapping = page->mapping;
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700529
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900530 get_page(newpage);
531
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500532 xas_store(&xas, newpage);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900533
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700534 page_ref_unfreeze(page, expected_count - 1);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900535
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500536 xas_unlock_irq(&xas);
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700537
Rafael Aquini78bd5202012-12-11 16:02:31 -0800538 return MIGRATEPAGE_SUCCESS;
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900539}
540
541/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800542 * Copy the page to its new location
543 */
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700544void migrate_page_states(struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800545{
Rik van Riel7851a452013-10-07 11:29:23 +0100546 int cpupid;
547
Christoph Lameterb20a3502006-03-22 00:09:12 -0800548 if (PageError(page))
549 SetPageError(newpage);
550 if (PageReferenced(page))
551 SetPageReferenced(newpage);
552 if (PageUptodate(page))
553 SetPageUptodate(newpage);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700554 if (TestClearPageActive(page)) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800555 VM_BUG_ON_PAGE(PageUnevictable(page), page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800556 SetPageActive(newpage);
Lee Schermerhorn418b27e2009-12-14 17:59:54 -0800557 } else if (TestClearPageUnevictable(page))
558 SetPageUnevictable(newpage);
Johannes Weiner1899ad12018-10-26 15:06:04 -0700559 if (PageWorkingset(page))
560 SetPageWorkingset(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800561 if (PageChecked(page))
562 SetPageChecked(newpage);
563 if (PageMappedToDisk(page))
564 SetPageMappedToDisk(newpage);
565
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800566 /* Move dirty on pages not done by migrate_page_move_mapping() */
567 if (PageDirty(page))
568 SetPageDirty(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800569
Vladimir Davydov33c3fc72015-09-09 15:35:45 -0700570 if (page_is_young(page))
571 set_page_young(newpage);
572 if (page_is_idle(page))
573 set_page_idle(newpage);
574
Rik van Riel7851a452013-10-07 11:29:23 +0100575 /*
576 * Copy NUMA information to the new page, to prevent over-eager
577 * future migrations of this same page.
578 */
579 cpupid = page_cpupid_xchg_last(page, -1);
580 page_cpupid_xchg_last(newpage, cpupid);
581
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800582 ksm_migrate_page(newpage, page);
Hugh Dickinsc8d65532013-02-22 16:35:10 -0800583 /*
584 * Please do not reorder this without considering how mm/ksm.c's
585 * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache().
586 */
Naoya Horiguchib3b3a992015-04-15 16:13:15 -0700587 if (PageSwapCache(page))
588 ClearPageSwapCache(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800589 ClearPagePrivate(page);
Muchun Songad2fa372021-06-30 18:47:21 -0700590
591 /* page->private contains hugetlb specific flags */
592 if (!PageHuge(page))
593 set_page_private(page, 0);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800594
595 /*
596 * If any waiters have accumulated on the new page then
597 * wake them up.
598 */
599 if (PageWriteback(newpage))
600 end_page_writeback(newpage);
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700601
Yang Shi6aeff242020-04-06 20:04:21 -0700602 /*
603 * PG_readahead shares the same bit with PG_reclaim. The above
604 * end_page_writeback() may clear PG_readahead mistakenly, so set the
605 * bit after that.
606 */
607 if (PageReadahead(page))
608 SetPageReadahead(newpage);
609
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700610 copy_page_owner(page, newpage);
Johannes Weiner74485cf2016-03-15 14:57:54 -0700611
Hugh Dickinsa333e3e2020-09-18 21:20:06 -0700612 if (!PageHuge(page))
613 mem_cgroup_migrate(page, newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800614}
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700615EXPORT_SYMBOL(migrate_page_states);
616
617void migrate_page_copy(struct page *newpage, struct page *page)
618{
619 if (PageHuge(page) || PageTransHuge(page))
620 copy_huge_page(newpage, page);
621 else
622 copy_highpage(newpage, page);
623
624 migrate_page_states(newpage, page);
625}
Richard Weinberger1118dce2016-06-16 23:26:14 +0200626EXPORT_SYMBOL(migrate_page_copy);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800627
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700628/************************************************************
629 * Migration functions
630 ***********************************************************/
631
Christoph Lameterb20a3502006-03-22 00:09:12 -0800632/*
Minchan Kimbda807d2016-07-26 15:23:05 -0700633 * Common logic to directly migrate a single LRU page suitable for
David Howells266cf652009-04-03 16:42:36 +0100634 * pages that do not use PagePrivate/PagePrivate2.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800635 *
636 * Pages are locked upon entry and exit.
637 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700638int migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800639 struct page *newpage, struct page *page,
640 enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800641{
642 int rc;
643
644 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
645
Keith Busch37109692019-07-18 15:58:46 -0700646 rc = migrate_page_move_mapping(mapping, newpage, page, 0);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800647
Rafael Aquini78bd5202012-12-11 16:02:31 -0800648 if (rc != MIGRATEPAGE_SUCCESS)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800649 return rc;
650
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700651 if (mode != MIGRATE_SYNC_NO_COPY)
652 migrate_page_copy(newpage, page);
653 else
654 migrate_page_states(newpage, page);
Rafael Aquini78bd5202012-12-11 16:02:31 -0800655 return MIGRATEPAGE_SUCCESS;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800656}
657EXPORT_SYMBOL(migrate_page);
658
David Howells93614012006-09-30 20:45:40 +0200659#ifdef CONFIG_BLOCK
Jan Kara84ade7c2018-12-28 00:39:09 -0800660/* Returns true if all buffers are successfully locked */
661static bool buffer_migrate_lock_buffers(struct buffer_head *head,
662 enum migrate_mode mode)
663{
664 struct buffer_head *bh = head;
665
666 /* Simple case, sync compaction */
667 if (mode != MIGRATE_ASYNC) {
668 do {
Jan Kara84ade7c2018-12-28 00:39:09 -0800669 lock_buffer(bh);
670 bh = bh->b_this_page;
671
672 } while (bh != head);
673
674 return true;
675 }
676
677 /* async case, we cannot block on lock_buffer so use trylock_buffer */
678 do {
Jan Kara84ade7c2018-12-28 00:39:09 -0800679 if (!trylock_buffer(bh)) {
680 /*
681 * We failed to lock the buffer and cannot stall in
682 * async migration. Release the taken locks
683 */
684 struct buffer_head *failed_bh = bh;
Jan Kara84ade7c2018-12-28 00:39:09 -0800685 bh = head;
686 while (bh != failed_bh) {
687 unlock_buffer(bh);
Jan Kara84ade7c2018-12-28 00:39:09 -0800688 bh = bh->b_this_page;
689 }
690 return false;
691 }
692
693 bh = bh->b_this_page;
694 } while (bh != head);
695 return true;
696}
697
Jan Kara89cb0882018-12-28 00:39:12 -0800698static int __buffer_migrate_page(struct address_space *mapping,
699 struct page *newpage, struct page *page, enum migrate_mode mode,
700 bool check_refs)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700701{
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700702 struct buffer_head *bh, *head;
703 int rc;
Jan Karacc4f11e2018-12-28 00:39:05 -0800704 int expected_count;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700705
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700706 if (!page_has_buffers(page))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800707 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700708
Jan Karacc4f11e2018-12-28 00:39:05 -0800709 /* Check whether page does not have extra refs before we do more work */
Jan Karaf9004822019-03-05 15:48:46 -0800710 expected_count = expected_page_refs(mapping, page);
Jan Karacc4f11e2018-12-28 00:39:05 -0800711 if (page_count(page) != expected_count)
712 return -EAGAIN;
713
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700714 head = page_buffers(page);
Jan Karacc4f11e2018-12-28 00:39:05 -0800715 if (!buffer_migrate_lock_buffers(head, mode))
716 return -EAGAIN;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700717
Jan Kara89cb0882018-12-28 00:39:12 -0800718 if (check_refs) {
719 bool busy;
720 bool invalidated = false;
721
722recheck_buffers:
723 busy = false;
724 spin_lock(&mapping->private_lock);
725 bh = head;
726 do {
727 if (atomic_read(&bh->b_count)) {
728 busy = true;
729 break;
730 }
731 bh = bh->b_this_page;
732 } while (bh != head);
Jan Kara89cb0882018-12-28 00:39:12 -0800733 if (busy) {
734 if (invalidated) {
735 rc = -EAGAIN;
736 goto unlock_buffers;
737 }
Jan Karaebdf4de2019-08-02 21:48:47 -0700738 spin_unlock(&mapping->private_lock);
Jan Kara89cb0882018-12-28 00:39:12 -0800739 invalidate_bh_lrus();
740 invalidated = true;
741 goto recheck_buffers;
742 }
743 }
744
Keith Busch37109692019-07-18 15:58:46 -0700745 rc = migrate_page_move_mapping(mapping, newpage, page, 0);
Rafael Aquini78bd5202012-12-11 16:02:31 -0800746 if (rc != MIGRATEPAGE_SUCCESS)
Jan Karacc4f11e2018-12-28 00:39:05 -0800747 goto unlock_buffers;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700748
Guoqing Jiangcd0f3712020-06-01 21:48:06 -0700749 attach_page_private(newpage, detach_page_private(page));
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700750
751 bh = head;
752 do {
753 set_bh_page(bh, newpage, bh_offset(bh));
754 bh = bh->b_this_page;
755
756 } while (bh != head);
757
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700758 if (mode != MIGRATE_SYNC_NO_COPY)
759 migrate_page_copy(newpage, page);
760 else
761 migrate_page_states(newpage, page);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700762
Jan Karacc4f11e2018-12-28 00:39:05 -0800763 rc = MIGRATEPAGE_SUCCESS;
764unlock_buffers:
Jan Karaebdf4de2019-08-02 21:48:47 -0700765 if (check_refs)
766 spin_unlock(&mapping->private_lock);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700767 bh = head;
768 do {
769 unlock_buffer(bh);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700770 bh = bh->b_this_page;
771
772 } while (bh != head);
773
Jan Karacc4f11e2018-12-28 00:39:05 -0800774 return rc;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700775}
Jan Kara89cb0882018-12-28 00:39:12 -0800776
777/*
778 * Migration function for pages with buffers. This function can only be used
779 * if the underlying filesystem guarantees that no other references to "page"
780 * exist. For example attached buffer heads are accessed only under page lock.
781 */
782int buffer_migrate_page(struct address_space *mapping,
783 struct page *newpage, struct page *page, enum migrate_mode mode)
784{
785 return __buffer_migrate_page(mapping, newpage, page, mode, false);
786}
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700787EXPORT_SYMBOL(buffer_migrate_page);
Jan Kara89cb0882018-12-28 00:39:12 -0800788
789/*
790 * Same as above except that this variant is more careful and checks that there
791 * are also no buffer head references. This function is the right one for
792 * mappings where buffer heads are directly looked up and referenced (such as
793 * block device mappings).
794 */
795int buffer_migrate_page_norefs(struct address_space *mapping,
796 struct page *newpage, struct page *page, enum migrate_mode mode)
797{
798 return __buffer_migrate_page(mapping, newpage, page, mode, true);
799}
David Howells93614012006-09-30 20:45:40 +0200800#endif
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700801
Christoph Lameter04e62a22006-06-23 02:03:38 -0700802/*
803 * Writeback a page to clean the dirty state
804 */
805static int writeout(struct address_space *mapping, struct page *page)
806{
807 struct writeback_control wbc = {
808 .sync_mode = WB_SYNC_NONE,
809 .nr_to_write = 1,
810 .range_start = 0,
811 .range_end = LLONG_MAX,
Christoph Lameter04e62a22006-06-23 02:03:38 -0700812 .for_reclaim = 1
813 };
814 int rc;
815
816 if (!mapping->a_ops->writepage)
817 /* No write method for the address space */
818 return -EINVAL;
819
820 if (!clear_page_dirty_for_io(page))
821 /* Someone else already triggered a write */
822 return -EAGAIN;
823
824 /*
825 * A dirty page may imply that the underlying filesystem has
826 * the page on some queue. So the page must be clean for
827 * migration. Writeout may mean we loose the lock and the
828 * page state is no longer what we checked for earlier.
829 * At this point we know that the migration attempt cannot
830 * be successful.
831 */
Kirill A. Shutemove3884662016-03-17 14:20:07 -0700832 remove_migration_ptes(page, page, false);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700833
834 rc = mapping->a_ops->writepage(page, &wbc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700835
836 if (rc != AOP_WRITEPAGE_ACTIVATE)
837 /* unlocked. Relock */
838 lock_page(page);
839
Hugh Dickinsbda85502008-11-19 15:36:36 -0800840 return (rc < 0) ? -EIO : -EAGAIN;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700841}
842
843/*
844 * Default handling if a filesystem does not provide a migration function.
845 */
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700846static int fallback_migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800847 struct page *newpage, struct page *page, enum migrate_mode mode)
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700848{
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800849 if (PageDirty(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800850 /* Only writeback pages in full synchronous migration */
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700851 switch (mode) {
852 case MIGRATE_SYNC:
853 case MIGRATE_SYNC_NO_COPY:
854 break;
855 default:
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800856 return -EBUSY;
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700857 }
Christoph Lameter04e62a22006-06-23 02:03:38 -0700858 return writeout(mapping, page);
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800859 }
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700860
861 /*
862 * Buffers may be managed in a filesystem specific way.
863 * We must have no buffers or drop them.
864 */
David Howells266cf652009-04-03 16:42:36 +0100865 if (page_has_private(page) &&
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700866 !try_to_release_page(page, GFP_KERNEL))
Mel Gorman806031b2019-03-05 15:44:43 -0800867 return mode == MIGRATE_SYNC ? -EAGAIN : -EBUSY;
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700868
Mel Gormana6bc32b2012-01-12 17:19:43 -0800869 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700870}
871
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700872/*
Christoph Lametere24f0b82006-06-23 02:03:51 -0700873 * Move a page to a newly allocated page
874 * The page is locked and all ptes have been successfully removed.
875 *
876 * The new page will have replaced the old page if this function
877 * is successful.
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700878 *
879 * Return value:
880 * < 0 - error code
Rafael Aquini78bd5202012-12-11 16:02:31 -0800881 * MIGRATEPAGE_SUCCESS - success
Christoph Lametere24f0b82006-06-23 02:03:51 -0700882 */
Mel Gorman3fe20112010-05-24 14:32:20 -0700883static int move_to_new_page(struct page *newpage, struct page *page,
Hugh Dickins5c3f9a62015-11-05 18:49:53 -0800884 enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700885{
886 struct address_space *mapping;
Minchan Kimbda807d2016-07-26 15:23:05 -0700887 int rc = -EAGAIN;
888 bool is_lru = !__PageMovable(page);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700889
Hugh Dickins7db76712015-11-05 18:49:49 -0800890 VM_BUG_ON_PAGE(!PageLocked(page), page);
891 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700892
Christoph Lametere24f0b82006-06-23 02:03:51 -0700893 mapping = page_mapping(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700894
895 if (likely(is_lru)) {
896 if (!mapping)
897 rc = migrate_page(mapping, newpage, page, mode);
898 else if (mapping->a_ops->migratepage)
899 /*
900 * Most pages have a mapping and most filesystems
901 * provide a migratepage callback. Anonymous pages
902 * are part of swap space which also has its own
903 * migratepage callback. This is the most common path
904 * for page migration.
905 */
906 rc = mapping->a_ops->migratepage(mapping, newpage,
907 page, mode);
908 else
909 rc = fallback_migrate_page(mapping, newpage,
910 page, mode);
911 } else {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700912 /*
Minchan Kimbda807d2016-07-26 15:23:05 -0700913 * In case of non-lru page, it could be released after
914 * isolation step. In that case, we shouldn't try migration.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700915 */
Minchan Kimbda807d2016-07-26 15:23:05 -0700916 VM_BUG_ON_PAGE(!PageIsolated(page), page);
917 if (!PageMovable(page)) {
918 rc = MIGRATEPAGE_SUCCESS;
919 __ClearPageIsolated(page);
920 goto out;
921 }
922
923 rc = mapping->a_ops->migratepage(mapping, newpage,
924 page, mode);
925 WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS &&
926 !PageIsolated(page));
927 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700928
Hugh Dickins5c3f9a62015-11-05 18:49:53 -0800929 /*
930 * When successful, old pagecache page->mapping must be cleared before
931 * page is freed; but stats require that PageAnon be left as PageAnon.
932 */
933 if (rc == MIGRATEPAGE_SUCCESS) {
Minchan Kimbda807d2016-07-26 15:23:05 -0700934 if (__PageMovable(page)) {
935 VM_BUG_ON_PAGE(!PageIsolated(page), page);
936
937 /*
938 * We clear PG_movable under page_lock so any compactor
939 * cannot try to migrate this page.
940 */
941 __ClearPageIsolated(page);
942 }
943
944 /*
Ralph Campbellc23a0c92020-01-30 22:14:41 -0800945 * Anonymous and movable page->mapping will be cleared by
Minchan Kimbda807d2016-07-26 15:23:05 -0700946 * free_pages_prepare so don't reset it here for keeping
947 * the type to work PageAnon, for example.
948 */
949 if (!PageMappingFlags(page))
Hugh Dickins5c3f9a62015-11-05 18:49:53 -0800950 page->mapping = NULL;
Lars Perssond2b2c6dd2019-03-28 20:44:28 -0700951
Christoph Hellwig25b29952019-06-13 22:50:49 +0200952 if (likely(!is_zone_device_page(newpage)))
Lars Perssond2b2c6dd2019-03-28 20:44:28 -0700953 flush_dcache_page(newpage);
954
Mel Gorman3fe20112010-05-24 14:32:20 -0700955 }
Minchan Kimbda807d2016-07-26 15:23:05 -0700956out:
Christoph Lametere24f0b82006-06-23 02:03:51 -0700957 return rc;
958}
959
Minchan Kim0dabec92011-10-31 17:06:57 -0700960static int __unmap_and_move(struct page *page, struct page *newpage,
Hugh Dickins9c620e22013-02-22 16:35:14 -0800961 int force, enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700962{
Minchan Kim0dabec92011-10-31 17:06:57 -0700963 int rc = -EAGAIN;
Baolin Wang213ecb32021-09-08 15:18:06 -0700964 bool page_was_mapped = false;
Mel Gorman3f6c8272010-05-24 14:32:17 -0700965 struct anon_vma *anon_vma = NULL;
Minchan Kimbda807d2016-07-26 15:23:05 -0700966 bool is_lru = !__PageMovable(page);
Christoph Lameter95a402c2006-06-23 02:03:53 -0700967
Nick Piggin529ae9a2008-08-02 12:01:03 +0200968 if (!trylock_page(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800969 if (!force || mode == MIGRATE_ASYNC)
Minchan Kim0dabec92011-10-31 17:06:57 -0700970 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800971
972 /*
973 * It's not safe for direct compaction to call lock_page.
974 * For example, during page readahead pages are added locked
975 * to the LRU. Later, when the IO completes the pages are
976 * marked uptodate and unlocked. However, the queueing
977 * could be merging multiple pages for one bio (e.g.
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -0700978 * mpage_readahead). If an allocation happens for the
Mel Gorman3e7d3442011-01-13 15:45:56 -0800979 * second or third page, the process can end up locking
980 * the same page twice and deadlocking. Rather than
981 * trying to be clever about what pages can be locked,
982 * avoid the use of lock_page for direct compaction
983 * altogether.
984 */
985 if (current->flags & PF_MEMALLOC)
Minchan Kim0dabec92011-10-31 17:06:57 -0700986 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800987
Christoph Lametere24f0b82006-06-23 02:03:51 -0700988 lock_page(page);
989 }
990
991 if (PageWriteback(page)) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700992 /*
Jianguo Wufed5b642013-04-29 15:07:58 -0700993 * Only in the case of a full synchronous migration is it
Mel Gormana6bc32b2012-01-12 17:19:43 -0800994 * necessary to wait for PageWriteback. In the async case,
995 * the retry loop is too short and in the sync-light case,
996 * the overhead of stalling is too much
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700997 */
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700998 switch (mode) {
999 case MIGRATE_SYNC:
1000 case MIGRATE_SYNC_NO_COPY:
1001 break;
1002 default:
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -07001003 rc = -EBUSY;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07001004 goto out_unlock;
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -07001005 }
1006 if (!force)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07001007 goto out_unlock;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001008 wait_on_page_writeback(page);
1009 }
Hugh Dickins03f15c82015-11-05 18:49:56 -08001010
Christoph Lametere24f0b82006-06-23 02:03:51 -07001011 /*
Baolin Wang68a98432021-09-08 15:18:03 -07001012 * By try_to_migrate(), page->mapcount goes down to 0 here. In this case,
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001013 * we cannot notice that anon_vma is freed while we migrates a page.
Hugh Dickins1ce82b62011-01-13 15:47:30 -08001014 * This get_anon_vma() delays freeing anon_vma pointer until the end
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001015 * of migration. File cache pages are no problem because of page_lock()
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -07001016 * File Caches may use write_page() or lock_page() in migration, then,
1017 * just care Anon page here.
Hugh Dickins03f15c82015-11-05 18:49:56 -08001018 *
1019 * Only page_get_anon_vma() understands the subtleties of
1020 * getting a hold on an anon_vma from outside one of its mms.
1021 * But if we cannot get anon_vma, then we won't need it anyway,
1022 * because that implies that the anon page is no longer mapped
1023 * (and cannot be remapped so long as we hold the page lock).
Christoph Lametere24f0b82006-06-23 02:03:51 -07001024 */
Hugh Dickins03f15c82015-11-05 18:49:56 -08001025 if (PageAnon(page) && !PageKsm(page))
Peter Zijlstra746b18d2011-05-24 17:12:10 -07001026 anon_vma = page_get_anon_vma(page);
Shaohua Li62e1c552008-02-04 22:29:33 -08001027
Hugh Dickins7db76712015-11-05 18:49:49 -08001028 /*
1029 * Block others from accessing the new page when we get around to
1030 * establishing additional references. We are usually the only one
1031 * holding a reference to newpage at this point. We used to have a BUG
1032 * here if trylock_page(newpage) fails, but would like to allow for
1033 * cases where there might be a race with the previous use of newpage.
1034 * This is much like races on refcount of oldpage: just don't BUG().
1035 */
1036 if (unlikely(!trylock_page(newpage)))
1037 goto out_unlock;
1038
Minchan Kimbda807d2016-07-26 15:23:05 -07001039 if (unlikely(!is_lru)) {
1040 rc = move_to_new_page(newpage, page, mode);
1041 goto out_unlock_both;
1042 }
1043
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001044 /*
Shaohua Li62e1c552008-02-04 22:29:33 -08001045 * Corner case handling:
1046 * 1. When a new swap-cache page is read into, it is added to the LRU
1047 * and treated as swapcache but it has no rmap yet.
1048 * Calling try_to_unmap() against a page->mapping==NULL page will
1049 * trigger a BUG. So handle it here.
Yang Shid12b8952020-12-14 19:13:02 -08001050 * 2. An orphaned page (see truncate_cleanup_page) might have
Shaohua Li62e1c552008-02-04 22:29:33 -08001051 * fs-private metadata. The page can be picked up due to memory
1052 * offlining. Everywhere else except page reclaim, the page is
1053 * invisible to the vm, so the page can not be migrated. So try to
1054 * free the metadata, so the page can be freed.
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001055 */
Shaohua Li62e1c552008-02-04 22:29:33 -08001056 if (!page->mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -08001057 VM_BUG_ON_PAGE(PageAnon(page), page);
Hugh Dickins1ce82b62011-01-13 15:47:30 -08001058 if (page_has_private(page)) {
Shaohua Li62e1c552008-02-04 22:29:33 -08001059 try_to_free_buffers(page);
Hugh Dickins7db76712015-11-05 18:49:49 -08001060 goto out_unlock_both;
Shaohua Li62e1c552008-02-04 22:29:33 -08001061 }
Hugh Dickins7db76712015-11-05 18:49:49 -08001062 } else if (page_mapped(page)) {
1063 /* Establish migration ptes */
Hugh Dickins03f15c82015-11-05 18:49:56 -08001064 VM_BUG_ON_PAGE(PageAnon(page) && !PageKsm(page) && !anon_vma,
1065 page);
Alistair Popplea98a2f02021-06-30 18:54:16 -07001066 try_to_migrate(page, 0);
Baolin Wang213ecb32021-09-08 15:18:06 -07001067 page_was_mapped = true;
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001068 }
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001069
Christoph Lametere6a15302006-06-25 05:46:49 -07001070 if (!page_mapped(page))
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001071 rc = move_to_new_page(newpage, page, mode);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001072
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001073 if (page_was_mapped)
1074 remove_migration_ptes(page,
Kirill A. Shutemove3884662016-03-17 14:20:07 -07001075 rc == MIGRATEPAGE_SUCCESS ? newpage : page, false);
Mel Gorman3f6c8272010-05-24 14:32:17 -07001076
Hugh Dickins7db76712015-11-05 18:49:49 -08001077out_unlock_both:
1078 unlock_page(newpage);
1079out_unlock:
Mel Gorman3f6c8272010-05-24 14:32:17 -07001080 /* Drop an anon_vma reference if we took one */
Rik van Riel76545062010-08-09 17:18:41 -07001081 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -07001082 put_anon_vma(anon_vma);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001083 unlock_page(page);
Minchan Kim0dabec92011-10-31 17:06:57 -07001084out:
Minchan Kimc6c919e2016-07-26 15:23:02 -07001085 /*
1086 * If migration is successful, decrease refcount of the newpage
1087 * which will not free the page because new page owner increased
1088 * refcounter. As well, if it is LRU page, add the page to LRU
David Hildenbrande0a352f2019-02-01 14:21:19 -08001089 * list in here. Use the old state of the isolated source page to
1090 * determine if we migrated a LRU page. newpage was already unlocked
1091 * and possibly modified by its owner - don't rely on the page
1092 * state.
Minchan Kimc6c919e2016-07-26 15:23:02 -07001093 */
1094 if (rc == MIGRATEPAGE_SUCCESS) {
David Hildenbrande0a352f2019-02-01 14:21:19 -08001095 if (unlikely(!is_lru))
Minchan Kimc6c919e2016-07-26 15:23:02 -07001096 put_page(newpage);
1097 else
1098 putback_lru_page(newpage);
1099 }
1100
Minchan Kim0dabec92011-10-31 17:06:57 -07001101 return rc;
1102}
Christoph Lameter95a402c2006-06-23 02:03:53 -07001103
Dave Hansen79c28a42021-09-02 14:59:06 -07001104
1105/*
1106 * node_demotion[] example:
1107 *
1108 * Consider a system with two sockets. Each socket has
1109 * three classes of memory attached: fast, medium and slow.
1110 * Each memory class is placed in its own NUMA node. The
1111 * CPUs are placed in the node with the "fast" memory. The
1112 * 6 NUMA nodes (0-5) might be split among the sockets like
1113 * this:
1114 *
1115 * Socket A: 0, 1, 2
1116 * Socket B: 3, 4, 5
1117 *
1118 * When Node 0 fills up, its memory should be migrated to
1119 * Node 1. When Node 1 fills up, it should be migrated to
1120 * Node 2. The migration path start on the nodes with the
1121 * processors (since allocations default to this node) and
1122 * fast memory, progress through medium and end with the
1123 * slow memory:
1124 *
1125 * 0 -> 1 -> 2 -> stop
1126 * 3 -> 4 -> 5 -> stop
1127 *
1128 * This is represented in the node_demotion[] like this:
1129 *
1130 * { 1, // Node 0 migrates to 1
1131 * 2, // Node 1 migrates to 2
1132 * -1, // Node 2 does not migrate
1133 * 4, // Node 3 migrates to 4
1134 * 5, // Node 4 migrates to 5
1135 * -1} // Node 5 does not migrate
1136 */
1137
1138/*
1139 * Writes to this array occur without locking. Cycles are
1140 * not allowed: Node X demotes to Y which demotes to X...
1141 *
1142 * If multiple reads are performed, a single rcu_read_lock()
1143 * must be held over all reads to ensure that no cycles are
1144 * observed.
1145 */
1146static int node_demotion[MAX_NUMNODES] __read_mostly =
1147 {[0 ... MAX_NUMNODES - 1] = NUMA_NO_NODE};
1148
1149/**
1150 * next_demotion_node() - Get the next node in the demotion path
1151 * @node: The starting node to lookup the next node
1152 *
Randy Dunlapc9bd7d12021-09-02 15:00:36 -07001153 * Return: node id for next memory node in the demotion path hierarchy
Dave Hansen79c28a42021-09-02 14:59:06 -07001154 * from @node; NUMA_NO_NODE if @node is terminal. This does not keep
1155 * @node online or guarantee that it *continues* to be the next demotion
1156 * target.
1157 */
1158int next_demotion_node(int node)
1159{
1160 int target;
1161
1162 /*
1163 * node_demotion[] is updated without excluding this
1164 * function from running. RCU doesn't provide any
1165 * compiler barriers, so the READ_ONCE() is required
1166 * to avoid compiler reordering or read merging.
1167 *
1168 * Make sure to use RCU over entire code blocks if
1169 * node_demotion[] reads need to be consistent.
1170 */
1171 rcu_read_lock();
1172 target = READ_ONCE(node_demotion[node]);
1173 rcu_read_unlock();
1174
1175 return target;
1176}
1177
Minchan Kim0dabec92011-10-31 17:06:57 -07001178/*
1179 * Obtain the lock on page, remove all ptes and migrate the page
1180 * to the newly allocated page in newpage.
1181 */
Linus Torvalds6ec44762020-07-08 10:48:35 -07001182static int unmap_and_move(new_page_t get_new_page,
Geert Uytterhoevenef2a5152015-04-14 15:44:22 -07001183 free_page_t put_new_page,
1184 unsigned long private, struct page *page,
Naoya Horiguchiadd05ce2015-06-24 16:56:50 -07001185 int force, enum migrate_mode mode,
Yang Shidd4ae782020-12-14 19:13:06 -08001186 enum migrate_reason reason,
1187 struct list_head *ret)
Minchan Kim0dabec92011-10-31 17:06:57 -07001188{
Hugh Dickins2def7422015-11-05 18:49:46 -08001189 int rc = MIGRATEPAGE_SUCCESS;
Yang Shi74d4a572019-11-30 17:57:12 -08001190 struct page *newpage = NULL;
Minchan Kim0dabec92011-10-31 17:06:57 -07001191
Michal Hocko94723aa2018-04-10 16:30:07 -07001192 if (!thp_migration_supported() && PageTransHuge(page))
Yang Shid532e2e2020-12-14 19:13:16 -08001193 return -ENOSYS;
Michal Hocko94723aa2018-04-10 16:30:07 -07001194
Minchan Kim0dabec92011-10-31 17:06:57 -07001195 if (page_count(page) == 1) {
1196 /* page was freed from under us. So we are done. */
Minchan Kimc6c919e2016-07-26 15:23:02 -07001197 ClearPageActive(page);
1198 ClearPageUnevictable(page);
Minchan Kimbda807d2016-07-26 15:23:05 -07001199 if (unlikely(__PageMovable(page))) {
1200 lock_page(page);
1201 if (!PageMovable(page))
1202 __ClearPageIsolated(page);
1203 unlock_page(page);
1204 }
Minchan Kim0dabec92011-10-31 17:06:57 -07001205 goto out;
1206 }
1207
Yang Shi74d4a572019-11-30 17:57:12 -08001208 newpage = get_new_page(page, private);
1209 if (!newpage)
1210 return -ENOMEM;
1211
Hugh Dickins9c620e22013-02-22 16:35:14 -08001212 rc = __unmap_and_move(page, newpage, force, mode);
Minchan Kimc6c919e2016-07-26 15:23:02 -07001213 if (rc == MIGRATEPAGE_SUCCESS)
Vlastimil Babka7cd12b42016-03-15 14:56:18 -07001214 set_page_owner_migrate_reason(newpage, reason);
Rafael Aquinibf6bddf12012-12-11 16:02:42 -08001215
Minchan Kim0dabec92011-10-31 17:06:57 -07001216out:
Christoph Lametere24f0b82006-06-23 02:03:51 -07001217 if (rc != -EAGAIN) {
Minchan Kim0dabec92011-10-31 17:06:57 -07001218 /*
1219 * A page that has been migrated has all references
1220 * removed and will be freed. A page that has not been
Ralph Campbellc23a0c92020-01-30 22:14:41 -08001221 * migrated will have kept its references and be restored.
Minchan Kim0dabec92011-10-31 17:06:57 -07001222 */
1223 list_del(&page->lru);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001224 }
David Rientjes68711a72014-06-04 16:08:25 -07001225
Christoph Lameter95a402c2006-06-23 02:03:53 -07001226 /*
Minchan Kimc6c919e2016-07-26 15:23:02 -07001227 * If migration is successful, releases reference grabbed during
1228 * isolation. Otherwise, restore the page to right list unless
1229 * we want to retry.
Christoph Lameter95a402c2006-06-23 02:03:53 -07001230 */
Minchan Kimc6c919e2016-07-26 15:23:02 -07001231 if (rc == MIGRATEPAGE_SUCCESS) {
Yang Shidd4ae782020-12-14 19:13:06 -08001232 /*
1233 * Compaction can migrate also non-LRU pages which are
1234 * not accounted to NR_ISOLATED_*. They can be recognized
1235 * as __PageMovable
1236 */
1237 if (likely(!__PageMovable(page)))
1238 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
1239 page_is_file_lru(page), -thp_nr_pages(page));
1240
Oscar Salvador79f5f8f2020-10-15 20:07:09 -07001241 if (reason != MR_MEMORY_FAILURE)
Minchan Kimc6c919e2016-07-26 15:23:02 -07001242 /*
Oscar Salvador79f5f8f2020-10-15 20:07:09 -07001243 * We release the page in page_handle_poison.
Minchan Kimc6c919e2016-07-26 15:23:02 -07001244 */
Oscar Salvador79f5f8f2020-10-15 20:07:09 -07001245 put_page(page);
Minchan Kimc6c919e2016-07-26 15:23:02 -07001246 } else {
Yang Shidd4ae782020-12-14 19:13:06 -08001247 if (rc != -EAGAIN)
1248 list_add_tail(&page->lru, ret);
Minchan Kimbda807d2016-07-26 15:23:05 -07001249
Minchan Kimc6c919e2016-07-26 15:23:02 -07001250 if (put_new_page)
1251 put_new_page(newpage, private);
1252 else
1253 put_page(newpage);
1254 }
David Rientjes68711a72014-06-04 16:08:25 -07001255
Christoph Lametere24f0b82006-06-23 02:03:51 -07001256 return rc;
1257}
1258
1259/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001260 * Counterpart of unmap_and_move_page() for hugepage migration.
1261 *
1262 * This function doesn't wait the completion of hugepage I/O
1263 * because there is no race between I/O and migration for hugepage.
1264 * Note that currently hugepage I/O occurs only in direct I/O
1265 * where no lock is held and PG_writeback is irrelevant,
1266 * and writeback status of all subpages are counted in the reference
1267 * count of the head page (i.e. if all subpages of a 2MB hugepage are
1268 * under direct I/O, the reference of the head page is 512 and a bit more.)
1269 * This means that when we try to migrate hugepage whose subpages are
1270 * doing direct I/O, some references remain after try_to_unmap() and
1271 * hugepage migration fails without data corruption.
1272 *
1273 * There is also no race when direct I/O is issued on the page under migration,
1274 * because then pte is replaced with migration swap entry and direct I/O code
1275 * will wait in the page fault for migration to complete.
1276 */
1277static int unmap_and_move_huge_page(new_page_t get_new_page,
David Rientjes68711a72014-06-04 16:08:25 -07001278 free_page_t put_new_page, unsigned long private,
1279 struct page *hpage, int force,
Yang Shidd4ae782020-12-14 19:13:06 -08001280 enum migrate_mode mode, int reason,
1281 struct list_head *ret)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001282{
Hugh Dickins2def7422015-11-05 18:49:46 -08001283 int rc = -EAGAIN;
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001284 int page_was_mapped = 0;
Joonsoo Kim32665f22014-01-21 15:51:15 -08001285 struct page *new_hpage;
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001286 struct anon_vma *anon_vma = NULL;
Mike Kravetzc0d03812020-04-01 21:11:05 -07001287 struct address_space *mapping = NULL;
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001288
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001289 /*
Anshuman Khandual7ed2c312019-03-05 15:43:44 -08001290 * Migratability of hugepages depends on architectures and their size.
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001291 * This check is necessary because some callers of hugepage migration
1292 * like soft offline and memory hotremove don't walk through page
1293 * tables or check whether the hugepage is pmd-based or not before
1294 * kicking migration.
1295 */
Naoya Horiguchi100873d2014-06-04 16:10:56 -07001296 if (!hugepage_migration_supported(page_hstate(hpage))) {
Yang Shidd4ae782020-12-14 19:13:06 -08001297 list_move_tail(&hpage->lru, ret);
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001298 return -ENOSYS;
Joonsoo Kim32665f22014-01-21 15:51:15 -08001299 }
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001300
Muchun Song71a64f62021-02-04 18:32:17 -08001301 if (page_count(hpage) == 1) {
1302 /* page was freed from under us. So we are done. */
1303 putback_active_hugepage(hpage);
1304 return MIGRATEPAGE_SUCCESS;
1305 }
1306
Michal Hocko666feb22018-04-10 16:30:03 -07001307 new_hpage = get_new_page(hpage, private);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001308 if (!new_hpage)
1309 return -ENOMEM;
1310
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001311 if (!trylock_page(hpage)) {
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07001312 if (!force)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001313 goto out;
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07001314 switch (mode) {
1315 case MIGRATE_SYNC:
1316 case MIGRATE_SYNC_NO_COPY:
1317 break;
1318 default:
1319 goto out;
1320 }
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001321 lock_page(hpage);
1322 }
1323
Mike Kravetzcb6acd02019-02-28 16:22:02 -08001324 /*
1325 * Check for pages which are in the process of being freed. Without
1326 * page_mapping() set, hugetlbfs specific move page routine will not
1327 * be called and we could leak usage counts for subpools.
1328 */
Muchun Song6acfb5b2021-06-30 18:51:29 -07001329 if (hugetlb_page_subpool(hpage) && !page_mapping(hpage)) {
Mike Kravetzcb6acd02019-02-28 16:22:02 -08001330 rc = -EBUSY;
1331 goto out_unlock;
1332 }
1333
Peter Zijlstra746b18d2011-05-24 17:12:10 -07001334 if (PageAnon(hpage))
1335 anon_vma = page_get_anon_vma(hpage);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001336
Hugh Dickins7db76712015-11-05 18:49:49 -08001337 if (unlikely(!trylock_page(new_hpage)))
1338 goto put_anon;
1339
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001340 if (page_mapped(hpage)) {
Mike Kravetz336bf302020-11-13 22:52:16 -08001341 bool mapping_locked = false;
Alistair Popplea98a2f02021-06-30 18:54:16 -07001342 enum ttu_flags ttu = 0;
Mike Kravetzc0d03812020-04-01 21:11:05 -07001343
Mike Kravetz336bf302020-11-13 22:52:16 -08001344 if (!PageAnon(hpage)) {
1345 /*
1346 * In shared mappings, try_to_unmap could potentially
1347 * call huge_pmd_unshare. Because of this, take
1348 * semaphore in write mode here and set TTU_RMAP_LOCKED
1349 * to let lower levels know we have taken the lock.
1350 */
1351 mapping = hugetlb_page_mapping_lock_write(hpage);
1352 if (unlikely(!mapping))
1353 goto unlock_put_anon;
1354
1355 mapping_locked = true;
1356 ttu |= TTU_RMAP_LOCKED;
1357 }
1358
Alistair Popplea98a2f02021-06-30 18:54:16 -07001359 try_to_migrate(hpage, ttu);
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001360 page_was_mapped = 1;
Mike Kravetz336bf302020-11-13 22:52:16 -08001361
1362 if (mapping_locked)
1363 i_mmap_unlock_write(mapping);
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001364 }
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001365
1366 if (!page_mapped(hpage))
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001367 rc = move_to_new_page(new_hpage, hpage, mode);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001368
Mike Kravetz336bf302020-11-13 22:52:16 -08001369 if (page_was_mapped)
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001370 remove_migration_ptes(hpage,
Mike Kravetz336bf302020-11-13 22:52:16 -08001371 rc == MIGRATEPAGE_SUCCESS ? new_hpage : hpage, false);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001372
Mike Kravetzc0d03812020-04-01 21:11:05 -07001373unlock_put_anon:
Hugh Dickins7db76712015-11-05 18:49:49 -08001374 unlock_page(new_hpage);
1375
1376put_anon:
Hugh Dickinsfd4a4662011-01-13 15:47:31 -08001377 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -07001378 put_anon_vma(anon_vma);
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -07001379
Hugh Dickins2def7422015-11-05 18:49:46 -08001380 if (rc == MIGRATEPAGE_SUCCESS) {
Michal Hockoab5ac902018-01-31 16:20:48 -08001381 move_hugetlb_state(hpage, new_hpage, reason);
Hugh Dickins2def7422015-11-05 18:49:46 -08001382 put_new_page = NULL;
1383 }
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -07001384
Mike Kravetzcb6acd02019-02-28 16:22:02 -08001385out_unlock:
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001386 unlock_page(hpage);
Hillf Danton09761332011-12-08 14:34:20 -08001387out:
Yang Shidd4ae782020-12-14 19:13:06 -08001388 if (rc == MIGRATEPAGE_SUCCESS)
Naoya Horiguchib8ec1ce2013-09-11 14:22:01 -07001389 putback_active_hugepage(hpage);
Miaohe Lina04840c2021-05-04 18:37:07 -07001390 else if (rc != -EAGAIN)
Yang Shidd4ae782020-12-14 19:13:06 -08001391 list_move_tail(&hpage->lru, ret);
David Rientjes68711a72014-06-04 16:08:25 -07001392
1393 /*
1394 * If migration was not successful and there's a freeing callback, use
1395 * it. Otherwise, put_page() will drop the reference grabbed during
1396 * isolation.
1397 */
Hugh Dickins2def7422015-11-05 18:49:46 -08001398 if (put_new_page)
David Rientjes68711a72014-06-04 16:08:25 -07001399 put_new_page(new_hpage, private);
1400 else
Naoya Horiguchi3aaa76e2015-09-22 14:59:14 -07001401 putback_active_hugepage(new_hpage);
David Rientjes68711a72014-06-04 16:08:25 -07001402
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001403 return rc;
1404}
1405
Yang Shid532e2e2020-12-14 19:13:16 -08001406static inline int try_split_thp(struct page *page, struct page **page2,
1407 struct list_head *from)
1408{
1409 int rc = 0;
1410
1411 lock_page(page);
1412 rc = split_huge_page_to_list(page, from);
1413 unlock_page(page);
1414 if (!rc)
1415 list_safe_reset_next(page, *page2, lru);
1416
1417 return rc;
1418}
1419
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001420/*
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001421 * migrate_pages - migrate the pages specified in a list, to the free pages
1422 * supplied as the target for the page migration
Christoph Lameterb20a3502006-03-22 00:09:12 -08001423 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001424 * @from: The list of pages to be migrated.
1425 * @get_new_page: The function used to allocate free pages to be used
1426 * as the target of the page migration.
David Rientjes68711a72014-06-04 16:08:25 -07001427 * @put_new_page: The function used to free target pages if migration
1428 * fails, or NULL if no special handling is necessary.
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001429 * @private: Private data to be passed on to get_new_page()
1430 * @mode: The migration mode that specifies the constraints for
1431 * page migration, if any.
1432 * @reason: The reason for page migration.
Yang Shi5ac95882021-09-02 14:59:13 -07001433 * @ret_succeeded: Set to the number of pages migrated successfully if
1434 * the caller passes a non-NULL pointer.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001435 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001436 * The function returns after 10 attempts or if no pages are movable any more
1437 * because the list has become empty or no retryable pages exist any more.
Yang Shidd4ae782020-12-14 19:13:06 -08001438 * It is caller's responsibility to call putback_movable_pages() to return pages
1439 * to the LRU or free list only if ret != 0.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001440 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001441 * Returns the number of pages that were not migrated, or an error code.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001442 */
Hugh Dickins9c620e22013-02-22 16:35:14 -08001443int migrate_pages(struct list_head *from, new_page_t get_new_page,
David Rientjes68711a72014-06-04 16:08:25 -07001444 free_page_t put_new_page, unsigned long private,
Yang Shi5ac95882021-09-02 14:59:13 -07001445 enum migrate_mode mode, int reason, unsigned int *ret_succeeded)
Christoph Lameterb20a3502006-03-22 00:09:12 -08001446{
Christoph Lametere24f0b82006-06-23 02:03:51 -07001447 int retry = 1;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001448 int thp_retry = 1;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001449 int nr_failed = 0;
Mel Gorman5647bc22012-10-19 10:46:20 +01001450 int nr_succeeded = 0;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001451 int nr_thp_succeeded = 0;
1452 int nr_thp_failed = 0;
1453 int nr_thp_split = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001454 int pass = 0;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001455 bool is_thp = false;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001456 struct page *page;
1457 struct page *page2;
1458 int swapwrite = current->flags & PF_SWAPWRITE;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001459 int rc, nr_subpages;
Yang Shidd4ae782020-12-14 19:13:06 -08001460 LIST_HEAD(ret_pages);
Yang Shib0b515b2021-06-30 18:51:48 -07001461 bool nosplit = (reason == MR_NUMA_MISPLACED);
Christoph Lameterb20a3502006-03-22 00:09:12 -08001462
Liam Mark7bc1aec2021-05-04 18:37:25 -07001463 trace_mm_migrate_pages_start(mode, reason);
1464
Christoph Lameterb20a3502006-03-22 00:09:12 -08001465 if (!swapwrite)
1466 current->flags |= PF_SWAPWRITE;
1467
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001468 for (pass = 0; pass < 10 && (retry || thp_retry); pass++) {
Christoph Lametere24f0b82006-06-23 02:03:51 -07001469 retry = 0;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001470 thp_retry = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001471
Christoph Lametere24f0b82006-06-23 02:03:51 -07001472 list_for_each_entry_safe(page, page2, from, lru) {
Michal Hocko94723aa2018-04-10 16:30:07 -07001473retry:
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001474 /*
1475 * THP statistics is based on the source huge page.
1476 * Capture required information that might get lost
1477 * during migration.
1478 */
Zi Yan6c5c7b92020-09-25 21:19:14 -07001479 is_thp = PageTransHuge(page) && !PageHuge(page);
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -07001480 nr_subpages = thp_nr_pages(page);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001481 cond_resched();
Christoph Lameterb20a3502006-03-22 00:09:12 -08001482
Naoya Horiguchi31caf662013-09-11 14:21:59 -07001483 if (PageHuge(page))
1484 rc = unmap_and_move_huge_page(get_new_page,
David Rientjes68711a72014-06-04 16:08:25 -07001485 put_new_page, private, page,
Yang Shidd4ae782020-12-14 19:13:06 -08001486 pass > 2, mode, reason,
1487 &ret_pages);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07001488 else
David Rientjes68711a72014-06-04 16:08:25 -07001489 rc = unmap_and_move(get_new_page, put_new_page,
Naoya Horiguchiadd05ce2015-06-24 16:56:50 -07001490 private, page, pass > 2, mode,
Yang Shidd4ae782020-12-14 19:13:06 -08001491 reason, &ret_pages);
1492 /*
1493 * The rules are:
1494 * Success: non hugetlb page will be freed, hugetlb
1495 * page will be put back
1496 * -EAGAIN: stay on the from list
1497 * -ENOMEM: stay on the from list
1498 * Other errno: put on ret_pages list then splice to
1499 * from list
1500 */
Christoph Lametere24f0b82006-06-23 02:03:51 -07001501 switch(rc) {
Yang Shid532e2e2020-12-14 19:13:16 -08001502 /*
1503 * THP migration might be unsupported or the
1504 * allocation could've failed so we should
1505 * retry on the same page with the THP split
1506 * to base pages.
1507 *
1508 * Head page is retried immediately and tail
1509 * pages are added to the tail of the list so
1510 * we encounter them after the rest of the list
1511 * is processed.
1512 */
1513 case -ENOSYS:
1514 /* THP migration is unsupported */
1515 if (is_thp) {
1516 if (!try_split_thp(page, &page2, from)) {
1517 nr_thp_split++;
1518 goto retry;
1519 }
1520
1521 nr_thp_failed++;
1522 nr_failed += nr_subpages;
1523 break;
1524 }
1525
1526 /* Hugetlb migration is unsupported */
1527 nr_failed++;
1528 break;
Christoph Lameter95a402c2006-06-23 02:03:53 -07001529 case -ENOMEM:
Michal Hocko94723aa2018-04-10 16:30:07 -07001530 /*
Yang Shid532e2e2020-12-14 19:13:16 -08001531 * When memory is low, don't bother to try to migrate
1532 * other pages, just exit.
Yang Shib0b515b2021-06-30 18:51:48 -07001533 * THP NUMA faulting doesn't split THP to retry.
Michal Hocko94723aa2018-04-10 16:30:07 -07001534 */
Yang Shib0b515b2021-06-30 18:51:48 -07001535 if (is_thp && !nosplit) {
Yang Shid532e2e2020-12-14 19:13:16 -08001536 if (!try_split_thp(page, &page2, from)) {
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001537 nr_thp_split++;
Michal Hocko94723aa2018-04-10 16:30:07 -07001538 goto retry;
1539 }
Zi Yan6c5c7b92020-09-25 21:19:14 -07001540
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001541 nr_thp_failed++;
1542 nr_failed += nr_subpages;
1543 goto out;
1544 }
David Rientjesdfef2ef2016-05-20 16:59:05 -07001545 nr_failed++;
Christoph Lameter95a402c2006-06-23 02:03:53 -07001546 goto out;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001547 case -EAGAIN:
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001548 if (is_thp) {
1549 thp_retry++;
1550 break;
1551 }
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001552 retry++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001553 break;
Rafael Aquini78bd5202012-12-11 16:02:31 -08001554 case MIGRATEPAGE_SUCCESS:
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001555 if (is_thp) {
1556 nr_thp_succeeded++;
1557 nr_succeeded += nr_subpages;
1558 break;
1559 }
Mel Gorman5647bc22012-10-19 10:46:20 +01001560 nr_succeeded++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001561 break;
1562 default:
Naoya Horiguchi354a3362014-01-21 15:51:14 -08001563 /*
Yang Shid532e2e2020-12-14 19:13:16 -08001564 * Permanent failure (-EBUSY, etc.):
Naoya Horiguchi354a3362014-01-21 15:51:14 -08001565 * unlike -EAGAIN case, the failed page is
1566 * removed from migration page list and not
1567 * retried in the next outer loop.
1568 */
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001569 if (is_thp) {
1570 nr_thp_failed++;
1571 nr_failed += nr_subpages;
1572 break;
1573 }
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001574 nr_failed++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001575 break;
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001576 }
Christoph Lameterb20a3502006-03-22 00:09:12 -08001577 }
1578 }
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001579 nr_failed += retry + thp_retry;
1580 nr_thp_failed += thp_retry;
Vlastimil Babkaf2f81fb2015-11-05 18:47:03 -08001581 rc = nr_failed;
Christoph Lameter95a402c2006-06-23 02:03:53 -07001582out:
Yang Shidd4ae782020-12-14 19:13:06 -08001583 /*
1584 * Put the permanent failure page back to migration list, they
1585 * will be put back to the right list by the caller.
1586 */
1587 list_splice(&ret_pages, from);
1588
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001589 count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
1590 count_vm_events(PGMIGRATE_FAIL, nr_failed);
1591 count_vm_events(THP_MIGRATION_SUCCESS, nr_thp_succeeded);
1592 count_vm_events(THP_MIGRATION_FAIL, nr_thp_failed);
1593 count_vm_events(THP_MIGRATION_SPLIT, nr_thp_split);
1594 trace_mm_migrate_pages(nr_succeeded, nr_failed, nr_thp_succeeded,
1595 nr_thp_failed, nr_thp_split, mode, reason);
Mel Gorman7b2a2d42012-10-19 14:07:31 +01001596
Christoph Lameterb20a3502006-03-22 00:09:12 -08001597 if (!swapwrite)
1598 current->flags &= ~PF_SWAPWRITE;
1599
Yang Shi5ac95882021-09-02 14:59:13 -07001600 if (ret_succeeded)
1601 *ret_succeeded = nr_succeeded;
1602
Rafael Aquini78bd5202012-12-11 16:02:31 -08001603 return rc;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001604}
Charan Teja Reddyf47b8522021-02-16 13:59:45 +05301605EXPORT_SYMBOL_GPL(migrate_pages);
Christoph Lameterb20a3502006-03-22 00:09:12 -08001606
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001607struct page *alloc_migration_target(struct page *page, unsigned long private)
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001608{
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001609 struct migration_target_control *mtc;
1610 gfp_t gfp_mask;
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001611 unsigned int order = 0;
1612 struct page *new_page = NULL;
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001613 int nid;
1614 int zidx;
1615
1616 mtc = (struct migration_target_control *)private;
1617 gfp_mask = mtc->gfp_mask;
1618 nid = mtc->nid;
1619 if (nid == NUMA_NO_NODE)
1620 nid = page_to_nid(page);
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001621
Joonsoo Kimd92bbc22020-08-11 18:37:17 -07001622 if (PageHuge(page)) {
1623 struct hstate *h = page_hstate(compound_head(page));
1624
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001625 gfp_mask = htlb_modify_alloc_mask(h, gfp_mask);
1626 return alloc_huge_page_nodemask(h, nid, mtc->nmask, gfp_mask);
Joonsoo Kimd92bbc22020-08-11 18:37:17 -07001627 }
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001628
1629 if (PageTransHuge(page)) {
Joonsoo Kim9933a0c2020-08-11 18:37:20 -07001630 /*
1631 * clear __GFP_RECLAIM to make the migration callback
1632 * consistent with regular THP allocations.
1633 */
1634 gfp_mask &= ~__GFP_RECLAIM;
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001635 gfp_mask |= GFP_TRANSHUGE;
1636 order = HPAGE_PMD_ORDER;
1637 }
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001638 zidx = zone_idx(page_zone(page));
1639 if (is_highmem_idx(zidx) || zidx == ZONE_MOVABLE)
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001640 gfp_mask |= __GFP_HIGHMEM;
1641
Matthew Wilcox (Oracle)84172f42021-04-29 23:01:15 -07001642 new_page = __alloc_pages(gfp_mask, order, nid, mtc->nmask);
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001643
1644 if (new_page && PageTransHuge(new_page))
1645 prep_transhuge_page(new_page);
1646
1647 return new_page;
1648}
1649
Christoph Lameter742755a2006-06-23 02:03:55 -07001650#ifdef CONFIG_NUMA
Christoph Lameter742755a2006-06-23 02:03:55 -07001651
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001652static int store_status(int __user *status, int start, int value, int nr)
Christoph Lameter742755a2006-06-23 02:03:55 -07001653{
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001654 while (nr-- > 0) {
1655 if (put_user(value, status + start))
1656 return -EFAULT;
1657 start++;
1658 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001659
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001660 return 0;
1661}
Christoph Lameter742755a2006-06-23 02:03:55 -07001662
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001663static int do_move_pages_to_node(struct mm_struct *mm,
1664 struct list_head *pagelist, int node)
1665{
1666 int err;
Joonsoo Kima0976312020-08-11 18:37:28 -07001667 struct migration_target_control mtc = {
1668 .nid = node,
1669 .gfp_mask = GFP_HIGHUSER_MOVABLE | __GFP_THISNODE,
1670 };
Christoph Lameter742755a2006-06-23 02:03:55 -07001671
Joonsoo Kima0976312020-08-11 18:37:28 -07001672 err = migrate_pages(pagelist, alloc_migration_target, NULL,
Yang Shi5ac95882021-09-02 14:59:13 -07001673 (unsigned long)&mtc, MIGRATE_SYNC, MR_SYSCALL, NULL);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001674 if (err)
1675 putback_movable_pages(pagelist);
1676 return err;
Christoph Lameter742755a2006-06-23 02:03:55 -07001677}
1678
1679/*
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001680 * Resolves the given address to a struct page, isolates it from the LRU and
1681 * puts it to the given pagelist.
Yang Shie0153fc2020-01-04 12:59:46 -08001682 * Returns:
1683 * errno - if the page cannot be found/isolated
1684 * 0 - when it doesn't have to be migrated because it is already on the
1685 * target node
1686 * 1 - when it has been queued
Christoph Lameter742755a2006-06-23 02:03:55 -07001687 */
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001688static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
1689 int node, struct list_head *pagelist, bool migrate_all)
Christoph Lameter742755a2006-06-23 02:03:55 -07001690{
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001691 struct vm_area_struct *vma;
1692 struct page *page;
1693 unsigned int follflags;
Christoph Lameter742755a2006-06-23 02:03:55 -07001694 int err;
Christoph Lameter742755a2006-06-23 02:03:55 -07001695
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001696 mmap_read_lock(mm);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001697 err = -EFAULT;
1698 vma = find_vma(mm, addr);
1699 if (!vma || addr < vma->vm_start || !vma_migratable(vma))
1700 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001701
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001702 /* FOLL_DUMP to ignore special (like zero) pages */
1703 follflags = FOLL_GET | FOLL_DUMP;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001704 page = follow_page(vma, addr, follflags);
Christoph Lameter742755a2006-06-23 02:03:55 -07001705
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001706 err = PTR_ERR(page);
1707 if (IS_ERR(page))
1708 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001709
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001710 err = -ENOENT;
1711 if (!page)
1712 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001713
Brice Gogline78bbfa2008-10-18 20:27:15 -07001714 err = 0;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001715 if (page_to_nid(page) == node)
1716 goto out_putpage;
Christoph Lameter742755a2006-06-23 02:03:55 -07001717
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001718 err = -EACCES;
1719 if (page_mapcount(page) > 1 && !migrate_all)
1720 goto out_putpage;
1721
1722 if (PageHuge(page)) {
1723 if (PageHead(page)) {
1724 isolate_huge_page(page, pagelist);
Yang Shie0153fc2020-01-04 12:59:46 -08001725 err = 1;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001726 }
1727 } else {
1728 struct page *head;
1729
1730 head = compound_head(page);
1731 err = isolate_lru_page(head);
1732 if (err)
1733 goto out_putpage;
1734
Yang Shie0153fc2020-01-04 12:59:46 -08001735 err = 1;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001736 list_add_tail(&head->lru, pagelist);
1737 mod_node_page_state(page_pgdat(head),
Huang Ying9de4f222020-04-06 20:04:41 -07001738 NR_ISOLATED_ANON + page_is_file_lru(head),
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -07001739 thp_nr_pages(head));
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001740 }
1741out_putpage:
1742 /*
1743 * Either remove the duplicate refcount from
1744 * isolate_lru_page() or drop the page ref if it was
1745 * not isolated.
1746 */
1747 put_page(page);
1748out:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001749 mmap_read_unlock(mm);
Christoph Lameter742755a2006-06-23 02:03:55 -07001750 return err;
1751}
1752
Wei Yang7ca87832020-04-06 20:04:12 -07001753static int move_pages_and_store_status(struct mm_struct *mm, int node,
1754 struct list_head *pagelist, int __user *status,
1755 int start, int i, unsigned long nr_pages)
1756{
1757 int err;
1758
Wei Yang5d7ae892020-04-06 20:04:15 -07001759 if (list_empty(pagelist))
1760 return 0;
1761
Wei Yang7ca87832020-04-06 20:04:12 -07001762 err = do_move_pages_to_node(mm, pagelist, node);
1763 if (err) {
1764 /*
1765 * Positive err means the number of failed
1766 * pages to migrate. Since we are going to
1767 * abort and return the number of non-migrated
Long Liab9dd4f2020-12-14 19:12:52 -08001768 * pages, so need to include the rest of the
Wei Yang7ca87832020-04-06 20:04:12 -07001769 * nr_pages that have not been attempted as
1770 * well.
1771 */
1772 if (err > 0)
1773 err += nr_pages - i - 1;
1774 return err;
1775 }
1776 return store_status(status, start, node, i - start);
1777}
1778
Christoph Lameter742755a2006-06-23 02:03:55 -07001779/*
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001780 * Migrate an array of page address onto an array of nodes and fill
1781 * the corresponding array of status.
1782 */
Christoph Lameter3268c632012-03-21 16:34:06 -07001783static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001784 unsigned long nr_pages,
1785 const void __user * __user *pages,
1786 const int __user *nodes,
1787 int __user *status, int flags)
1788{
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001789 int current_node = NUMA_NO_NODE;
1790 LIST_HEAD(pagelist);
1791 int start, i;
1792 int err = 0, err1;
Brice Goglin35282a22009-06-16 15:32:43 -07001793
Minchan Kim361a2a22021-05-04 18:36:57 -07001794 lru_cache_disable();
Brice Goglin35282a22009-06-16 15:32:43 -07001795
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001796 for (i = start = 0; i < nr_pages; i++) {
1797 const void __user *p;
1798 unsigned long addr;
1799 int node;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001800
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001801 err = -EFAULT;
1802 if (get_user(p, pages + i))
1803 goto out_flush;
1804 if (get_user(node, nodes + i))
1805 goto out_flush;
Andrey Konovalov057d33892019-09-25 16:48:30 -07001806 addr = (unsigned long)untagged_addr(p);
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001807
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001808 err = -ENODEV;
1809 if (node < 0 || node >= MAX_NUMNODES)
1810 goto out_flush;
1811 if (!node_state(node, N_MEMORY))
1812 goto out_flush;
Brice Goglin3140a222009-01-06 14:38:57 -08001813
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001814 err = -EACCES;
1815 if (!node_isset(node, task_nodes))
1816 goto out_flush;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001817
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001818 if (current_node == NUMA_NO_NODE) {
1819 current_node = node;
1820 start = i;
1821 } else if (node != current_node) {
Wei Yang7ca87832020-04-06 20:04:12 -07001822 err = move_pages_and_store_status(mm, current_node,
1823 &pagelist, status, start, i, nr_pages);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001824 if (err)
1825 goto out;
1826 start = i;
1827 current_node = node;
Brice Goglin3140a222009-01-06 14:38:57 -08001828 }
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001829
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001830 /*
1831 * Errors in the page lookup or isolation are not fatal and we simply
1832 * report them via status
1833 */
1834 err = add_page_for_migration(mm, addr, current_node,
1835 &pagelist, flags & MPOL_MF_MOVE_ALL);
Yang Shie0153fc2020-01-04 12:59:46 -08001836
Wei Yangd08221a2020-04-06 20:04:18 -07001837 if (err > 0) {
Yang Shie0153fc2020-01-04 12:59:46 -08001838 /* The page is successfully queued for migration */
1839 continue;
1840 }
Brice Goglin3140a222009-01-06 14:38:57 -08001841
Wei Yangd08221a2020-04-06 20:04:18 -07001842 /*
1843 * If the page is already on the target node (!err), store the
1844 * node, otherwise, store the err.
1845 */
1846 err = store_status(status, i, err ? : current_node, 1);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001847 if (err)
1848 goto out_flush;
Brice Goglin3140a222009-01-06 14:38:57 -08001849
Wei Yang7ca87832020-04-06 20:04:12 -07001850 err = move_pages_and_store_status(mm, current_node, &pagelist,
1851 status, start, i, nr_pages);
Wei Yang4afdace2020-04-06 20:04:09 -07001852 if (err)
1853 goto out;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001854 current_node = NUMA_NO_NODE;
Brice Goglin3140a222009-01-06 14:38:57 -08001855 }
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001856out_flush:
1857 /* Make sure we do not overwrite the existing error */
Wei Yang7ca87832020-04-06 20:04:12 -07001858 err1 = move_pages_and_store_status(mm, current_node, &pagelist,
1859 status, start, i, nr_pages);
Wei Yangdfe9aa22020-01-30 22:11:14 -08001860 if (err >= 0)
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001861 err = err1;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001862out:
Minchan Kim361a2a22021-05-04 18:36:57 -07001863 lru_cache_enable();
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001864 return err;
1865}
1866
1867/*
Brice Goglin2f007e72008-10-18 20:27:16 -07001868 * Determine the nodes of an array of pages and store it in an array of status.
Christoph Lameter742755a2006-06-23 02:03:55 -07001869 */
Brice Goglin80bba122008-12-09 13:14:23 -08001870static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1871 const void __user **pages, int *status)
Christoph Lameter742755a2006-06-23 02:03:55 -07001872{
Brice Goglin2f007e72008-10-18 20:27:16 -07001873 unsigned long i;
Brice Goglin2f007e72008-10-18 20:27:16 -07001874
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001875 mmap_read_lock(mm);
Christoph Lameter742755a2006-06-23 02:03:55 -07001876
Brice Goglin2f007e72008-10-18 20:27:16 -07001877 for (i = 0; i < nr_pages; i++) {
Brice Goglin80bba122008-12-09 13:14:23 -08001878 unsigned long addr = (unsigned long)(*pages);
Christoph Lameter742755a2006-06-23 02:03:55 -07001879 struct vm_area_struct *vma;
1880 struct page *page;
KOSAKI Motohiroc095adb2008-12-16 16:06:43 +09001881 int err = -EFAULT;
Brice Goglin2f007e72008-10-18 20:27:16 -07001882
Liam Howlett059b8b42021-06-28 19:39:44 -07001883 vma = vma_lookup(mm, addr);
1884 if (!vma)
Christoph Lameter742755a2006-06-23 02:03:55 -07001885 goto set_status;
1886
Kirill A. Shutemovd8998442015-09-04 15:47:53 -07001887 /* FOLL_DUMP to ignore special (like zero) pages */
1888 page = follow_page(vma, addr, FOLL_DUMP);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001889
1890 err = PTR_ERR(page);
1891 if (IS_ERR(page))
1892 goto set_status;
1893
Kirill A. Shutemovd8998442015-09-04 15:47:53 -07001894 err = page ? page_to_nid(page) : -ENOENT;
Christoph Lameter742755a2006-06-23 02:03:55 -07001895set_status:
Brice Goglin80bba122008-12-09 13:14:23 -08001896 *status = err;
1897
1898 pages++;
1899 status++;
1900 }
1901
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001902 mmap_read_unlock(mm);
Brice Goglin80bba122008-12-09 13:14:23 -08001903}
1904
Arnd Bergmann5b1b5612021-09-08 15:18:17 -07001905static int get_compat_pages_array(const void __user *chunk_pages[],
1906 const void __user * __user *pages,
1907 unsigned long chunk_nr)
1908{
1909 compat_uptr_t __user *pages32 = (compat_uptr_t __user *)pages;
1910 compat_uptr_t p;
1911 int i;
1912
1913 for (i = 0; i < chunk_nr; i++) {
1914 if (get_user(p, pages32 + i))
1915 return -EFAULT;
1916 chunk_pages[i] = compat_ptr(p);
1917 }
1918
1919 return 0;
1920}
1921
Brice Goglin80bba122008-12-09 13:14:23 -08001922/*
1923 * Determine the nodes of a user array of pages and store it in
1924 * a user array of status.
1925 */
1926static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1927 const void __user * __user *pages,
1928 int __user *status)
1929{
1930#define DO_PAGES_STAT_CHUNK_NR 16
1931 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1932 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
Brice Goglin80bba122008-12-09 13:14:23 -08001933
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001934 while (nr_pages) {
1935 unsigned long chunk_nr;
Brice Goglin80bba122008-12-09 13:14:23 -08001936
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001937 chunk_nr = nr_pages;
1938 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1939 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1940
Arnd Bergmann5b1b5612021-09-08 15:18:17 -07001941 if (in_compat_syscall()) {
1942 if (get_compat_pages_array(chunk_pages, pages,
1943 chunk_nr))
1944 break;
1945 } else {
1946 if (copy_from_user(chunk_pages, pages,
1947 chunk_nr * sizeof(*chunk_pages)))
1948 break;
1949 }
Brice Goglin80bba122008-12-09 13:14:23 -08001950
1951 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1952
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001953 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1954 break;
Christoph Lameter742755a2006-06-23 02:03:55 -07001955
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001956 pages += chunk_nr;
1957 status += chunk_nr;
1958 nr_pages -= chunk_nr;
1959 }
1960 return nr_pages ? -EFAULT : 0;
Christoph Lameter742755a2006-06-23 02:03:55 -07001961}
1962
Miaohe Lin4dc200c2020-10-17 16:14:03 -07001963static struct mm_struct *find_mm_struct(pid_t pid, nodemask_t *mem_nodes)
1964{
1965 struct task_struct *task;
1966 struct mm_struct *mm;
1967
1968 /*
1969 * There is no need to check if current process has the right to modify
1970 * the specified process when they are same.
1971 */
1972 if (!pid) {
1973 mmget(current->mm);
1974 *mem_nodes = cpuset_mems_allowed(current);
1975 return current->mm;
1976 }
1977
1978 /* Find the mm_struct */
1979 rcu_read_lock();
1980 task = find_task_by_vpid(pid);
1981 if (!task) {
1982 rcu_read_unlock();
1983 return ERR_PTR(-ESRCH);
1984 }
1985 get_task_struct(task);
1986
1987 /*
1988 * Check if this process has the right to modify the specified
1989 * process. Use the regular "ptrace_may_access()" checks.
1990 */
1991 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
1992 rcu_read_unlock();
1993 mm = ERR_PTR(-EPERM);
1994 goto out;
1995 }
1996 rcu_read_unlock();
1997
1998 mm = ERR_PTR(security_task_movememory(task));
1999 if (IS_ERR(mm))
2000 goto out;
2001 *mem_nodes = cpuset_mems_allowed(task);
2002 mm = get_task_mm(task);
2003out:
2004 put_task_struct(task);
2005 if (!mm)
2006 mm = ERR_PTR(-EINVAL);
2007 return mm;
2008}
2009
Christoph Lameter742755a2006-06-23 02:03:55 -07002010/*
2011 * Move a list of pages in the address space of the currently executing
2012 * process.
2013 */
Dominik Brodowski7addf442018-03-17 16:08:03 +01002014static int kernel_move_pages(pid_t pid, unsigned long nr_pages,
2015 const void __user * __user *pages,
2016 const int __user *nodes,
2017 int __user *status, int flags)
Christoph Lameter742755a2006-06-23 02:03:55 -07002018{
Christoph Lameter742755a2006-06-23 02:03:55 -07002019 struct mm_struct *mm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07002020 int err;
Christoph Lameter3268c632012-03-21 16:34:06 -07002021 nodemask_t task_nodes;
Christoph Lameter742755a2006-06-23 02:03:55 -07002022
2023 /* Check flags */
2024 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
2025 return -EINVAL;
2026
2027 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
2028 return -EPERM;
2029
Miaohe Lin4dc200c2020-10-17 16:14:03 -07002030 mm = find_mm_struct(pid, &task_nodes);
2031 if (IS_ERR(mm))
2032 return PTR_ERR(mm);
Sasha Levin6e8b09e2012-04-25 16:01:53 -07002033
2034 if (nodes)
2035 err = do_pages_move(mm, task_nodes, nr_pages, pages,
2036 nodes, status, flags);
2037 else
2038 err = do_pages_stat(mm, nr_pages, pages, status);
Christoph Lameter3268c632012-03-21 16:34:06 -07002039
2040 mmput(mm);
2041 return err;
Christoph Lameter742755a2006-06-23 02:03:55 -07002042}
Christoph Lameter742755a2006-06-23 02:03:55 -07002043
Dominik Brodowski7addf442018-03-17 16:08:03 +01002044SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
2045 const void __user * __user *, pages,
2046 const int __user *, nodes,
2047 int __user *, status, int, flags)
2048{
2049 return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags);
2050}
2051
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002052#ifdef CONFIG_NUMA_BALANCING
2053/*
2054 * Returns true if this is a safe migration target node for misplaced NUMA
2055 * pages. Currently it only checks the watermarks which crude
2056 */
2057static bool migrate_balanced_pgdat(struct pglist_data *pgdat,
Mel Gorman3abef4e2013-02-22 16:34:27 -08002058 unsigned long nr_migrate_pages)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002059{
2060 int z;
Mel Gorman599d0c92016-07-28 15:45:31 -07002061
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002062 for (z = pgdat->nr_zones - 1; z >= 0; z--) {
2063 struct zone *zone = pgdat->node_zones + z;
2064
2065 if (!populated_zone(zone))
2066 continue;
2067
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002068 /* Avoid waking kswapd by allocating pages_to_migrate pages. */
2069 if (!zone_watermark_ok(zone, 0,
2070 high_wmark_pages(zone) +
2071 nr_migrate_pages,
Huang Yingbfe9d002019-11-30 17:57:28 -08002072 ZONE_MOVABLE, 0))
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002073 continue;
2074 return true;
2075 }
2076 return false;
2077}
2078
2079static struct page *alloc_misplaced_dst_page(struct page *page,
Michal Hocko666feb22018-04-10 16:30:03 -07002080 unsigned long data)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002081{
2082 int nid = (int) data;
2083 struct page *newpage;
2084
Vlastimil Babka96db8002015-09-08 15:03:50 -07002085 newpage = __alloc_pages_node(nid,
Johannes Weinere97ca8e52014-03-10 15:49:43 -07002086 (GFP_HIGHUSER_MOVABLE |
2087 __GFP_THISNODE | __GFP_NOMEMALLOC |
2088 __GFP_NORETRY | __GFP_NOWARN) &
Mel Gorman8479eba2016-02-26 15:19:31 -08002089 ~__GFP_RECLAIM, 0);
Hillf Dantonbac03822012-11-27 14:46:24 +00002090
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002091 return newpage;
2092}
2093
Yang Shic5b5a3d2021-06-30 18:51:42 -07002094static struct page *alloc_misplaced_dst_page_thp(struct page *page,
2095 unsigned long data)
2096{
2097 int nid = (int) data;
2098 struct page *newpage;
2099
2100 newpage = alloc_pages_node(nid, (GFP_TRANSHUGE_LIGHT | __GFP_THISNODE),
2101 HPAGE_PMD_ORDER);
2102 if (!newpage)
2103 goto out;
2104
2105 prep_transhuge_page(newpage);
2106
2107out:
2108 return newpage;
2109}
2110
Mel Gorman1c30e012014-01-21 15:50:58 -08002111static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
Mel Gormanb32967f2012-11-19 12:35:47 +00002112{
Hugh Dickins340ef392013-02-22 16:34:33 -08002113 int page_lru;
Baolin Wang2b9b6242021-09-08 15:18:01 -07002114 int nr_pages = thp_nr_pages(page);
Mel Gormanb32967f2012-11-19 12:35:47 +00002115
Sasha Levin309381fea2014-01-23 15:52:54 -08002116 VM_BUG_ON_PAGE(compound_order(page) && !PageTransHuge(page), page);
Mel Gorman3abef4e2013-02-22 16:34:27 -08002117
Yang Shi662aeea2021-06-30 18:51:51 -07002118 /* Do not migrate THP mapped by multiple processes */
2119 if (PageTransHuge(page) && total_mapcount(page) > 1)
2120 return 0;
2121
Mel Gormanb32967f2012-11-19 12:35:47 +00002122 /* Avoid migrating to a node that is nearly full */
Baolin Wang2b9b6242021-09-08 15:18:01 -07002123 if (!migrate_balanced_pgdat(pgdat, nr_pages))
Hugh Dickins340ef392013-02-22 16:34:33 -08002124 return 0;
Mel Gormanb32967f2012-11-19 12:35:47 +00002125
Hugh Dickins340ef392013-02-22 16:34:33 -08002126 if (isolate_lru_page(page))
2127 return 0;
Mel Gormanb32967f2012-11-19 12:35:47 +00002128
Huang Ying9de4f222020-04-06 20:04:41 -07002129 page_lru = page_is_file_lru(page);
Mel Gorman599d0c92016-07-28 15:45:31 -07002130 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_lru,
Baolin Wang2b9b6242021-09-08 15:18:01 -07002131 nr_pages);
Hugh Dickins340ef392013-02-22 16:34:33 -08002132
2133 /*
2134 * Isolating the page has taken another reference, so the
2135 * caller's reference can be safely dropped without the page
2136 * disappearing underneath us during migration.
Mel Gormanb32967f2012-11-19 12:35:47 +00002137 */
2138 put_page(page);
Hugh Dickins340ef392013-02-22 16:34:33 -08002139 return 1;
Mel Gormanb32967f2012-11-19 12:35:47 +00002140}
2141
Mel Gormana8f60772012-11-14 21:41:46 +00002142/*
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002143 * Attempt to migrate a misplaced page to the specified destination
2144 * node. Caller is expected to have an elevated reference count on
2145 * the page that will be dropped by this function before returning.
2146 */
Mel Gorman1bc115d2013-10-07 11:29:05 +01002147int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma,
2148 int node)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002149{
Mel Gormana8f60772012-11-14 21:41:46 +00002150 pg_data_t *pgdat = NODE_DATA(node);
Hugh Dickins340ef392013-02-22 16:34:33 -08002151 int isolated;
Mel Gormanb32967f2012-11-19 12:35:47 +00002152 int nr_remaining;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002153 LIST_HEAD(migratepages);
Yang Shic5b5a3d2021-06-30 18:51:42 -07002154 new_page_t *new;
2155 bool compound;
Aneesh Kumar K.Vb5916c02021-07-29 14:53:47 -07002156 int nr_pages = thp_nr_pages(page);
Yang Shic5b5a3d2021-06-30 18:51:42 -07002157
2158 /*
2159 * PTE mapped THP or HugeTLB page can't reach here so the page could
2160 * be either base page or THP. And it must be head page if it is
2161 * THP.
2162 */
2163 compound = PageTransHuge(page);
2164
2165 if (compound)
2166 new = alloc_misplaced_dst_page_thp;
2167 else
2168 new = alloc_misplaced_dst_page;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002169
2170 /*
Mel Gorman1bc115d2013-10-07 11:29:05 +01002171 * Don't migrate file pages that are mapped in multiple processes
2172 * with execute permissions as they are probably shared libraries.
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002173 */
Miaohe Lin7ee820e2021-05-04 18:37:16 -07002174 if (page_mapcount(page) != 1 && page_is_file_lru(page) &&
2175 (vma->vm_flags & VM_EXEC))
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002176 goto out;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002177
Mel Gormana8f60772012-11-14 21:41:46 +00002178 /*
Mel Gorman09a913a2018-04-10 16:29:20 -07002179 * Also do not migrate dirty pages as not all filesystems can move
2180 * dirty pages in MIGRATE_ASYNC mode which is a waste of cycles.
2181 */
Huang Ying9de4f222020-04-06 20:04:41 -07002182 if (page_is_file_lru(page) && PageDirty(page))
Mel Gorman09a913a2018-04-10 16:29:20 -07002183 goto out;
2184
Mel Gormanb32967f2012-11-19 12:35:47 +00002185 isolated = numamigrate_isolate_page(pgdat, page);
2186 if (!isolated)
2187 goto out;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002188
Mel Gormanb32967f2012-11-19 12:35:47 +00002189 list_add(&page->lru, &migratepages);
Yang Shic5b5a3d2021-06-30 18:51:42 -07002190 nr_remaining = migrate_pages(&migratepages, *new, NULL, node,
Yang Shi5ac95882021-09-02 14:59:13 -07002191 MIGRATE_ASYNC, MR_NUMA_MISPLACED, NULL);
Mel Gormanb32967f2012-11-19 12:35:47 +00002192 if (nr_remaining) {
Joonsoo Kim59c82b72014-01-21 15:51:17 -08002193 if (!list_empty(&migratepages)) {
2194 list_del(&page->lru);
Yang Shic5fc5c32021-06-30 18:51:45 -07002195 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
2196 page_is_file_lru(page), -nr_pages);
Joonsoo Kim59c82b72014-01-21 15:51:17 -08002197 putback_lru_page(page);
2198 }
Mel Gormanb32967f2012-11-19 12:35:47 +00002199 isolated = 0;
2200 } else
Yang Shic5fc5c32021-06-30 18:51:45 -07002201 count_vm_numa_events(NUMA_PAGE_MIGRATE, nr_pages);
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002202 BUG_ON(!list_empty(&migratepages));
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002203 return isolated;
Hugh Dickins340ef392013-02-22 16:34:33 -08002204
2205out:
2206 put_page(page);
2207 return 0;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002208}
Mel Gorman220018d2012-12-05 09:32:56 +00002209#endif /* CONFIG_NUMA_BALANCING */
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002210#endif /* CONFIG_NUMA */
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002211
Christoph Hellwig9b2ed9c2019-08-14 09:59:28 +02002212#ifdef CONFIG_DEVICE_PRIVATE
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002213static int migrate_vma_collect_skip(unsigned long start,
2214 unsigned long end,
2215 struct mm_walk *walk)
2216{
2217 struct migrate_vma *migrate = walk->private;
2218 unsigned long addr;
2219
Ralph Campbell872ea702020-01-30 22:14:38 -08002220 for (addr = start; addr < end; addr += PAGE_SIZE) {
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002221 migrate->dst[migrate->npages] = 0;
2222 migrate->src[migrate->npages++] = 0;
2223 }
2224
2225 return 0;
2226}
2227
Miaohe Lin843e1be2021-05-04 18:37:13 -07002228static int migrate_vma_collect_hole(unsigned long start,
2229 unsigned long end,
2230 __always_unused int depth,
2231 struct mm_walk *walk)
2232{
2233 struct migrate_vma *migrate = walk->private;
2234 unsigned long addr;
2235
2236 /* Only allow populating anonymous memory. */
2237 if (!vma_is_anonymous(walk->vma))
2238 return migrate_vma_collect_skip(start, end, walk);
2239
2240 for (addr = start; addr < end; addr += PAGE_SIZE) {
2241 migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE;
2242 migrate->dst[migrate->npages] = 0;
2243 migrate->npages++;
2244 migrate->cpages++;
2245 }
2246
2247 return 0;
2248}
2249
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002250static int migrate_vma_collect_pmd(pmd_t *pmdp,
2251 unsigned long start,
2252 unsigned long end,
2253 struct mm_walk *walk)
2254{
2255 struct migrate_vma *migrate = walk->private;
2256 struct vm_area_struct *vma = walk->vma;
2257 struct mm_struct *mm = vma->vm_mm;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002258 unsigned long addr = start, unmapped = 0;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002259 spinlock_t *ptl;
2260 pte_t *ptep;
2261
2262again:
2263 if (pmd_none(*pmdp))
Steven Priceb7a16c72020-02-03 17:36:03 -08002264 return migrate_vma_collect_hole(start, end, -1, walk);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002265
2266 if (pmd_trans_huge(*pmdp)) {
2267 struct page *page;
2268
2269 ptl = pmd_lock(mm, pmdp);
2270 if (unlikely(!pmd_trans_huge(*pmdp))) {
2271 spin_unlock(ptl);
2272 goto again;
2273 }
2274
2275 page = pmd_page(*pmdp);
2276 if (is_huge_zero_page(page)) {
2277 spin_unlock(ptl);
2278 split_huge_pmd(vma, pmdp, addr);
2279 if (pmd_trans_unstable(pmdp))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002280 return migrate_vma_collect_skip(start, end,
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002281 walk);
2282 } else {
2283 int ret;
2284
2285 get_page(page);
2286 spin_unlock(ptl);
2287 if (unlikely(!trylock_page(page)))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002288 return migrate_vma_collect_skip(start, end,
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002289 walk);
2290 ret = split_huge_page(page);
2291 unlock_page(page);
2292 put_page(page);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002293 if (ret)
2294 return migrate_vma_collect_skip(start, end,
2295 walk);
2296 if (pmd_none(*pmdp))
Steven Priceb7a16c72020-02-03 17:36:03 -08002297 return migrate_vma_collect_hole(start, end, -1,
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002298 walk);
2299 }
2300 }
2301
2302 if (unlikely(pmd_bad(*pmdp)))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002303 return migrate_vma_collect_skip(start, end, walk);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002304
2305 ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002306 arch_enter_lazy_mmu_mode();
2307
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002308 for (; addr < end; addr += PAGE_SIZE, ptep++) {
Christoph Hellwig800bb1c2020-03-16 20:32:14 +01002309 unsigned long mpfn = 0, pfn;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002310 struct page *page;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002311 swp_entry_t entry;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002312 pte_t pte;
2313
2314 pte = *ptep;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002315
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002316 if (pte_none(pte)) {
Ralph Campbell0744f282020-08-11 18:31:41 -07002317 if (vma_is_anonymous(vma)) {
2318 mpfn = MIGRATE_PFN_MIGRATE;
2319 migrate->cpages++;
2320 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002321 goto next;
2322 }
2323
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002324 if (!pte_present(pte)) {
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002325 /*
2326 * Only care about unaddressable device page special
2327 * page table entry. Other special swap entries are not
2328 * migratable, and we ignore regular swapped page.
2329 */
2330 entry = pte_to_swp_entry(pte);
2331 if (!is_device_private_entry(entry))
2332 goto next;
2333
Alistair Poppleaf5cdaf2021-06-30 18:54:06 -07002334 page = pfn_swap_entry_to_page(entry);
Ralph Campbell51431922020-07-23 15:30:00 -07002335 if (!(migrate->flags &
2336 MIGRATE_VMA_SELECT_DEVICE_PRIVATE) ||
2337 page->pgmap->owner != migrate->pgmap_owner)
Christoph Hellwig800bb1c2020-03-16 20:32:14 +01002338 goto next;
2339
Christoph Hellwig06d462b2019-08-14 09:59:27 +02002340 mpfn = migrate_pfn(page_to_pfn(page)) |
2341 MIGRATE_PFN_MIGRATE;
Alistair Popple4dd845b2021-06-30 18:54:09 -07002342 if (is_writable_device_private_entry(entry))
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002343 mpfn |= MIGRATE_PFN_WRITE;
2344 } else {
Ralph Campbell51431922020-07-23 15:30:00 -07002345 if (!(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM))
Christoph Hellwig800bb1c2020-03-16 20:32:14 +01002346 goto next;
Pingfan Liu276f7562019-09-23 15:37:38 -07002347 pfn = pte_pfn(pte);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002348 if (is_zero_pfn(pfn)) {
2349 mpfn = MIGRATE_PFN_MIGRATE;
2350 migrate->cpages++;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002351 goto next;
2352 }
Christoph Hellwig25b29952019-06-13 22:50:49 +02002353 page = vm_normal_page(migrate->vma, addr, pte);
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002354 mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
2355 mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0;
2356 }
2357
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002358 /* FIXME support THP */
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002359 if (!page || !page->mapping || PageTransCompound(page)) {
Pingfan Liu276f7562019-09-23 15:37:38 -07002360 mpfn = 0;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002361 goto next;
2362 }
2363
2364 /*
2365 * By getting a reference on the page we pin it and that blocks
2366 * any kind of migration. Side effect is that it "freezes" the
2367 * pte.
2368 *
2369 * We drop this reference after isolating the page from the lru
2370 * for non device page (device page are not on the lru and thus
2371 * can't be dropped from it).
2372 */
2373 get_page(page);
2374 migrate->cpages++;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002375
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002376 /*
2377 * Optimize for the common case where page is only mapped once
2378 * in one process. If we can lock the page, then we can safely
2379 * set up a special migration page table entry now.
2380 */
2381 if (trylock_page(page)) {
2382 pte_t swp_pte;
2383
2384 mpfn |= MIGRATE_PFN_LOCKED;
2385 ptep_get_and_clear(mm, addr, ptep);
2386
2387 /* Setup special migration page table entry */
Alistair Popple4dd845b2021-06-30 18:54:09 -07002388 if (mpfn & MIGRATE_PFN_WRITE)
2389 entry = make_writable_migration_entry(
2390 page_to_pfn(page));
2391 else
2392 entry = make_readable_migration_entry(
2393 page_to_pfn(page));
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002394 swp_pte = swp_entry_to_pte(entry);
Alistair Popplead7df762020-09-04 16:36:01 -07002395 if (pte_present(pte)) {
2396 if (pte_soft_dirty(pte))
2397 swp_pte = pte_swp_mksoft_dirty(swp_pte);
2398 if (pte_uffd_wp(pte))
2399 swp_pte = pte_swp_mkuffd_wp(swp_pte);
2400 } else {
2401 if (pte_swp_soft_dirty(pte))
2402 swp_pte = pte_swp_mksoft_dirty(swp_pte);
2403 if (pte_swp_uffd_wp(pte))
2404 swp_pte = pte_swp_mkuffd_wp(swp_pte);
2405 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002406 set_pte_at(mm, addr, ptep, swp_pte);
2407
2408 /*
2409 * This is like regular unmap: we remove the rmap and
2410 * drop page refcount. Page won't be freed, as we took
2411 * a reference just above.
2412 */
2413 page_remove_rmap(page, false);
2414 put_page(page);
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002415
2416 if (pte_present(pte))
2417 unmapped++;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002418 }
2419
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002420next:
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002421 migrate->dst[migrate->npages] = 0;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002422 migrate->src[migrate->npages++] = mpfn;
2423 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002424 arch_leave_lazy_mmu_mode();
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002425 pte_unmap_unlock(ptep - 1, ptl);
2426
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002427 /* Only flush the TLB if we actually modified any entries */
2428 if (unmapped)
2429 flush_tlb_range(walk->vma, start, end);
2430
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002431 return 0;
2432}
2433
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02002434static const struct mm_walk_ops migrate_vma_walk_ops = {
2435 .pmd_entry = migrate_vma_collect_pmd,
2436 .pte_hole = migrate_vma_collect_hole,
2437};
2438
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002439/*
2440 * migrate_vma_collect() - collect pages over a range of virtual addresses
2441 * @migrate: migrate struct containing all migration information
2442 *
2443 * This will walk the CPU page table. For each virtual address backed by a
2444 * valid page, it updates the src array and takes a reference on the page, in
2445 * order to pin the page until we lock it and unmap it.
2446 */
2447static void migrate_vma_collect(struct migrate_vma *migrate)
2448{
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002449 struct mmu_notifier_range range;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002450
Ralph Campbell998427b2020-07-23 15:30:01 -07002451 /*
2452 * Note that the pgmap_owner is passed to the mmu notifier callback so
2453 * that the registered device driver can skip invalidating device
2454 * private page mappings that won't be migrated.
2455 */
Alistair Popple6b49bf62021-06-30 18:54:19 -07002456 mmu_notifier_range_init_owner(&range, MMU_NOTIFY_MIGRATE, 0,
2457 migrate->vma, migrate->vma->vm_mm, migrate->start, migrate->end,
Ralph Campbellc1a06df2020-08-06 23:17:09 -07002458 migrate->pgmap_owner);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002459 mmu_notifier_invalidate_range_start(&range);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002460
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02002461 walk_page_range(migrate->vma->vm_mm, migrate->start, migrate->end,
2462 &migrate_vma_walk_ops, migrate);
2463
2464 mmu_notifier_invalidate_range_end(&range);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002465 migrate->end = migrate->start + (migrate->npages << PAGE_SHIFT);
2466}
2467
2468/*
2469 * migrate_vma_check_page() - check if page is pinned or not
2470 * @page: struct page to check
2471 *
2472 * Pinned pages cannot be migrated. This is the same test as in
2473 * migrate_page_move_mapping(), except that here we allow migration of a
2474 * ZONE_DEVICE page.
2475 */
2476static bool migrate_vma_check_page(struct page *page)
2477{
2478 /*
2479 * One extra ref because caller holds an extra reference, either from
2480 * isolate_lru_page() for a regular page, or migrate_vma_collect() for
2481 * a device page.
2482 */
2483 int extra = 1;
2484
2485 /*
2486 * FIXME support THP (transparent huge page), it is bit more complex to
2487 * check them than regular pages, because they can be mapped with a pmd
2488 * or with a pte (split pte mapping).
2489 */
2490 if (PageCompound(page))
2491 return false;
2492
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002493 /* Page from ZONE_DEVICE have one extra reference */
2494 if (is_zone_device_page(page)) {
2495 /*
2496 * Private page can never be pin as they have no valid pte and
2497 * GUP will fail for those. Yet if there is a pending migration
2498 * a thread might try to wait on the pte migration entry and
2499 * will bump the page reference count. Sadly there is no way to
2500 * differentiate a regular pin from migration wait. Hence to
2501 * avoid 2 racing thread trying to migrate back to CPU to enter
Haitao Shi8958b242020-12-15 20:47:26 -08002502 * infinite loop (one stopping migration because the other is
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002503 * waiting on pte migration entry). We always return true here.
2504 *
2505 * FIXME proper solution is to rework migration_entry_wait() so
2506 * it does not need to take a reference on page.
2507 */
Christoph Hellwig25b29952019-06-13 22:50:49 +02002508 return is_device_private_page(page);
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002509 }
2510
Jérôme Glissedf6ad692017-09-08 16:12:24 -07002511 /* For file back page */
2512 if (page_mapping(page))
2513 extra += 1 + page_has_private(page);
2514
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002515 if ((page_count(page) - extra) > page_mapcount(page))
2516 return false;
2517
2518 return true;
2519}
2520
2521/*
2522 * migrate_vma_prepare() - lock pages and isolate them from the lru
2523 * @migrate: migrate struct containing all migration information
2524 *
2525 * This locks pages that have been collected by migrate_vma_collect(). Once each
2526 * page is locked it is isolated from the lru (for non-device pages). Finally,
2527 * the ref taken by migrate_vma_collect() is dropped, as locked pages cannot be
2528 * migrated by concurrent kernel threads.
2529 */
2530static void migrate_vma_prepare(struct migrate_vma *migrate)
2531{
2532 const unsigned long npages = migrate->npages;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002533 const unsigned long start = migrate->start;
2534 unsigned long addr, i, restore = 0;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002535 bool allow_drain = true;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002536
2537 lru_add_drain();
2538
2539 for (i = 0; (i < npages) && migrate->cpages; i++) {
2540 struct page *page = migrate_pfn_to_page(migrate->src[i]);
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002541 bool remap = true;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002542
2543 if (!page)
2544 continue;
2545
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002546 if (!(migrate->src[i] & MIGRATE_PFN_LOCKED)) {
2547 /*
2548 * Because we are migrating several pages there can be
2549 * a deadlock between 2 concurrent migration where each
2550 * are waiting on each other page lock.
2551 *
2552 * Make migrate_vma() a best effort thing and backoff
2553 * for any page we can not lock right away.
2554 */
2555 if (!trylock_page(page)) {
2556 migrate->src[i] = 0;
2557 migrate->cpages--;
2558 put_page(page);
2559 continue;
2560 }
2561 remap = false;
2562 migrate->src[i] |= MIGRATE_PFN_LOCKED;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002563 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002564
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002565 /* ZONE_DEVICE pages are not on LRU */
2566 if (!is_zone_device_page(page)) {
2567 if (!PageLRU(page) && allow_drain) {
2568 /* Drain CPU's pagevec */
2569 lru_add_drain_all();
2570 allow_drain = false;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002571 }
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002572
2573 if (isolate_lru_page(page)) {
2574 if (remap) {
2575 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2576 migrate->cpages--;
2577 restore++;
2578 } else {
2579 migrate->src[i] = 0;
2580 unlock_page(page);
2581 migrate->cpages--;
2582 put_page(page);
2583 }
2584 continue;
2585 }
2586
2587 /* Drop the reference we took in collect */
2588 put_page(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002589 }
2590
2591 if (!migrate_vma_check_page(page)) {
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002592 if (remap) {
2593 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2594 migrate->cpages--;
2595 restore++;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002596
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002597 if (!is_zone_device_page(page)) {
2598 get_page(page);
2599 putback_lru_page(page);
2600 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002601 } else {
2602 migrate->src[i] = 0;
2603 unlock_page(page);
2604 migrate->cpages--;
2605
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002606 if (!is_zone_device_page(page))
2607 putback_lru_page(page);
2608 else
2609 put_page(page);
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002610 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002611 }
2612 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002613
2614 for (i = 0, addr = start; i < npages && restore; i++, addr += PAGE_SIZE) {
2615 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2616
2617 if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE))
2618 continue;
2619
2620 remove_migration_pte(page, migrate->vma, addr, page);
2621
2622 migrate->src[i] = 0;
2623 unlock_page(page);
2624 put_page(page);
2625 restore--;
2626 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002627}
2628
2629/*
2630 * migrate_vma_unmap() - replace page mapping with special migration pte entry
2631 * @migrate: migrate struct containing all migration information
2632 *
2633 * Replace page mapping (CPU page table pte) with a special migration pte entry
2634 * and check again if it has been pinned. Pinned pages are restored because we
2635 * cannot migrate them.
2636 *
2637 * This is the last step before we call the device driver callback to allocate
2638 * destination memory and copy contents of original page over to new page.
2639 */
2640static void migrate_vma_unmap(struct migrate_vma *migrate)
2641{
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002642 const unsigned long npages = migrate->npages;
2643 const unsigned long start = migrate->start;
2644 unsigned long addr, i, restore = 0;
2645
2646 for (i = 0; i < npages; i++) {
2647 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2648
2649 if (!page || !(migrate->src[i] & MIGRATE_PFN_MIGRATE))
2650 continue;
2651
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002652 if (page_mapped(page)) {
Alistair Popplea98a2f02021-06-30 18:54:16 -07002653 try_to_migrate(page, 0);
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002654 if (page_mapped(page))
2655 goto restore;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002656 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002657
2658 if (migrate_vma_check_page(page))
2659 continue;
2660
2661restore:
2662 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2663 migrate->cpages--;
2664 restore++;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002665 }
2666
2667 for (addr = start, i = 0; i < npages && restore; addr += PAGE_SIZE, i++) {
2668 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2669
2670 if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE))
2671 continue;
2672
2673 remove_migration_ptes(page, page, false);
2674
2675 migrate->src[i] = 0;
2676 unlock_page(page);
2677 restore--;
2678
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002679 if (is_zone_device_page(page))
2680 put_page(page);
2681 else
2682 putback_lru_page(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002683 }
2684}
2685
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002686/**
2687 * migrate_vma_setup() - prepare to migrate a range of memory
Randy Dunlapeaf444d2020-08-11 18:33:08 -07002688 * @args: contains the vma, start, and pfns arrays for the migration
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002689 *
2690 * Returns: negative errno on failures, 0 when 0 or more pages were migrated
2691 * without an error.
2692 *
2693 * Prepare to migrate a range of memory virtual address range by collecting all
2694 * the pages backing each virtual address in the range, saving them inside the
2695 * src array. Then lock those pages and unmap them. Once the pages are locked
2696 * and unmapped, check whether each page is pinned or not. Pages that aren't
2697 * pinned have the MIGRATE_PFN_MIGRATE flag set (by this function) in the
2698 * corresponding src array entry. Then restores any pages that are pinned, by
2699 * remapping and unlocking those pages.
2700 *
2701 * The caller should then allocate destination memory and copy source memory to
2702 * it for all those entries (ie with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE
2703 * flag set). Once these are allocated and copied, the caller must update each
2704 * corresponding entry in the dst array with the pfn value of the destination
2705 * page and with the MIGRATE_PFN_VALID and MIGRATE_PFN_LOCKED flags set
2706 * (destination pages must have their struct pages locked, via lock_page()).
2707 *
2708 * Note that the caller does not have to migrate all the pages that are marked
2709 * with MIGRATE_PFN_MIGRATE flag in src array unless this is a migration from
2710 * device memory to system memory. If the caller cannot migrate a device page
2711 * back to system memory, then it must return VM_FAULT_SIGBUS, which has severe
2712 * consequences for the userspace process, so it must be avoided if at all
2713 * possible.
2714 *
2715 * For empty entries inside CPU page table (pte_none() or pmd_none() is true) we
2716 * do set MIGRATE_PFN_MIGRATE flag inside the corresponding source array thus
Ingo Molnarf0953a12021-05-06 18:06:47 -07002717 * allowing the caller to allocate device memory for those unbacked virtual
2718 * addresses. For this the caller simply has to allocate device memory and
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002719 * properly set the destination entry like for regular migration. Note that
Ingo Molnarf0953a12021-05-06 18:06:47 -07002720 * this can still fail, and thus inside the device driver you must check if the
2721 * migration was successful for those entries after calling migrate_vma_pages(),
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002722 * just like for regular migration.
2723 *
2724 * After that, the callers must call migrate_vma_pages() to go over each entry
2725 * in the src array that has the MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag
2726 * set. If the corresponding entry in dst array has MIGRATE_PFN_VALID flag set,
2727 * then migrate_vma_pages() to migrate struct page information from the source
2728 * struct page to the destination struct page. If it fails to migrate the
2729 * struct page information, then it clears the MIGRATE_PFN_MIGRATE flag in the
2730 * src array.
2731 *
2732 * At this point all successfully migrated pages have an entry in the src
2733 * array with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag set and the dst
2734 * array entry with MIGRATE_PFN_VALID flag set.
2735 *
2736 * Once migrate_vma_pages() returns the caller may inspect which pages were
2737 * successfully migrated, and which were not. Successfully migrated pages will
2738 * have the MIGRATE_PFN_MIGRATE flag set for their src array entry.
2739 *
2740 * It is safe to update device page table after migrate_vma_pages() because
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002741 * both destination and source page are still locked, and the mmap_lock is held
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002742 * in read mode (hence no one can unmap the range being migrated).
2743 *
2744 * Once the caller is done cleaning up things and updating its page table (if it
2745 * chose to do so, this is not an obligation) it finally calls
2746 * migrate_vma_finalize() to update the CPU page table to point to new pages
2747 * for successfully migrated pages or otherwise restore the CPU page table to
2748 * point to the original source pages.
2749 */
2750int migrate_vma_setup(struct migrate_vma *args)
2751{
2752 long nr_pages = (args->end - args->start) >> PAGE_SHIFT;
2753
2754 args->start &= PAGE_MASK;
2755 args->end &= PAGE_MASK;
2756 if (!args->vma || is_vm_hugetlb_page(args->vma) ||
2757 (args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma))
2758 return -EINVAL;
2759 if (nr_pages <= 0)
2760 return -EINVAL;
2761 if (args->start < args->vma->vm_start ||
2762 args->start >= args->vma->vm_end)
2763 return -EINVAL;
2764 if (args->end <= args->vma->vm_start || args->end > args->vma->vm_end)
2765 return -EINVAL;
2766 if (!args->src || !args->dst)
2767 return -EINVAL;
2768
2769 memset(args->src, 0, sizeof(*args->src) * nr_pages);
2770 args->cpages = 0;
2771 args->npages = 0;
2772
2773 migrate_vma_collect(args);
2774
2775 if (args->cpages)
2776 migrate_vma_prepare(args);
2777 if (args->cpages)
2778 migrate_vma_unmap(args);
2779
2780 /*
2781 * At this point pages are locked and unmapped, and thus they have
2782 * stable content and can safely be copied to destination memory that
2783 * is allocated by the drivers.
2784 */
2785 return 0;
2786
2787}
2788EXPORT_SYMBOL(migrate_vma_setup);
2789
Ralph Campbell34290e22020-01-30 22:14:44 -08002790/*
2791 * This code closely matches the code in:
2792 * __handle_mm_fault()
2793 * handle_pte_fault()
2794 * do_anonymous_page()
2795 * to map in an anonymous zero page but the struct page will be a ZONE_DEVICE
2796 * private page.
2797 */
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002798static void migrate_vma_insert_page(struct migrate_vma *migrate,
2799 unsigned long addr,
2800 struct page *page,
Stephen Zhangd85c6db2020-12-14 19:13:20 -08002801 unsigned long *src)
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002802{
2803 struct vm_area_struct *vma = migrate->vma;
2804 struct mm_struct *mm = vma->vm_mm;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002805 bool flush = false;
2806 spinlock_t *ptl;
2807 pte_t entry;
2808 pgd_t *pgdp;
2809 p4d_t *p4dp;
2810 pud_t *pudp;
2811 pmd_t *pmdp;
2812 pte_t *ptep;
2813
2814 /* Only allow populating anonymous memory */
2815 if (!vma_is_anonymous(vma))
2816 goto abort;
2817
2818 pgdp = pgd_offset(mm, addr);
2819 p4dp = p4d_alloc(mm, pgdp, addr);
2820 if (!p4dp)
2821 goto abort;
2822 pudp = pud_alloc(mm, p4dp, addr);
2823 if (!pudp)
2824 goto abort;
2825 pmdp = pmd_alloc(mm, pudp, addr);
2826 if (!pmdp)
2827 goto abort;
2828
2829 if (pmd_trans_huge(*pmdp) || pmd_devmap(*pmdp))
2830 goto abort;
2831
2832 /*
2833 * Use pte_alloc() instead of pte_alloc_map(). We can't run
2834 * pte_offset_map() on pmds where a huge pmd might be created
2835 * from a different thread.
2836 *
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002837 * pte_alloc_map() is safe to use under mmap_write_lock(mm) or when
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002838 * parallel threads are excluded by other means.
2839 *
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002840 * Here we only have mmap_read_lock(mm).
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002841 */
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -08002842 if (pte_alloc(mm, pmdp))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002843 goto abort;
2844
2845 /* See the comment in pte_alloc_one_map() */
2846 if (unlikely(pmd_trans_unstable(pmdp)))
2847 goto abort;
2848
2849 if (unlikely(anon_vma_prepare(vma)))
2850 goto abort;
Johannes Weinerd9eb1ea2020-06-03 16:02:24 -07002851 if (mem_cgroup_charge(page, vma->vm_mm, GFP_KERNEL))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002852 goto abort;
2853
2854 /*
2855 * The memory barrier inside __SetPageUptodate makes sure that
2856 * preceding stores to the page contents become visible before
2857 * the set_pte_at() write.
2858 */
2859 __SetPageUptodate(page);
2860
Jérôme Glissedf6ad692017-09-08 16:12:24 -07002861 if (is_zone_device_page(page)) {
2862 if (is_device_private_page(page)) {
2863 swp_entry_t swp_entry;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002864
Alistair Popple4dd845b2021-06-30 18:54:09 -07002865 if (vma->vm_flags & VM_WRITE)
2866 swp_entry = make_writable_device_private_entry(
2867 page_to_pfn(page));
2868 else
2869 swp_entry = make_readable_device_private_entry(
2870 page_to_pfn(page));
Jérôme Glissedf6ad692017-09-08 16:12:24 -07002871 entry = swp_entry_to_pte(swp_entry);
Miaohe Lin34f5e9b2021-05-04 18:37:10 -07002872 } else {
2873 /*
2874 * For now we only support migrating to un-addressable
2875 * device memory.
2876 */
2877 pr_warn_once("Unsupported ZONE_DEVICE page type.\n");
2878 goto abort;
Jérôme Glissedf6ad692017-09-08 16:12:24 -07002879 }
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002880 } else {
2881 entry = mk_pte(page, vma->vm_page_prot);
2882 if (vma->vm_flags & VM_WRITE)
2883 entry = pte_mkwrite(pte_mkdirty(entry));
2884 }
2885
2886 ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
2887
Ralph Campbell34290e22020-01-30 22:14:44 -08002888 if (check_stable_address_space(mm))
2889 goto unlock_abort;
2890
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002891 if (pte_present(*ptep)) {
2892 unsigned long pfn = pte_pfn(*ptep);
2893
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002894 if (!is_zero_pfn(pfn))
2895 goto unlock_abort;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002896 flush = true;
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002897 } else if (!pte_none(*ptep))
2898 goto unlock_abort;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002899
2900 /*
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002901 * Check for userfaultfd but do not deliver the fault. Instead,
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002902 * just back off.
2903 */
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002904 if (userfaultfd_missing(vma))
2905 goto unlock_abort;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002906
2907 inc_mm_counter(mm, MM_ANONPAGES);
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07002908 page_add_new_anon_rmap(page, vma, addr, false);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002909 if (!is_zone_device_page(page))
Joonsoo Kimb5181542020-08-11 18:30:40 -07002910 lru_cache_add_inactive_or_unevictable(page, vma);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002911 get_page(page);
2912
2913 if (flush) {
2914 flush_cache_page(vma, addr, pte_pfn(*ptep));
2915 ptep_clear_flush_notify(vma, addr, ptep);
2916 set_pte_at_notify(mm, addr, ptep, entry);
2917 update_mmu_cache(vma, addr, ptep);
2918 } else {
2919 /* No need to invalidate - it was non-present before */
2920 set_pte_at(mm, addr, ptep, entry);
2921 update_mmu_cache(vma, addr, ptep);
2922 }
2923
2924 pte_unmap_unlock(ptep, ptl);
2925 *src = MIGRATE_PFN_MIGRATE;
2926 return;
2927
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002928unlock_abort:
2929 pte_unmap_unlock(ptep, ptl);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002930abort:
2931 *src &= ~MIGRATE_PFN_MIGRATE;
2932}
2933
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002934/**
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002935 * migrate_vma_pages() - migrate meta-data from src page to dst page
2936 * @migrate: migrate struct containing all migration information
2937 *
2938 * This migrates struct page meta-data from source struct page to destination
2939 * struct page. This effectively finishes the migration from source page to the
2940 * destination page.
2941 */
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002942void migrate_vma_pages(struct migrate_vma *migrate)
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002943{
2944 const unsigned long npages = migrate->npages;
2945 const unsigned long start = migrate->start;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002946 struct mmu_notifier_range range;
2947 unsigned long addr, i;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002948 bool notified = false;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002949
2950 for (i = 0, addr = start; i < npages; addr += PAGE_SIZE, i++) {
2951 struct page *newpage = migrate_pfn_to_page(migrate->dst[i]);
2952 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2953 struct address_space *mapping;
2954 int r;
2955
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002956 if (!newpage) {
2957 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002958 continue;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002959 }
2960
2961 if (!page) {
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002962 if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002963 continue;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002964 if (!notified) {
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002965 notified = true;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002966
Alistair Popple6b49bf62021-06-30 18:54:19 -07002967 mmu_notifier_range_init_owner(&range,
2968 MMU_NOTIFY_MIGRATE, 0, migrate->vma,
2969 migrate->vma->vm_mm, addr, migrate->end,
Ralph Campbell5e5dda82020-12-14 19:12:55 -08002970 migrate->pgmap_owner);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002971 mmu_notifier_invalidate_range_start(&range);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002972 }
2973 migrate_vma_insert_page(migrate, addr, newpage,
Stephen Zhangd85c6db2020-12-14 19:13:20 -08002974 &migrate->src[i]);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002975 continue;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002976 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002977
2978 mapping = page_mapping(page);
2979
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002980 if (is_zone_device_page(newpage)) {
2981 if (is_device_private_page(newpage)) {
2982 /*
2983 * For now only support private anonymous when
2984 * migrating to un-addressable device memory.
2985 */
2986 if (mapping) {
2987 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2988 continue;
2989 }
Christoph Hellwig25b29952019-06-13 22:50:49 +02002990 } else {
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002991 /*
2992 * Other types of ZONE_DEVICE page are not
2993 * supported.
2994 */
2995 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2996 continue;
2997 }
2998 }
2999
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003000 r = migrate_page(mapping, newpage, page, MIGRATE_SYNC_NO_COPY);
3001 if (r != MIGRATEPAGE_SUCCESS)
3002 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
3003 }
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003004
Jérôme Glisse4645b9f2017-11-15 17:34:11 -08003005 /*
3006 * No need to double call mmu_notifier->invalidate_range() callback as
3007 * the above ptep_clear_flush_notify() inside migrate_vma_insert_page()
3008 * did already call it.
3009 */
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003010 if (notified)
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003011 mmu_notifier_invalidate_range_only_end(&range);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003012}
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003013EXPORT_SYMBOL(migrate_vma_pages);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003014
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003015/**
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003016 * migrate_vma_finalize() - restore CPU page table entry
3017 * @migrate: migrate struct containing all migration information
3018 *
3019 * This replaces the special migration pte entry with either a mapping to the
3020 * new page if migration was successful for that page, or to the original page
3021 * otherwise.
3022 *
3023 * This also unlocks the pages and puts them back on the lru, or drops the extra
3024 * refcount, for device pages.
3025 */
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003026void migrate_vma_finalize(struct migrate_vma *migrate)
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003027{
3028 const unsigned long npages = migrate->npages;
3029 unsigned long i;
3030
3031 for (i = 0; i < npages; i++) {
3032 struct page *newpage = migrate_pfn_to_page(migrate->dst[i]);
3033 struct page *page = migrate_pfn_to_page(migrate->src[i]);
3034
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003035 if (!page) {
3036 if (newpage) {
3037 unlock_page(newpage);
3038 put_page(newpage);
3039 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003040 continue;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003041 }
3042
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003043 if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE) || !newpage) {
3044 if (newpage) {
3045 unlock_page(newpage);
3046 put_page(newpage);
3047 }
3048 newpage = page;
3049 }
3050
3051 remove_migration_ptes(page, newpage, false);
3052 unlock_page(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003053
Jérôme Glissea5430dd2017-09-08 16:12:17 -07003054 if (is_zone_device_page(page))
3055 put_page(page);
3056 else
3057 putback_lru_page(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003058
3059 if (newpage != page) {
3060 unlock_page(newpage);
Jérôme Glissea5430dd2017-09-08 16:12:17 -07003061 if (is_zone_device_page(newpage))
3062 put_page(newpage);
3063 else
3064 putback_lru_page(newpage);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003065 }
3066 }
3067}
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003068EXPORT_SYMBOL(migrate_vma_finalize);
Christoph Hellwig9b2ed9c2019-08-14 09:59:28 +02003069#endif /* CONFIG_DEVICE_PRIVATE */
Dave Hansen79c28a42021-09-02 14:59:06 -07003070
Dave Hansen76af6a02021-10-18 15:15:32 -07003071#if defined(CONFIG_HOTPLUG_CPU)
Dave Hansen79c28a42021-09-02 14:59:06 -07003072/* Disable reclaim-based migration. */
3073static void __disable_all_migrate_targets(void)
3074{
3075 int node;
3076
3077 for_each_online_node(node)
3078 node_demotion[node] = NUMA_NO_NODE;
3079}
3080
3081static void disable_all_migrate_targets(void)
3082{
3083 __disable_all_migrate_targets();
3084
3085 /*
3086 * Ensure that the "disable" is visible across the system.
3087 * Readers will see either a combination of before+disable
3088 * state or disable+after. They will never see before and
3089 * after state together.
3090 *
3091 * The before+after state together might have cycles and
3092 * could cause readers to do things like loop until this
3093 * function finishes. This ensures they can only see a
3094 * single "bad" read and would, for instance, only loop
3095 * once.
3096 */
3097 synchronize_rcu();
3098}
3099
3100/*
3101 * Find an automatic demotion target for 'node'.
3102 * Failing here is OK. It might just indicate
3103 * being at the end of a chain.
3104 */
3105static int establish_migrate_target(int node, nodemask_t *used)
3106{
3107 int migration_target;
3108
3109 /*
3110 * Can not set a migration target on a
3111 * node with it already set.
3112 *
3113 * No need for READ_ONCE() here since this
3114 * in the write path for node_demotion[].
3115 * This should be the only thread writing.
3116 */
3117 if (node_demotion[node] != NUMA_NO_NODE)
3118 return NUMA_NO_NODE;
3119
3120 migration_target = find_next_best_node(node, used);
3121 if (migration_target == NUMA_NO_NODE)
3122 return NUMA_NO_NODE;
3123
3124 node_demotion[node] = migration_target;
3125
3126 return migration_target;
3127}
3128
3129/*
3130 * When memory fills up on a node, memory contents can be
3131 * automatically migrated to another node instead of
3132 * discarded at reclaim.
3133 *
3134 * Establish a "migration path" which will start at nodes
3135 * with CPUs and will follow the priorities used to build the
3136 * page allocator zonelists.
3137 *
3138 * The difference here is that cycles must be avoided. If
3139 * node0 migrates to node1, then neither node1, nor anything
3140 * node1 migrates to can migrate to node0.
3141 *
3142 * This function can run simultaneously with readers of
3143 * node_demotion[]. However, it can not run simultaneously
3144 * with itself. Exclusion is provided by memory hotplug events
3145 * being single-threaded.
3146 */
3147static void __set_migration_target_nodes(void)
3148{
3149 nodemask_t next_pass = NODE_MASK_NONE;
3150 nodemask_t this_pass = NODE_MASK_NONE;
3151 nodemask_t used_targets = NODE_MASK_NONE;
3152 int node;
3153
3154 /*
3155 * Avoid any oddities like cycles that could occur
3156 * from changes in the topology. This will leave
3157 * a momentary gap when migration is disabled.
3158 */
3159 disable_all_migrate_targets();
3160
3161 /*
3162 * Allocations go close to CPUs, first. Assume that
3163 * the migration path starts at the nodes with CPUs.
3164 */
3165 next_pass = node_states[N_CPU];
3166again:
3167 this_pass = next_pass;
3168 next_pass = NODE_MASK_NONE;
3169 /*
3170 * To avoid cycles in the migration "graph", ensure
3171 * that migration sources are not future targets by
3172 * setting them in 'used_targets'. Do this only
3173 * once per pass so that multiple source nodes can
3174 * share a target node.
3175 *
3176 * 'used_targets' will become unavailable in future
3177 * passes. This limits some opportunities for
3178 * multiple source nodes to share a destination.
3179 */
3180 nodes_or(used_targets, used_targets, this_pass);
3181 for_each_node_mask(node, this_pass) {
3182 int target_node = establish_migrate_target(node, &used_targets);
3183
3184 if (target_node == NUMA_NO_NODE)
3185 continue;
3186
3187 /*
3188 * Visit targets from this pass in the next pass.
3189 * Eventually, every node will have been part of
3190 * a pass, and will become set in 'used_targets'.
3191 */
3192 node_set(target_node, next_pass);
3193 }
3194 /*
3195 * 'next_pass' contains nodes which became migration
3196 * targets in this pass. Make additional passes until
3197 * no more migrations targets are available.
3198 */
3199 if (!nodes_empty(next_pass))
3200 goto again;
3201}
3202
3203/*
3204 * For callers that do not hold get_online_mems() already.
3205 */
Dave Hansen79c28a42021-09-02 14:59:06 -07003206static void set_migration_target_nodes(void)
3207{
3208 get_online_mems();
3209 __set_migration_target_nodes();
3210 put_online_mems();
3211}
Dave Hansen884a6e52021-09-02 14:59:09 -07003212
3213/*
Dave Hansen884a6e52021-09-02 14:59:09 -07003214 * This leaves migrate-on-reclaim transiently disabled between
3215 * the MEM_GOING_OFFLINE and MEM_OFFLINE events. This runs
3216 * whether reclaim-based migration is enabled or not, which
3217 * ensures that the user can turn reclaim-based migration at
3218 * any time without needing to recalculate migration targets.
3219 *
3220 * These callbacks already hold get_online_mems(). That is why
3221 * __set_migration_target_nodes() can be used as opposed to
3222 * set_migration_target_nodes().
3223 */
3224static int __meminit migrate_on_reclaim_callback(struct notifier_block *self,
Dave Hansen295be912021-10-18 15:15:29 -07003225 unsigned long action, void *_arg)
Dave Hansen884a6e52021-09-02 14:59:09 -07003226{
Dave Hansen295be912021-10-18 15:15:29 -07003227 struct memory_notify *arg = _arg;
3228
3229 /*
3230 * Only update the node migration order when a node is
3231 * changing status, like online->offline. This avoids
3232 * the overhead of synchronize_rcu() in most cases.
3233 */
3234 if (arg->status_change_nid < 0)
3235 return notifier_from_errno(0);
3236
Dave Hansen884a6e52021-09-02 14:59:09 -07003237 switch (action) {
3238 case MEM_GOING_OFFLINE:
3239 /*
3240 * Make sure there are not transient states where
3241 * an offline node is a migration target. This
3242 * will leave migration disabled until the offline
3243 * completes and the MEM_OFFLINE case below runs.
3244 */
3245 disable_all_migrate_targets();
3246 break;
3247 case MEM_OFFLINE:
3248 case MEM_ONLINE:
3249 /*
3250 * Recalculate the target nodes once the node
3251 * reaches its final state (online or offline).
3252 */
3253 __set_migration_target_nodes();
3254 break;
3255 case MEM_CANCEL_OFFLINE:
3256 /*
3257 * MEM_GOING_OFFLINE disabled all the migration
3258 * targets. Reenable them.
3259 */
3260 __set_migration_target_nodes();
3261 break;
3262 case MEM_GOING_ONLINE:
3263 case MEM_CANCEL_ONLINE:
3264 break;
3265 }
3266
3267 return notifier_from_errno(0);
3268}
3269
Dave Hansen76af6a02021-10-18 15:15:32 -07003270/*
3271 * React to hotplug events that might affect the migration targets
3272 * like events that online or offline NUMA nodes.
3273 *
3274 * The ordering is also currently dependent on which nodes have
3275 * CPUs. That means we need CPU on/offline notification too.
3276 */
3277static int migration_online_cpu(unsigned int cpu)
3278{
3279 set_migration_target_nodes();
3280 return 0;
3281}
3282
3283static int migration_offline_cpu(unsigned int cpu)
3284{
3285 set_migration_target_nodes();
3286 return 0;
3287}
3288
Dave Hansen884a6e52021-09-02 14:59:09 -07003289static int __init migrate_on_reclaim_init(void)
3290{
3291 int ret;
3292
Huang Yinga6a02512021-10-18 15:15:35 -07003293 ret = cpuhp_setup_state_nocalls(CPUHP_MM_DEMOTION_DEAD, "mm/demotion:offline",
3294 NULL, migration_offline_cpu);
Dave Hansen884a6e52021-09-02 14:59:09 -07003295 /*
3296 * In the unlikely case that this fails, the automatic
3297 * migration targets may become suboptimal for nodes
3298 * where N_CPU changes. With such a small impact in a
3299 * rare case, do not bother trying to do anything special.
3300 */
3301 WARN_ON(ret < 0);
Huang Yinga6a02512021-10-18 15:15:35 -07003302 ret = cpuhp_setup_state(CPUHP_AP_MM_DEMOTION_ONLINE, "mm/demotion:online",
3303 migration_online_cpu, NULL);
3304 WARN_ON(ret < 0);
Dave Hansen884a6e52021-09-02 14:59:09 -07003305
3306 hotplug_memory_notifier(migrate_on_reclaim_callback, 100);
3307 return 0;
3308}
3309late_initcall(migrate_on_reclaim_init);
Dave Hansen76af6a02021-10-18 15:15:32 -07003310#endif /* CONFIG_HOTPLUG_CPU */