blob: 076beb8479e5389ba7b3b4da08e0352e49c1c6b9 [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));
andrew.yang0d8a8362022-03-15 16:58:34 +1100109 SetPageIsolated(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700110 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);
andrew.yang0d8a8362022-03-15 16:58:34 +1100128 ClearPageIsolated(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700129}
130
Christoph Lameterb20a3502006-03-22 00:09:12 -0800131/*
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800132 * Put previously isolated pages back onto the appropriate lists
133 * from where they were once taken off for compaction/migration.
134 *
Joonsoo Kim59c82b72014-01-21 15:51:17 -0800135 * This function shall be used whenever the isolated pageset has been
136 * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range()
Miaohe Lin072e7412022-05-30 19:30:15 +0800137 * and isolate_hugetlb().
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800138 */
139void putback_movable_pages(struct list_head *l)
140{
141 struct page *page;
142 struct page *page2;
143
144 list_for_each_entry_safe(page, page2, l, lru) {
Naoya Horiguchi31caf662013-09-11 14:21:59 -0700145 if (unlikely(PageHuge(page))) {
146 putback_active_hugepage(page);
147 continue;
148 }
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800149 list_del(&page->lru);
Minchan Kimbda807d2016-07-26 15:23:05 -0700150 /*
151 * We isolated non-lru movable page so here we can use
152 * __PageMovable because LRU page's mapping cannot have
153 * PAGE_MAPPING_MOVABLE.
154 */
Minchan Kimb1123ea62016-07-26 15:23:09 -0700155 if (unlikely(__PageMovable(page))) {
Minchan Kimbda807d2016-07-26 15:23:05 -0700156 VM_BUG_ON_PAGE(!PageIsolated(page), page);
157 lock_page(page);
158 if (PageMovable(page))
159 putback_movable_page(page);
160 else
andrew.yang0d8a8362022-03-15 16:58:34 +1100161 ClearPageIsolated(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700162 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;
andrew.yang0d8a8362022-03-15 16:58:34 +1100919 ClearPageIsolated(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700920 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 */
andrew.yang0d8a8362022-03-15 16:58:34 +1100941 ClearPageIsolated(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700942 }
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
Muchun Song97a9f802022-03-22 14:41:56 -0700952 if (likely(!is_zone_device_page(newpage))) {
953 int i, nr = compound_nr(newpage);
Lars Perssond2b2c6dd2019-03-28 20:44:28 -0700954
Muchun Song97a9f802022-03-22 14:41:56 -0700955 for (i = 0; i < nr; i++)
956 flush_dcache_page(newpage + i);
957 }
Mel Gorman3fe20112010-05-24 14:32:20 -0700958 }
Minchan Kimbda807d2016-07-26 15:23:05 -0700959out:
Christoph Lametere24f0b82006-06-23 02:03:51 -0700960 return rc;
961}
962
Minchan Kim0dabec92011-10-31 17:06:57 -0700963static int __unmap_and_move(struct page *page, struct page *newpage,
Hugh Dickins9c620e22013-02-22 16:35:14 -0800964 int force, enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700965{
Minchan Kim0dabec92011-10-31 17:06:57 -0700966 int rc = -EAGAIN;
Baolin Wang213ecb32021-09-08 15:18:06 -0700967 bool page_was_mapped = false;
Mel Gorman3f6c8272010-05-24 14:32:17 -0700968 struct anon_vma *anon_vma = NULL;
Minchan Kimbda807d2016-07-26 15:23:05 -0700969 bool is_lru = !__PageMovable(page);
Christoph Lameter95a402c2006-06-23 02:03:53 -0700970
Nick Piggin529ae9a2008-08-02 12:01:03 +0200971 if (!trylock_page(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800972 if (!force || mode == MIGRATE_ASYNC)
Minchan Kim0dabec92011-10-31 17:06:57 -0700973 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800974
975 /*
976 * It's not safe for direct compaction to call lock_page.
977 * For example, during page readahead pages are added locked
978 * to the LRU. Later, when the IO completes the pages are
979 * marked uptodate and unlocked. However, the queueing
980 * could be merging multiple pages for one bio (e.g.
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -0700981 * mpage_readahead). If an allocation happens for the
Mel Gorman3e7d3442011-01-13 15:45:56 -0800982 * second or third page, the process can end up locking
983 * the same page twice and deadlocking. Rather than
984 * trying to be clever about what pages can be locked,
985 * avoid the use of lock_page for direct compaction
986 * altogether.
987 */
988 if (current->flags & PF_MEMALLOC)
Minchan Kim0dabec92011-10-31 17:06:57 -0700989 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800990
Christoph Lametere24f0b82006-06-23 02:03:51 -0700991 lock_page(page);
992 }
993
994 if (PageWriteback(page)) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700995 /*
Jianguo Wufed5b642013-04-29 15:07:58 -0700996 * Only in the case of a full synchronous migration is it
Mel Gormana6bc32b2012-01-12 17:19:43 -0800997 * necessary to wait for PageWriteback. In the async case,
998 * the retry loop is too short and in the sync-light case,
999 * the overhead of stalling is too much
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -07001000 */
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07001001 switch (mode) {
1002 case MIGRATE_SYNC:
1003 case MIGRATE_SYNC_NO_COPY:
1004 break;
1005 default:
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -07001006 rc = -EBUSY;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07001007 goto out_unlock;
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -07001008 }
1009 if (!force)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07001010 goto out_unlock;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001011 wait_on_page_writeback(page);
1012 }
Hugh Dickins03f15c82015-11-05 18:49:56 -08001013
Christoph Lametere24f0b82006-06-23 02:03:51 -07001014 /*
Baolin Wang68a98432021-09-08 15:18:03 -07001015 * By try_to_migrate(), page->mapcount goes down to 0 here. In this case,
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001016 * we cannot notice that anon_vma is freed while we migrates a page.
Hugh Dickins1ce82b62011-01-13 15:47:30 -08001017 * This get_anon_vma() delays freeing anon_vma pointer until the end
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001018 * of migration. File cache pages are no problem because of page_lock()
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -07001019 * File Caches may use write_page() or lock_page() in migration, then,
1020 * just care Anon page here.
Hugh Dickins03f15c82015-11-05 18:49:56 -08001021 *
1022 * Only page_get_anon_vma() understands the subtleties of
1023 * getting a hold on an anon_vma from outside one of its mms.
1024 * But if we cannot get anon_vma, then we won't need it anyway,
1025 * because that implies that the anon page is no longer mapped
1026 * (and cannot be remapped so long as we hold the page lock).
Christoph Lametere24f0b82006-06-23 02:03:51 -07001027 */
Hugh Dickins03f15c82015-11-05 18:49:56 -08001028 if (PageAnon(page) && !PageKsm(page))
Peter Zijlstra746b18d2011-05-24 17:12:10 -07001029 anon_vma = page_get_anon_vma(page);
Shaohua Li62e1c552008-02-04 22:29:33 -08001030
Hugh Dickins7db76712015-11-05 18:49:49 -08001031 /*
1032 * Block others from accessing the new page when we get around to
1033 * establishing additional references. We are usually the only one
1034 * holding a reference to newpage at this point. We used to have a BUG
1035 * here if trylock_page(newpage) fails, but would like to allow for
1036 * cases where there might be a race with the previous use of newpage.
1037 * This is much like races on refcount of oldpage: just don't BUG().
1038 */
1039 if (unlikely(!trylock_page(newpage)))
1040 goto out_unlock;
1041
Minchan Kimbda807d2016-07-26 15:23:05 -07001042 if (unlikely(!is_lru)) {
1043 rc = move_to_new_page(newpage, page, mode);
1044 goto out_unlock_both;
1045 }
1046
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001047 /*
Shaohua Li62e1c552008-02-04 22:29:33 -08001048 * Corner case handling:
1049 * 1. When a new swap-cache page is read into, it is added to the LRU
1050 * and treated as swapcache but it has no rmap yet.
1051 * Calling try_to_unmap() against a page->mapping==NULL page will
1052 * trigger a BUG. So handle it here.
Yang Shid12b8952020-12-14 19:13:02 -08001053 * 2. An orphaned page (see truncate_cleanup_page) might have
Shaohua Li62e1c552008-02-04 22:29:33 -08001054 * fs-private metadata. The page can be picked up due to memory
1055 * offlining. Everywhere else except page reclaim, the page is
1056 * invisible to the vm, so the page can not be migrated. So try to
1057 * free the metadata, so the page can be freed.
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001058 */
Shaohua Li62e1c552008-02-04 22:29:33 -08001059 if (!page->mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -08001060 VM_BUG_ON_PAGE(PageAnon(page), page);
Hugh Dickins1ce82b62011-01-13 15:47:30 -08001061 if (page_has_private(page)) {
Shaohua Li62e1c552008-02-04 22:29:33 -08001062 try_to_free_buffers(page);
Hugh Dickins7db76712015-11-05 18:49:49 -08001063 goto out_unlock_both;
Shaohua Li62e1c552008-02-04 22:29:33 -08001064 }
Hugh Dickins7db76712015-11-05 18:49:49 -08001065 } else if (page_mapped(page)) {
1066 /* Establish migration ptes */
Hugh Dickins03f15c82015-11-05 18:49:56 -08001067 VM_BUG_ON_PAGE(PageAnon(page) && !PageKsm(page) && !anon_vma,
1068 page);
Alistair Popplea98a2f02021-06-30 18:54:16 -07001069 try_to_migrate(page, 0);
Baolin Wang213ecb32021-09-08 15:18:06 -07001070 page_was_mapped = true;
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001071 }
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001072
Christoph Lametere6a15302006-06-25 05:46:49 -07001073 if (!page_mapped(page))
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001074 rc = move_to_new_page(newpage, page, mode);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001075
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001076 if (page_was_mapped)
1077 remove_migration_ptes(page,
Kirill A. Shutemove3884662016-03-17 14:20:07 -07001078 rc == MIGRATEPAGE_SUCCESS ? newpage : page, false);
Mel Gorman3f6c8272010-05-24 14:32:17 -07001079
Hugh Dickins7db76712015-11-05 18:49:49 -08001080out_unlock_both:
1081 unlock_page(newpage);
1082out_unlock:
Mel Gorman3f6c8272010-05-24 14:32:17 -07001083 /* Drop an anon_vma reference if we took one */
Rik van Riel76545062010-08-09 17:18:41 -07001084 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -07001085 put_anon_vma(anon_vma);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001086 unlock_page(page);
Minchan Kim0dabec92011-10-31 17:06:57 -07001087out:
Minchan Kimc6c919e2016-07-26 15:23:02 -07001088 /*
1089 * If migration is successful, decrease refcount of the newpage
1090 * which will not free the page because new page owner increased
1091 * refcounter. As well, if it is LRU page, add the page to LRU
David Hildenbrande0a352f2019-02-01 14:21:19 -08001092 * list in here. Use the old state of the isolated source page to
1093 * determine if we migrated a LRU page. newpage was already unlocked
1094 * and possibly modified by its owner - don't rely on the page
1095 * state.
Minchan Kimc6c919e2016-07-26 15:23:02 -07001096 */
1097 if (rc == MIGRATEPAGE_SUCCESS) {
David Hildenbrande0a352f2019-02-01 14:21:19 -08001098 if (unlikely(!is_lru))
Minchan Kimc6c919e2016-07-26 15:23:02 -07001099 put_page(newpage);
1100 else
1101 putback_lru_page(newpage);
1102 }
1103
Minchan Kim0dabec92011-10-31 17:06:57 -07001104 return rc;
1105}
Christoph Lameter95a402c2006-06-23 02:03:53 -07001106
Dave Hansen79c28a42021-09-02 14:59:06 -07001107
1108/*
1109 * node_demotion[] example:
1110 *
1111 * Consider a system with two sockets. Each socket has
1112 * three classes of memory attached: fast, medium and slow.
1113 * Each memory class is placed in its own NUMA node. The
1114 * CPUs are placed in the node with the "fast" memory. The
1115 * 6 NUMA nodes (0-5) might be split among the sockets like
1116 * this:
1117 *
1118 * Socket A: 0, 1, 2
1119 * Socket B: 3, 4, 5
1120 *
1121 * When Node 0 fills up, its memory should be migrated to
1122 * Node 1. When Node 1 fills up, it should be migrated to
1123 * Node 2. The migration path start on the nodes with the
1124 * processors (since allocations default to this node) and
1125 * fast memory, progress through medium and end with the
1126 * slow memory:
1127 *
1128 * 0 -> 1 -> 2 -> stop
1129 * 3 -> 4 -> 5 -> stop
1130 *
1131 * This is represented in the node_demotion[] like this:
1132 *
1133 * { 1, // Node 0 migrates to 1
1134 * 2, // Node 1 migrates to 2
1135 * -1, // Node 2 does not migrate
1136 * 4, // Node 3 migrates to 4
1137 * 5, // Node 4 migrates to 5
1138 * -1} // Node 5 does not migrate
1139 */
1140
1141/*
1142 * Writes to this array occur without locking. Cycles are
1143 * not allowed: Node X demotes to Y which demotes to X...
1144 *
1145 * If multiple reads are performed, a single rcu_read_lock()
1146 * must be held over all reads to ensure that no cycles are
1147 * observed.
1148 */
1149static int node_demotion[MAX_NUMNODES] __read_mostly =
1150 {[0 ... MAX_NUMNODES - 1] = NUMA_NO_NODE};
1151
1152/**
1153 * next_demotion_node() - Get the next node in the demotion path
1154 * @node: The starting node to lookup the next node
1155 *
Randy Dunlapc9bd7d12021-09-02 15:00:36 -07001156 * Return: node id for next memory node in the demotion path hierarchy
Dave Hansen79c28a42021-09-02 14:59:06 -07001157 * from @node; NUMA_NO_NODE if @node is terminal. This does not keep
1158 * @node online or guarantee that it *continues* to be the next demotion
1159 * target.
1160 */
1161int next_demotion_node(int node)
1162{
1163 int target;
1164
1165 /*
1166 * node_demotion[] is updated without excluding this
1167 * function from running. RCU doesn't provide any
1168 * compiler barriers, so the READ_ONCE() is required
1169 * to avoid compiler reordering or read merging.
1170 *
1171 * Make sure to use RCU over entire code blocks if
1172 * node_demotion[] reads need to be consistent.
1173 */
1174 rcu_read_lock();
1175 target = READ_ONCE(node_demotion[node]);
1176 rcu_read_unlock();
1177
1178 return target;
1179}
1180
Minchan Kim0dabec92011-10-31 17:06:57 -07001181/*
1182 * Obtain the lock on page, remove all ptes and migrate the page
1183 * to the newly allocated page in newpage.
1184 */
Linus Torvalds6ec44762020-07-08 10:48:35 -07001185static int unmap_and_move(new_page_t get_new_page,
Geert Uytterhoevenef2a5152015-04-14 15:44:22 -07001186 free_page_t put_new_page,
1187 unsigned long private, struct page *page,
Naoya Horiguchiadd05ce2015-06-24 16:56:50 -07001188 int force, enum migrate_mode mode,
Yang Shidd4ae782020-12-14 19:13:06 -08001189 enum migrate_reason reason,
1190 struct list_head *ret)
Minchan Kim0dabec92011-10-31 17:06:57 -07001191{
Hugh Dickins2def7422015-11-05 18:49:46 -08001192 int rc = MIGRATEPAGE_SUCCESS;
Yang Shi74d4a572019-11-30 17:57:12 -08001193 struct page *newpage = NULL;
Minchan Kim0dabec92011-10-31 17:06:57 -07001194
Michal Hocko94723aa2018-04-10 16:30:07 -07001195 if (!thp_migration_supported() && PageTransHuge(page))
Yang Shid532e2e2020-12-14 19:13:16 -08001196 return -ENOSYS;
Michal Hocko94723aa2018-04-10 16:30:07 -07001197
Minchan Kim0dabec92011-10-31 17:06:57 -07001198 if (page_count(page) == 1) {
1199 /* page was freed from under us. So we are done. */
Minchan Kimc6c919e2016-07-26 15:23:02 -07001200 ClearPageActive(page);
1201 ClearPageUnevictable(page);
Minchan Kimbda807d2016-07-26 15:23:05 -07001202 if (unlikely(__PageMovable(page))) {
1203 lock_page(page);
1204 if (!PageMovable(page))
andrew.yang0d8a8362022-03-15 16:58:34 +11001205 ClearPageIsolated(page);
Minchan Kimbda807d2016-07-26 15:23:05 -07001206 unlock_page(page);
1207 }
Minchan Kim0dabec92011-10-31 17:06:57 -07001208 goto out;
1209 }
1210
Yang Shi74d4a572019-11-30 17:57:12 -08001211 newpage = get_new_page(page, private);
1212 if (!newpage)
1213 return -ENOMEM;
1214
Hugh Dickins9c620e22013-02-22 16:35:14 -08001215 rc = __unmap_and_move(page, newpage, force, mode);
Minchan Kimc6c919e2016-07-26 15:23:02 -07001216 if (rc == MIGRATEPAGE_SUCCESS)
Vlastimil Babka7cd12b42016-03-15 14:56:18 -07001217 set_page_owner_migrate_reason(newpage, reason);
Rafael Aquinibf6bddf12012-12-11 16:02:42 -08001218
Minchan Kim0dabec92011-10-31 17:06:57 -07001219out:
Christoph Lametere24f0b82006-06-23 02:03:51 -07001220 if (rc != -EAGAIN) {
Minchan Kim0dabec92011-10-31 17:06:57 -07001221 /*
1222 * A page that has been migrated has all references
1223 * removed and will be freed. A page that has not been
Ralph Campbellc23a0c92020-01-30 22:14:41 -08001224 * migrated will have kept its references and be restored.
Minchan Kim0dabec92011-10-31 17:06:57 -07001225 */
1226 list_del(&page->lru);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001227 }
David Rientjes68711a72014-06-04 16:08:25 -07001228
Christoph Lameter95a402c2006-06-23 02:03:53 -07001229 /*
Minchan Kimc6c919e2016-07-26 15:23:02 -07001230 * If migration is successful, releases reference grabbed during
1231 * isolation. Otherwise, restore the page to right list unless
1232 * we want to retry.
Christoph Lameter95a402c2006-06-23 02:03:53 -07001233 */
Minchan Kimc6c919e2016-07-26 15:23:02 -07001234 if (rc == MIGRATEPAGE_SUCCESS) {
Yang Shidd4ae782020-12-14 19:13:06 -08001235 /*
1236 * Compaction can migrate also non-LRU pages which are
1237 * not accounted to NR_ISOLATED_*. They can be recognized
1238 * as __PageMovable
1239 */
1240 if (likely(!__PageMovable(page)))
1241 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
1242 page_is_file_lru(page), -thp_nr_pages(page));
1243
Oscar Salvador79f5f8f2020-10-15 20:07:09 -07001244 if (reason != MR_MEMORY_FAILURE)
Minchan Kimc6c919e2016-07-26 15:23:02 -07001245 /*
Oscar Salvador79f5f8f2020-10-15 20:07:09 -07001246 * We release the page in page_handle_poison.
Minchan Kimc6c919e2016-07-26 15:23:02 -07001247 */
Oscar Salvador79f5f8f2020-10-15 20:07:09 -07001248 put_page(page);
Minchan Kimc6c919e2016-07-26 15:23:02 -07001249 } else {
Yang Shidd4ae782020-12-14 19:13:06 -08001250 if (rc != -EAGAIN)
1251 list_add_tail(&page->lru, ret);
Minchan Kimbda807d2016-07-26 15:23:05 -07001252
Minchan Kimc6c919e2016-07-26 15:23:02 -07001253 if (put_new_page)
1254 put_new_page(newpage, private);
1255 else
1256 put_page(newpage);
1257 }
David Rientjes68711a72014-06-04 16:08:25 -07001258
Christoph Lametere24f0b82006-06-23 02:03:51 -07001259 return rc;
1260}
1261
1262/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001263 * Counterpart of unmap_and_move_page() for hugepage migration.
1264 *
1265 * This function doesn't wait the completion of hugepage I/O
1266 * because there is no race between I/O and migration for hugepage.
1267 * Note that currently hugepage I/O occurs only in direct I/O
1268 * where no lock is held and PG_writeback is irrelevant,
1269 * and writeback status of all subpages are counted in the reference
1270 * count of the head page (i.e. if all subpages of a 2MB hugepage are
1271 * under direct I/O, the reference of the head page is 512 and a bit more.)
1272 * This means that when we try to migrate hugepage whose subpages are
1273 * doing direct I/O, some references remain after try_to_unmap() and
1274 * hugepage migration fails without data corruption.
1275 *
1276 * There is also no race when direct I/O is issued on the page under migration,
1277 * because then pte is replaced with migration swap entry and direct I/O code
1278 * will wait in the page fault for migration to complete.
1279 */
1280static int unmap_and_move_huge_page(new_page_t get_new_page,
David Rientjes68711a72014-06-04 16:08:25 -07001281 free_page_t put_new_page, unsigned long private,
1282 struct page *hpage, int force,
Yang Shidd4ae782020-12-14 19:13:06 -08001283 enum migrate_mode mode, int reason,
1284 struct list_head *ret)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001285{
Hugh Dickins2def7422015-11-05 18:49:46 -08001286 int rc = -EAGAIN;
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001287 int page_was_mapped = 0;
Joonsoo Kim32665f22014-01-21 15:51:15 -08001288 struct page *new_hpage;
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001289 struct anon_vma *anon_vma = NULL;
Mike Kravetzc0d03812020-04-01 21:11:05 -07001290 struct address_space *mapping = NULL;
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001291
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001292 /*
Anshuman Khandual7ed2c312019-03-05 15:43:44 -08001293 * Migratability of hugepages depends on architectures and their size.
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001294 * This check is necessary because some callers of hugepage migration
1295 * like soft offline and memory hotremove don't walk through page
1296 * tables or check whether the hugepage is pmd-based or not before
1297 * kicking migration.
1298 */
Naoya Horiguchi100873d2014-06-04 16:10:56 -07001299 if (!hugepage_migration_supported(page_hstate(hpage))) {
Yang Shidd4ae782020-12-14 19:13:06 -08001300 list_move_tail(&hpage->lru, ret);
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001301 return -ENOSYS;
Joonsoo Kim32665f22014-01-21 15:51:15 -08001302 }
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001303
Muchun Song71a64f62021-02-04 18:32:17 -08001304 if (page_count(hpage) == 1) {
1305 /* page was freed from under us. So we are done. */
1306 putback_active_hugepage(hpage);
1307 return MIGRATEPAGE_SUCCESS;
1308 }
1309
Michal Hocko666feb22018-04-10 16:30:03 -07001310 new_hpage = get_new_page(hpage, private);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001311 if (!new_hpage)
1312 return -ENOMEM;
1313
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001314 if (!trylock_page(hpage)) {
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07001315 if (!force)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001316 goto out;
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07001317 switch (mode) {
1318 case MIGRATE_SYNC:
1319 case MIGRATE_SYNC_NO_COPY:
1320 break;
1321 default:
1322 goto out;
1323 }
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001324 lock_page(hpage);
1325 }
1326
Mike Kravetzcb6acd02019-02-28 16:22:02 -08001327 /*
1328 * Check for pages which are in the process of being freed. Without
1329 * page_mapping() set, hugetlbfs specific move page routine will not
1330 * be called and we could leak usage counts for subpools.
1331 */
Muchun Song6acfb5b2021-06-30 18:51:29 -07001332 if (hugetlb_page_subpool(hpage) && !page_mapping(hpage)) {
Mike Kravetzcb6acd02019-02-28 16:22:02 -08001333 rc = -EBUSY;
1334 goto out_unlock;
1335 }
1336
Peter Zijlstra746b18d2011-05-24 17:12:10 -07001337 if (PageAnon(hpage))
1338 anon_vma = page_get_anon_vma(hpage);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001339
Hugh Dickins7db76712015-11-05 18:49:49 -08001340 if (unlikely(!trylock_page(new_hpage)))
1341 goto put_anon;
1342
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001343 if (page_mapped(hpage)) {
Mike Kravetz336bf302020-11-13 22:52:16 -08001344 bool mapping_locked = false;
Alistair Popplea98a2f02021-06-30 18:54:16 -07001345 enum ttu_flags ttu = 0;
Mike Kravetzc0d03812020-04-01 21:11:05 -07001346
Mike Kravetz336bf302020-11-13 22:52:16 -08001347 if (!PageAnon(hpage)) {
1348 /*
1349 * In shared mappings, try_to_unmap could potentially
1350 * call huge_pmd_unshare. Because of this, take
1351 * semaphore in write mode here and set TTU_RMAP_LOCKED
1352 * to let lower levels know we have taken the lock.
1353 */
1354 mapping = hugetlb_page_mapping_lock_write(hpage);
1355 if (unlikely(!mapping))
1356 goto unlock_put_anon;
1357
1358 mapping_locked = true;
1359 ttu |= TTU_RMAP_LOCKED;
1360 }
1361
Alistair Popplea98a2f02021-06-30 18:54:16 -07001362 try_to_migrate(hpage, ttu);
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001363 page_was_mapped = 1;
Mike Kravetz336bf302020-11-13 22:52:16 -08001364
1365 if (mapping_locked)
1366 i_mmap_unlock_write(mapping);
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001367 }
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001368
1369 if (!page_mapped(hpage))
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001370 rc = move_to_new_page(new_hpage, hpage, mode);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001371
Mike Kravetz336bf302020-11-13 22:52:16 -08001372 if (page_was_mapped)
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001373 remove_migration_ptes(hpage,
Mike Kravetz336bf302020-11-13 22:52:16 -08001374 rc == MIGRATEPAGE_SUCCESS ? new_hpage : hpage, false);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001375
Mike Kravetzc0d03812020-04-01 21:11:05 -07001376unlock_put_anon:
Hugh Dickins7db76712015-11-05 18:49:49 -08001377 unlock_page(new_hpage);
1378
1379put_anon:
Hugh Dickinsfd4a4662011-01-13 15:47:31 -08001380 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -07001381 put_anon_vma(anon_vma);
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -07001382
Hugh Dickins2def7422015-11-05 18:49:46 -08001383 if (rc == MIGRATEPAGE_SUCCESS) {
Michal Hockoab5ac902018-01-31 16:20:48 -08001384 move_hugetlb_state(hpage, new_hpage, reason);
Hugh Dickins2def7422015-11-05 18:49:46 -08001385 put_new_page = NULL;
1386 }
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -07001387
Mike Kravetzcb6acd02019-02-28 16:22:02 -08001388out_unlock:
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001389 unlock_page(hpage);
Hillf Danton09761332011-12-08 14:34:20 -08001390out:
Yang Shidd4ae782020-12-14 19:13:06 -08001391 if (rc == MIGRATEPAGE_SUCCESS)
Naoya Horiguchib8ec1ce2013-09-11 14:22:01 -07001392 putback_active_hugepage(hpage);
Miaohe Lina04840c2021-05-04 18:37:07 -07001393 else if (rc != -EAGAIN)
Yang Shidd4ae782020-12-14 19:13:06 -08001394 list_move_tail(&hpage->lru, ret);
David Rientjes68711a72014-06-04 16:08:25 -07001395
1396 /*
1397 * If migration was not successful and there's a freeing callback, use
1398 * it. Otherwise, put_page() will drop the reference grabbed during
1399 * isolation.
1400 */
Hugh Dickins2def7422015-11-05 18:49:46 -08001401 if (put_new_page)
David Rientjes68711a72014-06-04 16:08:25 -07001402 put_new_page(new_hpage, private);
1403 else
Naoya Horiguchi3aaa76e2015-09-22 14:59:14 -07001404 putback_active_hugepage(new_hpage);
David Rientjes68711a72014-06-04 16:08:25 -07001405
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001406 return rc;
1407}
1408
Yang Shid532e2e2020-12-14 19:13:16 -08001409static inline int try_split_thp(struct page *page, struct page **page2,
1410 struct list_head *from)
1411{
1412 int rc = 0;
1413
1414 lock_page(page);
1415 rc = split_huge_page_to_list(page, from);
1416 unlock_page(page);
1417 if (!rc)
1418 list_safe_reset_next(page, *page2, lru);
1419
1420 return rc;
1421}
1422
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001423/*
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001424 * migrate_pages - migrate the pages specified in a list, to the free pages
1425 * supplied as the target for the page migration
Christoph Lameterb20a3502006-03-22 00:09:12 -08001426 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001427 * @from: The list of pages to be migrated.
1428 * @get_new_page: The function used to allocate free pages to be used
1429 * as the target of the page migration.
David Rientjes68711a72014-06-04 16:08:25 -07001430 * @put_new_page: The function used to free target pages if migration
1431 * fails, or NULL if no special handling is necessary.
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001432 * @private: Private data to be passed on to get_new_page()
1433 * @mode: The migration mode that specifies the constraints for
1434 * page migration, if any.
1435 * @reason: The reason for page migration.
Yang Shi5ac95882021-09-02 14:59:13 -07001436 * @ret_succeeded: Set to the number of pages migrated successfully if
1437 * the caller passes a non-NULL pointer.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001438 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001439 * The function returns after 10 attempts or if no pages are movable any more
1440 * because the list has become empty or no retryable pages exist any more.
Yang Shidd4ae782020-12-14 19:13:06 -08001441 * It is caller's responsibility to call putback_movable_pages() to return pages
1442 * to the LRU or free list only if ret != 0.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001443 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001444 * Returns the number of pages that were not migrated, or an error code.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001445 */
Hugh Dickins9c620e22013-02-22 16:35:14 -08001446int migrate_pages(struct list_head *from, new_page_t get_new_page,
David Rientjes68711a72014-06-04 16:08:25 -07001447 free_page_t put_new_page, unsigned long private,
Yang Shi5ac95882021-09-02 14:59:13 -07001448 enum migrate_mode mode, int reason, unsigned int *ret_succeeded)
Christoph Lameterb20a3502006-03-22 00:09:12 -08001449{
Christoph Lametere24f0b82006-06-23 02:03:51 -07001450 int retry = 1;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001451 int thp_retry = 1;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001452 int nr_failed = 0;
Mel Gorman5647bc22012-10-19 10:46:20 +01001453 int nr_succeeded = 0;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001454 int nr_thp_succeeded = 0;
1455 int nr_thp_failed = 0;
1456 int nr_thp_split = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001457 int pass = 0;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001458 bool is_thp = false;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001459 struct page *page;
1460 struct page *page2;
1461 int swapwrite = current->flags & PF_SWAPWRITE;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001462 int rc, nr_subpages;
Yang Shidd4ae782020-12-14 19:13:06 -08001463 LIST_HEAD(ret_pages);
Yang Shib0b515b2021-06-30 18:51:48 -07001464 bool nosplit = (reason == MR_NUMA_MISPLACED);
Christoph Lameterb20a3502006-03-22 00:09:12 -08001465
Liam Mark7bc1aec2021-05-04 18:37:25 -07001466 trace_mm_migrate_pages_start(mode, reason);
1467
Christoph Lameterb20a3502006-03-22 00:09:12 -08001468 if (!swapwrite)
1469 current->flags |= PF_SWAPWRITE;
1470
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001471 for (pass = 0; pass < 10 && (retry || thp_retry); pass++) {
Christoph Lametere24f0b82006-06-23 02:03:51 -07001472 retry = 0;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001473 thp_retry = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001474
Christoph Lametere24f0b82006-06-23 02:03:51 -07001475 list_for_each_entry_safe(page, page2, from, lru) {
Michal Hocko94723aa2018-04-10 16:30:07 -07001476retry:
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001477 /*
1478 * THP statistics is based on the source huge page.
1479 * Capture required information that might get lost
1480 * during migration.
1481 */
Zi Yan6c5c7b92020-09-25 21:19:14 -07001482 is_thp = PageTransHuge(page) && !PageHuge(page);
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -07001483 nr_subpages = thp_nr_pages(page);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001484 cond_resched();
Christoph Lameterb20a3502006-03-22 00:09:12 -08001485
Naoya Horiguchi31caf662013-09-11 14:21:59 -07001486 if (PageHuge(page))
1487 rc = unmap_and_move_huge_page(get_new_page,
David Rientjes68711a72014-06-04 16:08:25 -07001488 put_new_page, private, page,
Yang Shidd4ae782020-12-14 19:13:06 -08001489 pass > 2, mode, reason,
1490 &ret_pages);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07001491 else
David Rientjes68711a72014-06-04 16:08:25 -07001492 rc = unmap_and_move(get_new_page, put_new_page,
Naoya Horiguchiadd05ce2015-06-24 16:56:50 -07001493 private, page, pass > 2, mode,
Yang Shidd4ae782020-12-14 19:13:06 -08001494 reason, &ret_pages);
1495 /*
1496 * The rules are:
1497 * Success: non hugetlb page will be freed, hugetlb
1498 * page will be put back
1499 * -EAGAIN: stay on the from list
1500 * -ENOMEM: stay on the from list
1501 * Other errno: put on ret_pages list then splice to
1502 * from list
1503 */
Christoph Lametere24f0b82006-06-23 02:03:51 -07001504 switch(rc) {
Yang Shid532e2e2020-12-14 19:13:16 -08001505 /*
1506 * THP migration might be unsupported or the
1507 * allocation could've failed so we should
1508 * retry on the same page with the THP split
1509 * to base pages.
1510 *
1511 * Head page is retried immediately and tail
1512 * pages are added to the tail of the list so
1513 * we encounter them after the rest of the list
1514 * is processed.
1515 */
1516 case -ENOSYS:
1517 /* THP migration is unsupported */
1518 if (is_thp) {
1519 if (!try_split_thp(page, &page2, from)) {
1520 nr_thp_split++;
1521 goto retry;
1522 }
1523
1524 nr_thp_failed++;
1525 nr_failed += nr_subpages;
1526 break;
1527 }
1528
1529 /* Hugetlb migration is unsupported */
1530 nr_failed++;
1531 break;
Christoph Lameter95a402c2006-06-23 02:03:53 -07001532 case -ENOMEM:
Michal Hocko94723aa2018-04-10 16:30:07 -07001533 /*
Yang Shid532e2e2020-12-14 19:13:16 -08001534 * When memory is low, don't bother to try to migrate
1535 * other pages, just exit.
Yang Shib0b515b2021-06-30 18:51:48 -07001536 * THP NUMA faulting doesn't split THP to retry.
Michal Hocko94723aa2018-04-10 16:30:07 -07001537 */
Yang Shib0b515b2021-06-30 18:51:48 -07001538 if (is_thp && !nosplit) {
Yang Shid532e2e2020-12-14 19:13:16 -08001539 if (!try_split_thp(page, &page2, from)) {
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001540 nr_thp_split++;
Michal Hocko94723aa2018-04-10 16:30:07 -07001541 goto retry;
1542 }
Zi Yan6c5c7b92020-09-25 21:19:14 -07001543
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001544 nr_thp_failed++;
1545 nr_failed += nr_subpages;
1546 goto out;
1547 }
David Rientjesdfef2ef2016-05-20 16:59:05 -07001548 nr_failed++;
Christoph Lameter95a402c2006-06-23 02:03:53 -07001549 goto out;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001550 case -EAGAIN:
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001551 if (is_thp) {
1552 thp_retry++;
1553 break;
1554 }
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001555 retry++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001556 break;
Rafael Aquini78bd5202012-12-11 16:02:31 -08001557 case MIGRATEPAGE_SUCCESS:
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001558 if (is_thp) {
1559 nr_thp_succeeded++;
1560 nr_succeeded += nr_subpages;
1561 break;
1562 }
Mel Gorman5647bc22012-10-19 10:46:20 +01001563 nr_succeeded++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001564 break;
1565 default:
Naoya Horiguchi354a3362014-01-21 15:51:14 -08001566 /*
Yang Shid532e2e2020-12-14 19:13:16 -08001567 * Permanent failure (-EBUSY, etc.):
Naoya Horiguchi354a3362014-01-21 15:51:14 -08001568 * unlike -EAGAIN case, the failed page is
1569 * removed from migration page list and not
1570 * retried in the next outer loop.
1571 */
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001572 if (is_thp) {
1573 nr_thp_failed++;
1574 nr_failed += nr_subpages;
1575 break;
1576 }
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001577 nr_failed++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001578 break;
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001579 }
Christoph Lameterb20a3502006-03-22 00:09:12 -08001580 }
1581 }
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001582 nr_failed += retry + thp_retry;
1583 nr_thp_failed += thp_retry;
Vlastimil Babkaf2f81fb2015-11-05 18:47:03 -08001584 rc = nr_failed;
Christoph Lameter95a402c2006-06-23 02:03:53 -07001585out:
Yang Shidd4ae782020-12-14 19:13:06 -08001586 /*
1587 * Put the permanent failure page back to migration list, they
1588 * will be put back to the right list by the caller.
1589 */
1590 list_splice(&ret_pages, from);
1591
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001592 count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
1593 count_vm_events(PGMIGRATE_FAIL, nr_failed);
1594 count_vm_events(THP_MIGRATION_SUCCESS, nr_thp_succeeded);
1595 count_vm_events(THP_MIGRATION_FAIL, nr_thp_failed);
1596 count_vm_events(THP_MIGRATION_SPLIT, nr_thp_split);
1597 trace_mm_migrate_pages(nr_succeeded, nr_failed, nr_thp_succeeded,
1598 nr_thp_failed, nr_thp_split, mode, reason);
Mel Gorman7b2a2d42012-10-19 14:07:31 +01001599
Christoph Lameterb20a3502006-03-22 00:09:12 -08001600 if (!swapwrite)
1601 current->flags &= ~PF_SWAPWRITE;
1602
Yang Shi5ac95882021-09-02 14:59:13 -07001603 if (ret_succeeded)
1604 *ret_succeeded = nr_succeeded;
1605
Rafael Aquini78bd5202012-12-11 16:02:31 -08001606 return rc;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001607}
Charan Teja Reddyf47b8522021-02-16 13:59:45 +05301608EXPORT_SYMBOL_GPL(migrate_pages);
Christoph Lameterb20a3502006-03-22 00:09:12 -08001609
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001610struct page *alloc_migration_target(struct page *page, unsigned long private)
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001611{
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001612 struct migration_target_control *mtc;
1613 gfp_t gfp_mask;
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001614 unsigned int order = 0;
1615 struct page *new_page = NULL;
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001616 int nid;
1617 int zidx;
1618
1619 mtc = (struct migration_target_control *)private;
1620 gfp_mask = mtc->gfp_mask;
1621 nid = mtc->nid;
1622 if (nid == NUMA_NO_NODE)
1623 nid = page_to_nid(page);
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001624
Joonsoo Kimd92bbc22020-08-11 18:37:17 -07001625 if (PageHuge(page)) {
1626 struct hstate *h = page_hstate(compound_head(page));
1627
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001628 gfp_mask = htlb_modify_alloc_mask(h, gfp_mask);
1629 return alloc_huge_page_nodemask(h, nid, mtc->nmask, gfp_mask);
Joonsoo Kimd92bbc22020-08-11 18:37:17 -07001630 }
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001631
1632 if (PageTransHuge(page)) {
Joonsoo Kim9933a0c2020-08-11 18:37:20 -07001633 /*
1634 * clear __GFP_RECLAIM to make the migration callback
1635 * consistent with regular THP allocations.
1636 */
1637 gfp_mask &= ~__GFP_RECLAIM;
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001638 gfp_mask |= GFP_TRANSHUGE;
1639 order = HPAGE_PMD_ORDER;
1640 }
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001641 zidx = zone_idx(page_zone(page));
1642 if (is_highmem_idx(zidx) || zidx == ZONE_MOVABLE)
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001643 gfp_mask |= __GFP_HIGHMEM;
1644
Matthew Wilcox (Oracle)84172f42021-04-29 23:01:15 -07001645 new_page = __alloc_pages(gfp_mask, order, nid, mtc->nmask);
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001646
1647 if (new_page && PageTransHuge(new_page))
1648 prep_transhuge_page(new_page);
1649
1650 return new_page;
1651}
1652
Christoph Lameter742755a2006-06-23 02:03:55 -07001653#ifdef CONFIG_NUMA
Christoph Lameter742755a2006-06-23 02:03:55 -07001654
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001655static int store_status(int __user *status, int start, int value, int nr)
Christoph Lameter742755a2006-06-23 02:03:55 -07001656{
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001657 while (nr-- > 0) {
1658 if (put_user(value, status + start))
1659 return -EFAULT;
1660 start++;
1661 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001662
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001663 return 0;
1664}
Christoph Lameter742755a2006-06-23 02:03:55 -07001665
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001666static int do_move_pages_to_node(struct mm_struct *mm,
1667 struct list_head *pagelist, int node)
1668{
1669 int err;
Joonsoo Kima0976312020-08-11 18:37:28 -07001670 struct migration_target_control mtc = {
1671 .nid = node,
1672 .gfp_mask = GFP_HIGHUSER_MOVABLE | __GFP_THISNODE,
1673 };
Christoph Lameter742755a2006-06-23 02:03:55 -07001674
Joonsoo Kima0976312020-08-11 18:37:28 -07001675 err = migrate_pages(pagelist, alloc_migration_target, NULL,
Yang Shi5ac95882021-09-02 14:59:13 -07001676 (unsigned long)&mtc, MIGRATE_SYNC, MR_SYSCALL, NULL);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001677 if (err)
1678 putback_movable_pages(pagelist);
1679 return err;
Christoph Lameter742755a2006-06-23 02:03:55 -07001680}
1681
1682/*
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001683 * Resolves the given address to a struct page, isolates it from the LRU and
1684 * puts it to the given pagelist.
Yang Shie0153fc2020-01-04 12:59:46 -08001685 * Returns:
1686 * errno - if the page cannot be found/isolated
1687 * 0 - when it doesn't have to be migrated because it is already on the
1688 * target node
1689 * 1 - when it has been queued
Christoph Lameter742755a2006-06-23 02:03:55 -07001690 */
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001691static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
1692 int node, struct list_head *pagelist, bool migrate_all)
Christoph Lameter742755a2006-06-23 02:03:55 -07001693{
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001694 struct vm_area_struct *vma;
1695 struct page *page;
1696 unsigned int follflags;
Christoph Lameter742755a2006-06-23 02:03:55 -07001697 int err;
Christoph Lameter742755a2006-06-23 02:03:55 -07001698
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001699 mmap_read_lock(mm);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001700 err = -EFAULT;
1701 vma = find_vma(mm, addr);
1702 if (!vma || addr < vma->vm_start || !vma_migratable(vma))
1703 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001704
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001705 /* FOLL_DUMP to ignore special (like zero) pages */
1706 follflags = FOLL_GET | FOLL_DUMP;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001707 page = follow_page(vma, addr, follflags);
Christoph Lameter742755a2006-06-23 02:03:55 -07001708
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001709 err = PTR_ERR(page);
1710 if (IS_ERR(page))
1711 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001712
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001713 err = -ENOENT;
1714 if (!page)
1715 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001716
Brice Gogline78bbfa2008-10-18 20:27:15 -07001717 err = 0;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001718 if (page_to_nid(page) == node)
1719 goto out_putpage;
Christoph Lameter742755a2006-06-23 02:03:55 -07001720
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001721 err = -EACCES;
1722 if (page_mapcount(page) > 1 && !migrate_all)
1723 goto out_putpage;
1724
1725 if (PageHuge(page)) {
1726 if (PageHead(page)) {
Miaohe Lin072e7412022-05-30 19:30:15 +08001727 err = isolate_hugetlb(page, pagelist);
1728 if (!err)
1729 err = 1;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001730 }
1731 } else {
1732 struct page *head;
1733
1734 head = compound_head(page);
1735 err = isolate_lru_page(head);
1736 if (err)
1737 goto out_putpage;
1738
Yang Shie0153fc2020-01-04 12:59:46 -08001739 err = 1;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001740 list_add_tail(&head->lru, pagelist);
1741 mod_node_page_state(page_pgdat(head),
Huang Ying9de4f222020-04-06 20:04:41 -07001742 NR_ISOLATED_ANON + page_is_file_lru(head),
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -07001743 thp_nr_pages(head));
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001744 }
1745out_putpage:
1746 /*
1747 * Either remove the duplicate refcount from
1748 * isolate_lru_page() or drop the page ref if it was
1749 * not isolated.
1750 */
1751 put_page(page);
1752out:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001753 mmap_read_unlock(mm);
Christoph Lameter742755a2006-06-23 02:03:55 -07001754 return err;
1755}
1756
Wei Yang7ca87832020-04-06 20:04:12 -07001757static int move_pages_and_store_status(struct mm_struct *mm, int node,
1758 struct list_head *pagelist, int __user *status,
1759 int start, int i, unsigned long nr_pages)
1760{
1761 int err;
1762
Wei Yang5d7ae892020-04-06 20:04:15 -07001763 if (list_empty(pagelist))
1764 return 0;
1765
Wei Yang7ca87832020-04-06 20:04:12 -07001766 err = do_move_pages_to_node(mm, pagelist, node);
1767 if (err) {
1768 /*
1769 * Positive err means the number of failed
1770 * pages to migrate. Since we are going to
1771 * abort and return the number of non-migrated
Long Liab9dd4f2020-12-14 19:12:52 -08001772 * pages, so need to include the rest of the
Wei Yang7ca87832020-04-06 20:04:12 -07001773 * nr_pages that have not been attempted as
1774 * well.
1775 */
1776 if (err > 0)
1777 err += nr_pages - i - 1;
1778 return err;
1779 }
1780 return store_status(status, start, node, i - start);
1781}
1782
Christoph Lameter742755a2006-06-23 02:03:55 -07001783/*
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001784 * Migrate an array of page address onto an array of nodes and fill
1785 * the corresponding array of status.
1786 */
Christoph Lameter3268c632012-03-21 16:34:06 -07001787static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001788 unsigned long nr_pages,
1789 const void __user * __user *pages,
1790 const int __user *nodes,
1791 int __user *status, int flags)
1792{
Gregory Price556b68d2023-10-03 10:48:56 -04001793 compat_uptr_t __user *compat_pages = (void __user *)pages;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001794 int current_node = NUMA_NO_NODE;
1795 LIST_HEAD(pagelist);
1796 int start, i;
1797 int err = 0, err1;
Brice Goglin35282a22009-06-16 15:32:43 -07001798
Minchan Kim361a2a22021-05-04 18:36:57 -07001799 lru_cache_disable();
Brice Goglin35282a22009-06-16 15:32:43 -07001800
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001801 for (i = start = 0; i < nr_pages; i++) {
1802 const void __user *p;
1803 unsigned long addr;
1804 int node;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001805
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001806 err = -EFAULT;
Gregory Price556b68d2023-10-03 10:48:56 -04001807 if (in_compat_syscall()) {
1808 compat_uptr_t cp;
1809
1810 if (get_user(cp, compat_pages + i))
1811 goto out_flush;
1812
1813 p = compat_ptr(cp);
1814 } else {
1815 if (get_user(p, pages + i))
1816 goto out_flush;
1817 }
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001818 if (get_user(node, nodes + i))
1819 goto out_flush;
Andrey Konovalov057d33892019-09-25 16:48:30 -07001820 addr = (unsigned long)untagged_addr(p);
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001821
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001822 err = -ENODEV;
1823 if (node < 0 || node >= MAX_NUMNODES)
1824 goto out_flush;
1825 if (!node_state(node, N_MEMORY))
1826 goto out_flush;
Brice Goglin3140a222009-01-06 14:38:57 -08001827
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001828 err = -EACCES;
1829 if (!node_isset(node, task_nodes))
1830 goto out_flush;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001831
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001832 if (current_node == NUMA_NO_NODE) {
1833 current_node = node;
1834 start = i;
1835 } else if (node != current_node) {
Wei Yang7ca87832020-04-06 20:04:12 -07001836 err = move_pages_and_store_status(mm, current_node,
1837 &pagelist, status, start, i, nr_pages);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001838 if (err)
1839 goto out;
1840 start = i;
1841 current_node = node;
Brice Goglin3140a222009-01-06 14:38:57 -08001842 }
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001843
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001844 /*
1845 * Errors in the page lookup or isolation are not fatal and we simply
1846 * report them via status
1847 */
1848 err = add_page_for_migration(mm, addr, current_node,
1849 &pagelist, flags & MPOL_MF_MOVE_ALL);
Yang Shie0153fc2020-01-04 12:59:46 -08001850
Wei Yangd08221a2020-04-06 20:04:18 -07001851 if (err > 0) {
Yang Shie0153fc2020-01-04 12:59:46 -08001852 /* The page is successfully queued for migration */
1853 continue;
1854 }
Brice Goglin3140a222009-01-06 14:38:57 -08001855
Wei Yangd08221a2020-04-06 20:04:18 -07001856 /*
1857 * If the page is already on the target node (!err), store the
1858 * node, otherwise, store the err.
1859 */
1860 err = store_status(status, i, err ? : current_node, 1);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001861 if (err)
1862 goto out_flush;
Brice Goglin3140a222009-01-06 14:38:57 -08001863
Wei Yang7ca87832020-04-06 20:04:12 -07001864 err = move_pages_and_store_status(mm, current_node, &pagelist,
1865 status, start, i, nr_pages);
Wei Yang4afdace2020-04-06 20:04:09 -07001866 if (err)
1867 goto out;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001868 current_node = NUMA_NO_NODE;
Brice Goglin3140a222009-01-06 14:38:57 -08001869 }
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001870out_flush:
1871 /* Make sure we do not overwrite the existing error */
Wei Yang7ca87832020-04-06 20:04:12 -07001872 err1 = move_pages_and_store_status(mm, current_node, &pagelist,
1873 status, start, i, nr_pages);
Wei Yangdfe9aa22020-01-30 22:11:14 -08001874 if (err >= 0)
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001875 err = err1;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001876out:
Minchan Kim361a2a22021-05-04 18:36:57 -07001877 lru_cache_enable();
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001878 return err;
1879}
1880
1881/*
Brice Goglin2f007e72008-10-18 20:27:16 -07001882 * Determine the nodes of an array of pages and store it in an array of status.
Christoph Lameter742755a2006-06-23 02:03:55 -07001883 */
Brice Goglin80bba122008-12-09 13:14:23 -08001884static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1885 const void __user **pages, int *status)
Christoph Lameter742755a2006-06-23 02:03:55 -07001886{
Brice Goglin2f007e72008-10-18 20:27:16 -07001887 unsigned long i;
Brice Goglin2f007e72008-10-18 20:27:16 -07001888
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001889 mmap_read_lock(mm);
Christoph Lameter742755a2006-06-23 02:03:55 -07001890
Brice Goglin2f007e72008-10-18 20:27:16 -07001891 for (i = 0; i < nr_pages; i++) {
Brice Goglin80bba122008-12-09 13:14:23 -08001892 unsigned long addr = (unsigned long)(*pages);
Christoph Lameter742755a2006-06-23 02:03:55 -07001893 struct vm_area_struct *vma;
1894 struct page *page;
KOSAKI Motohiroc095adb2008-12-16 16:06:43 +09001895 int err = -EFAULT;
Brice Goglin2f007e72008-10-18 20:27:16 -07001896
Liam Howlett059b8b42021-06-28 19:39:44 -07001897 vma = vma_lookup(mm, addr);
1898 if (!vma)
Christoph Lameter742755a2006-06-23 02:03:55 -07001899 goto set_status;
1900
Kirill A. Shutemovd8998442015-09-04 15:47:53 -07001901 /* FOLL_DUMP to ignore special (like zero) pages */
1902 page = follow_page(vma, addr, FOLL_DUMP);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001903
1904 err = PTR_ERR(page);
1905 if (IS_ERR(page))
1906 goto set_status;
1907
Kirill A. Shutemovd8998442015-09-04 15:47:53 -07001908 err = page ? page_to_nid(page) : -ENOENT;
Christoph Lameter742755a2006-06-23 02:03:55 -07001909set_status:
Brice Goglin80bba122008-12-09 13:14:23 -08001910 *status = err;
1911
1912 pages++;
1913 status++;
1914 }
1915
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001916 mmap_read_unlock(mm);
Brice Goglin80bba122008-12-09 13:14:23 -08001917}
1918
Arnd Bergmann5b1b5612021-09-08 15:18:17 -07001919static int get_compat_pages_array(const void __user *chunk_pages[],
1920 const void __user * __user *pages,
1921 unsigned long chunk_nr)
1922{
1923 compat_uptr_t __user *pages32 = (compat_uptr_t __user *)pages;
1924 compat_uptr_t p;
1925 int i;
1926
1927 for (i = 0; i < chunk_nr; i++) {
1928 if (get_user(p, pages32 + i))
1929 return -EFAULT;
1930 chunk_pages[i] = compat_ptr(p);
1931 }
1932
1933 return 0;
1934}
1935
Brice Goglin80bba122008-12-09 13:14:23 -08001936/*
1937 * Determine the nodes of a user array of pages and store it in
1938 * a user array of status.
1939 */
1940static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1941 const void __user * __user *pages,
1942 int __user *status)
1943{
1944#define DO_PAGES_STAT_CHUNK_NR 16
1945 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1946 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
Brice Goglin80bba122008-12-09 13:14:23 -08001947
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001948 while (nr_pages) {
1949 unsigned long chunk_nr;
Brice Goglin80bba122008-12-09 13:14:23 -08001950
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001951 chunk_nr = nr_pages;
1952 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1953 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1954
Arnd Bergmann5b1b5612021-09-08 15:18:17 -07001955 if (in_compat_syscall()) {
1956 if (get_compat_pages_array(chunk_pages, pages,
1957 chunk_nr))
1958 break;
1959 } else {
1960 if (copy_from_user(chunk_pages, pages,
1961 chunk_nr * sizeof(*chunk_pages)))
1962 break;
1963 }
Brice Goglin80bba122008-12-09 13:14:23 -08001964
1965 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1966
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001967 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1968 break;
Christoph Lameter742755a2006-06-23 02:03:55 -07001969
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001970 pages += chunk_nr;
1971 status += chunk_nr;
1972 nr_pages -= chunk_nr;
1973 }
1974 return nr_pages ? -EFAULT : 0;
Christoph Lameter742755a2006-06-23 02:03:55 -07001975}
1976
Miaohe Lin4dc200c2020-10-17 16:14:03 -07001977static struct mm_struct *find_mm_struct(pid_t pid, nodemask_t *mem_nodes)
1978{
1979 struct task_struct *task;
1980 struct mm_struct *mm;
1981
1982 /*
1983 * There is no need to check if current process has the right to modify
1984 * the specified process when they are same.
1985 */
1986 if (!pid) {
1987 mmget(current->mm);
1988 *mem_nodes = cpuset_mems_allowed(current);
1989 return current->mm;
1990 }
1991
1992 /* Find the mm_struct */
1993 rcu_read_lock();
1994 task = find_task_by_vpid(pid);
1995 if (!task) {
1996 rcu_read_unlock();
1997 return ERR_PTR(-ESRCH);
1998 }
1999 get_task_struct(task);
2000
2001 /*
2002 * Check if this process has the right to modify the specified
2003 * process. Use the regular "ptrace_may_access()" checks.
2004 */
2005 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
2006 rcu_read_unlock();
2007 mm = ERR_PTR(-EPERM);
2008 goto out;
2009 }
2010 rcu_read_unlock();
2011
2012 mm = ERR_PTR(security_task_movememory(task));
2013 if (IS_ERR(mm))
2014 goto out;
2015 *mem_nodes = cpuset_mems_allowed(task);
2016 mm = get_task_mm(task);
2017out:
2018 put_task_struct(task);
2019 if (!mm)
2020 mm = ERR_PTR(-EINVAL);
2021 return mm;
2022}
2023
Christoph Lameter742755a2006-06-23 02:03:55 -07002024/*
2025 * Move a list of pages in the address space of the currently executing
2026 * process.
2027 */
Dominik Brodowski7addf442018-03-17 16:08:03 +01002028static int kernel_move_pages(pid_t pid, unsigned long nr_pages,
2029 const void __user * __user *pages,
2030 const int __user *nodes,
2031 int __user *status, int flags)
Christoph Lameter742755a2006-06-23 02:03:55 -07002032{
Christoph Lameter742755a2006-06-23 02:03:55 -07002033 struct mm_struct *mm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07002034 int err;
Christoph Lameter3268c632012-03-21 16:34:06 -07002035 nodemask_t task_nodes;
Christoph Lameter742755a2006-06-23 02:03:55 -07002036
2037 /* Check flags */
2038 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
2039 return -EINVAL;
2040
2041 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
2042 return -EPERM;
2043
Miaohe Lin4dc200c2020-10-17 16:14:03 -07002044 mm = find_mm_struct(pid, &task_nodes);
2045 if (IS_ERR(mm))
2046 return PTR_ERR(mm);
Sasha Levin6e8b09e2012-04-25 16:01:53 -07002047
2048 if (nodes)
2049 err = do_pages_move(mm, task_nodes, nr_pages, pages,
2050 nodes, status, flags);
2051 else
2052 err = do_pages_stat(mm, nr_pages, pages, status);
Christoph Lameter3268c632012-03-21 16:34:06 -07002053
2054 mmput(mm);
2055 return err;
Christoph Lameter742755a2006-06-23 02:03:55 -07002056}
Christoph Lameter742755a2006-06-23 02:03:55 -07002057
Dominik Brodowski7addf442018-03-17 16:08:03 +01002058SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
2059 const void __user * __user *, pages,
2060 const int __user *, nodes,
2061 int __user *, status, int, flags)
2062{
2063 return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags);
2064}
2065
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002066#ifdef CONFIG_NUMA_BALANCING
2067/*
2068 * Returns true if this is a safe migration target node for misplaced NUMA
2069 * pages. Currently it only checks the watermarks which crude
2070 */
2071static bool migrate_balanced_pgdat(struct pglist_data *pgdat,
Mel Gorman3abef4e2013-02-22 16:34:27 -08002072 unsigned long nr_migrate_pages)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002073{
2074 int z;
Mel Gorman599d0c92016-07-28 15:45:31 -07002075
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002076 for (z = pgdat->nr_zones - 1; z >= 0; z--) {
2077 struct zone *zone = pgdat->node_zones + z;
2078
2079 if (!populated_zone(zone))
2080 continue;
2081
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002082 /* Avoid waking kswapd by allocating pages_to_migrate pages. */
2083 if (!zone_watermark_ok(zone, 0,
2084 high_wmark_pages(zone) +
2085 nr_migrate_pages,
Huang Yingbfe9d002019-11-30 17:57:28 -08002086 ZONE_MOVABLE, 0))
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002087 continue;
2088 return true;
2089 }
2090 return false;
2091}
2092
2093static struct page *alloc_misplaced_dst_page(struct page *page,
Michal Hocko666feb22018-04-10 16:30:03 -07002094 unsigned long data)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002095{
2096 int nid = (int) data;
2097 struct page *newpage;
2098
Vlastimil Babka96db8002015-09-08 15:03:50 -07002099 newpage = __alloc_pages_node(nid,
Johannes Weinere97ca8e52014-03-10 15:49:43 -07002100 (GFP_HIGHUSER_MOVABLE |
2101 __GFP_THISNODE | __GFP_NOMEMALLOC |
2102 __GFP_NORETRY | __GFP_NOWARN) &
Mel Gorman8479eba2016-02-26 15:19:31 -08002103 ~__GFP_RECLAIM, 0);
Hillf Dantonbac03822012-11-27 14:46:24 +00002104
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002105 return newpage;
2106}
2107
Yang Shic5b5a3d2021-06-30 18:51:42 -07002108static struct page *alloc_misplaced_dst_page_thp(struct page *page,
2109 unsigned long data)
2110{
2111 int nid = (int) data;
2112 struct page *newpage;
2113
2114 newpage = alloc_pages_node(nid, (GFP_TRANSHUGE_LIGHT | __GFP_THISNODE),
2115 HPAGE_PMD_ORDER);
2116 if (!newpage)
2117 goto out;
2118
2119 prep_transhuge_page(newpage);
2120
2121out:
2122 return newpage;
2123}
2124
Mel Gorman1c30e012014-01-21 15:50:58 -08002125static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
Mel Gormanb32967f2012-11-19 12:35:47 +00002126{
Hugh Dickins340ef392013-02-22 16:34:33 -08002127 int page_lru;
Baolin Wang2b9b6242021-09-08 15:18:01 -07002128 int nr_pages = thp_nr_pages(page);
Mel Gormanb32967f2012-11-19 12:35:47 +00002129
Sasha Levin309381fea2014-01-23 15:52:54 -08002130 VM_BUG_ON_PAGE(compound_order(page) && !PageTransHuge(page), page);
Mel Gorman3abef4e2013-02-22 16:34:27 -08002131
Yang Shi662aeea2021-06-30 18:51:51 -07002132 /* Do not migrate THP mapped by multiple processes */
2133 if (PageTransHuge(page) && total_mapcount(page) > 1)
2134 return 0;
2135
Mel Gormanb32967f2012-11-19 12:35:47 +00002136 /* Avoid migrating to a node that is nearly full */
Baolin Wang2b9b6242021-09-08 15:18:01 -07002137 if (!migrate_balanced_pgdat(pgdat, nr_pages))
Hugh Dickins340ef392013-02-22 16:34:33 -08002138 return 0;
Mel Gormanb32967f2012-11-19 12:35:47 +00002139
Hugh Dickins340ef392013-02-22 16:34:33 -08002140 if (isolate_lru_page(page))
2141 return 0;
Mel Gormanb32967f2012-11-19 12:35:47 +00002142
Huang Ying9de4f222020-04-06 20:04:41 -07002143 page_lru = page_is_file_lru(page);
Mel Gorman599d0c92016-07-28 15:45:31 -07002144 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_lru,
Baolin Wang2b9b6242021-09-08 15:18:01 -07002145 nr_pages);
Hugh Dickins340ef392013-02-22 16:34:33 -08002146
2147 /*
2148 * Isolating the page has taken another reference, so the
2149 * caller's reference can be safely dropped without the page
2150 * disappearing underneath us during migration.
Mel Gormanb32967f2012-11-19 12:35:47 +00002151 */
2152 put_page(page);
Hugh Dickins340ef392013-02-22 16:34:33 -08002153 return 1;
Mel Gormanb32967f2012-11-19 12:35:47 +00002154}
2155
Mel Gormana8f60772012-11-14 21:41:46 +00002156/*
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002157 * Attempt to migrate a misplaced page to the specified destination
2158 * node. Caller is expected to have an elevated reference count on
2159 * the page that will be dropped by this function before returning.
2160 */
Mel Gorman1bc115d2013-10-07 11:29:05 +01002161int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma,
2162 int node)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002163{
Mel Gormana8f60772012-11-14 21:41:46 +00002164 pg_data_t *pgdat = NODE_DATA(node);
Hugh Dickins340ef392013-02-22 16:34:33 -08002165 int isolated;
Mel Gormanb32967f2012-11-19 12:35:47 +00002166 int nr_remaining;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002167 LIST_HEAD(migratepages);
Yang Shic5b5a3d2021-06-30 18:51:42 -07002168 new_page_t *new;
2169 bool compound;
Aneesh Kumar K.Vb5916c02021-07-29 14:53:47 -07002170 int nr_pages = thp_nr_pages(page);
Yang Shic5b5a3d2021-06-30 18:51:42 -07002171
2172 /*
2173 * PTE mapped THP or HugeTLB page can't reach here so the page could
2174 * be either base page or THP. And it must be head page if it is
2175 * THP.
2176 */
2177 compound = PageTransHuge(page);
2178
2179 if (compound)
2180 new = alloc_misplaced_dst_page_thp;
2181 else
2182 new = alloc_misplaced_dst_page;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002183
2184 /*
Mel Gorman1bc115d2013-10-07 11:29:05 +01002185 * Don't migrate file pages that are mapped in multiple processes
2186 * with execute permissions as they are probably shared libraries.
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002187 */
Miaohe Lin7ee820e2021-05-04 18:37:16 -07002188 if (page_mapcount(page) != 1 && page_is_file_lru(page) &&
2189 (vma->vm_flags & VM_EXEC))
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002190 goto out;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002191
Mel Gormana8f60772012-11-14 21:41:46 +00002192 /*
Mel Gorman09a913a2018-04-10 16:29:20 -07002193 * Also do not migrate dirty pages as not all filesystems can move
2194 * dirty pages in MIGRATE_ASYNC mode which is a waste of cycles.
2195 */
Huang Ying9de4f222020-04-06 20:04:41 -07002196 if (page_is_file_lru(page) && PageDirty(page))
Mel Gorman09a913a2018-04-10 16:29:20 -07002197 goto out;
2198
Mel Gormanb32967f2012-11-19 12:35:47 +00002199 isolated = numamigrate_isolate_page(pgdat, page);
2200 if (!isolated)
2201 goto out;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002202
Mel Gormanb32967f2012-11-19 12:35:47 +00002203 list_add(&page->lru, &migratepages);
Yang Shic5b5a3d2021-06-30 18:51:42 -07002204 nr_remaining = migrate_pages(&migratepages, *new, NULL, node,
Yang Shi5ac95882021-09-02 14:59:13 -07002205 MIGRATE_ASYNC, MR_NUMA_MISPLACED, NULL);
Mel Gormanb32967f2012-11-19 12:35:47 +00002206 if (nr_remaining) {
Joonsoo Kim59c82b72014-01-21 15:51:17 -08002207 if (!list_empty(&migratepages)) {
2208 list_del(&page->lru);
Yang Shic5fc5c32021-06-30 18:51:45 -07002209 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
2210 page_is_file_lru(page), -nr_pages);
Joonsoo Kim59c82b72014-01-21 15:51:17 -08002211 putback_lru_page(page);
2212 }
Mel Gormanb32967f2012-11-19 12:35:47 +00002213 isolated = 0;
2214 } else
Yang Shic5fc5c32021-06-30 18:51:45 -07002215 count_vm_numa_events(NUMA_PAGE_MIGRATE, nr_pages);
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002216 BUG_ON(!list_empty(&migratepages));
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002217 return isolated;
Hugh Dickins340ef392013-02-22 16:34:33 -08002218
2219out:
2220 put_page(page);
2221 return 0;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002222}
Mel Gorman220018d2012-12-05 09:32:56 +00002223#endif /* CONFIG_NUMA_BALANCING */
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002224#endif /* CONFIG_NUMA */
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002225
Christoph Hellwig9b2ed9c2019-08-14 09:59:28 +02002226#ifdef CONFIG_DEVICE_PRIVATE
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002227static int migrate_vma_collect_skip(unsigned long start,
2228 unsigned long end,
2229 struct mm_walk *walk)
2230{
2231 struct migrate_vma *migrate = walk->private;
2232 unsigned long addr;
2233
Ralph Campbell872ea702020-01-30 22:14:38 -08002234 for (addr = start; addr < end; addr += PAGE_SIZE) {
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002235 migrate->dst[migrate->npages] = 0;
2236 migrate->src[migrate->npages++] = 0;
2237 }
2238
2239 return 0;
2240}
2241
Miaohe Lin843e1be2021-05-04 18:37:13 -07002242static int migrate_vma_collect_hole(unsigned long start,
2243 unsigned long end,
2244 __always_unused int depth,
2245 struct mm_walk *walk)
2246{
2247 struct migrate_vma *migrate = walk->private;
2248 unsigned long addr;
2249
2250 /* Only allow populating anonymous memory. */
2251 if (!vma_is_anonymous(walk->vma))
2252 return migrate_vma_collect_skip(start, end, walk);
2253
2254 for (addr = start; addr < end; addr += PAGE_SIZE) {
2255 migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE;
2256 migrate->dst[migrate->npages] = 0;
2257 migrate->npages++;
2258 migrate->cpages++;
2259 }
2260
2261 return 0;
2262}
2263
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002264static int migrate_vma_collect_pmd(pmd_t *pmdp,
2265 unsigned long start,
2266 unsigned long end,
2267 struct mm_walk *walk)
2268{
2269 struct migrate_vma *migrate = walk->private;
2270 struct vm_area_struct *vma = walk->vma;
2271 struct mm_struct *mm = vma->vm_mm;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002272 unsigned long addr = start, unmapped = 0;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002273 spinlock_t *ptl;
2274 pte_t *ptep;
2275
2276again:
2277 if (pmd_none(*pmdp))
Steven Priceb7a16c72020-02-03 17:36:03 -08002278 return migrate_vma_collect_hole(start, end, -1, walk);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002279
2280 if (pmd_trans_huge(*pmdp)) {
2281 struct page *page;
2282
2283 ptl = pmd_lock(mm, pmdp);
2284 if (unlikely(!pmd_trans_huge(*pmdp))) {
2285 spin_unlock(ptl);
2286 goto again;
2287 }
2288
2289 page = pmd_page(*pmdp);
2290 if (is_huge_zero_page(page)) {
2291 spin_unlock(ptl);
2292 split_huge_pmd(vma, pmdp, addr);
2293 if (pmd_trans_unstable(pmdp))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002294 return migrate_vma_collect_skip(start, end,
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002295 walk);
2296 } else {
2297 int ret;
2298
2299 get_page(page);
2300 spin_unlock(ptl);
2301 if (unlikely(!trylock_page(page)))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002302 return migrate_vma_collect_skip(start, end,
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002303 walk);
2304 ret = split_huge_page(page);
2305 unlock_page(page);
2306 put_page(page);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002307 if (ret)
2308 return migrate_vma_collect_skip(start, end,
2309 walk);
2310 if (pmd_none(*pmdp))
Steven Priceb7a16c72020-02-03 17:36:03 -08002311 return migrate_vma_collect_hole(start, end, -1,
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002312 walk);
2313 }
2314 }
2315
2316 if (unlikely(pmd_bad(*pmdp)))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002317 return migrate_vma_collect_skip(start, end, walk);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002318
2319 ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002320 arch_enter_lazy_mmu_mode();
2321
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002322 for (; addr < end; addr += PAGE_SIZE, ptep++) {
Christoph Hellwig800bb1c2020-03-16 20:32:14 +01002323 unsigned long mpfn = 0, pfn;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002324 struct page *page;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002325 swp_entry_t entry;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002326 pte_t pte;
2327
2328 pte = *ptep;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002329
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002330 if (pte_none(pte)) {
Ralph Campbell0744f282020-08-11 18:31:41 -07002331 if (vma_is_anonymous(vma)) {
2332 mpfn = MIGRATE_PFN_MIGRATE;
2333 migrate->cpages++;
2334 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002335 goto next;
2336 }
2337
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002338 if (!pte_present(pte)) {
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002339 /*
2340 * Only care about unaddressable device page special
2341 * page table entry. Other special swap entries are not
2342 * migratable, and we ignore regular swapped page.
2343 */
2344 entry = pte_to_swp_entry(pte);
2345 if (!is_device_private_entry(entry))
2346 goto next;
2347
Alistair Poppleaf5cdaf2021-06-30 18:54:06 -07002348 page = pfn_swap_entry_to_page(entry);
Ralph Campbell51431922020-07-23 15:30:00 -07002349 if (!(migrate->flags &
2350 MIGRATE_VMA_SELECT_DEVICE_PRIVATE) ||
2351 page->pgmap->owner != migrate->pgmap_owner)
Christoph Hellwig800bb1c2020-03-16 20:32:14 +01002352 goto next;
2353
Christoph Hellwig06d462b2019-08-14 09:59:27 +02002354 mpfn = migrate_pfn(page_to_pfn(page)) |
2355 MIGRATE_PFN_MIGRATE;
Alistair Popple4dd845b2021-06-30 18:54:09 -07002356 if (is_writable_device_private_entry(entry))
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002357 mpfn |= MIGRATE_PFN_WRITE;
2358 } else {
Ralph Campbell51431922020-07-23 15:30:00 -07002359 if (!(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM))
Christoph Hellwig800bb1c2020-03-16 20:32:14 +01002360 goto next;
Pingfan Liu276f7562019-09-23 15:37:38 -07002361 pfn = pte_pfn(pte);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002362 if (is_zero_pfn(pfn)) {
2363 mpfn = MIGRATE_PFN_MIGRATE;
2364 migrate->cpages++;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002365 goto next;
2366 }
Christoph Hellwig25b29952019-06-13 22:50:49 +02002367 page = vm_normal_page(migrate->vma, addr, pte);
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002368 mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
2369 mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0;
2370 }
2371
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002372 /* FIXME support THP */
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002373 if (!page || !page->mapping || PageTransCompound(page)) {
Pingfan Liu276f7562019-09-23 15:37:38 -07002374 mpfn = 0;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002375 goto next;
2376 }
2377
2378 /*
2379 * By getting a reference on the page we pin it and that blocks
2380 * any kind of migration. Side effect is that it "freezes" the
2381 * pte.
2382 *
2383 * We drop this reference after isolating the page from the lru
2384 * for non device page (device page are not on the lru and thus
2385 * can't be dropped from it).
2386 */
2387 get_page(page);
2388 migrate->cpages++;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002389
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002390 /*
2391 * Optimize for the common case where page is only mapped once
2392 * in one process. If we can lock the page, then we can safely
2393 * set up a special migration page table entry now.
2394 */
2395 if (trylock_page(page)) {
2396 pte_t swp_pte;
2397
2398 mpfn |= MIGRATE_PFN_LOCKED;
2399 ptep_get_and_clear(mm, addr, ptep);
2400
2401 /* Setup special migration page table entry */
Alistair Popple4dd845b2021-06-30 18:54:09 -07002402 if (mpfn & MIGRATE_PFN_WRITE)
2403 entry = make_writable_migration_entry(
2404 page_to_pfn(page));
2405 else
2406 entry = make_readable_migration_entry(
2407 page_to_pfn(page));
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002408 swp_pte = swp_entry_to_pte(entry);
Alistair Popplead7df762020-09-04 16:36:01 -07002409 if (pte_present(pte)) {
2410 if (pte_soft_dirty(pte))
2411 swp_pte = pte_swp_mksoft_dirty(swp_pte);
2412 if (pte_uffd_wp(pte))
2413 swp_pte = pte_swp_mkuffd_wp(swp_pte);
2414 } else {
2415 if (pte_swp_soft_dirty(pte))
2416 swp_pte = pte_swp_mksoft_dirty(swp_pte);
2417 if (pte_swp_uffd_wp(pte))
2418 swp_pte = pte_swp_mkuffd_wp(swp_pte);
2419 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002420 set_pte_at(mm, addr, ptep, swp_pte);
2421
2422 /*
2423 * This is like regular unmap: we remove the rmap and
2424 * drop page refcount. Page won't be freed, as we took
2425 * a reference just above.
2426 */
2427 page_remove_rmap(page, false);
2428 put_page(page);
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002429
2430 if (pte_present(pte))
2431 unmapped++;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002432 }
2433
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002434next:
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002435 migrate->dst[migrate->npages] = 0;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002436 migrate->src[migrate->npages++] = mpfn;
2437 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002438
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002439 /* Only flush the TLB if we actually modified any entries */
2440 if (unmapped)
2441 flush_tlb_range(walk->vma, start, end);
2442
Alistair Popple1299c112022-09-02 10:35:51 +10002443 arch_leave_lazy_mmu_mode();
2444 pte_unmap_unlock(ptep - 1, ptl);
2445
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002446 return 0;
2447}
2448
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02002449static const struct mm_walk_ops migrate_vma_walk_ops = {
2450 .pmd_entry = migrate_vma_collect_pmd,
2451 .pte_hole = migrate_vma_collect_hole,
2452};
2453
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002454/*
2455 * migrate_vma_collect() - collect pages over a range of virtual addresses
2456 * @migrate: migrate struct containing all migration information
2457 *
2458 * This will walk the CPU page table. For each virtual address backed by a
2459 * valid page, it updates the src array and takes a reference on the page, in
2460 * order to pin the page until we lock it and unmap it.
2461 */
2462static void migrate_vma_collect(struct migrate_vma *migrate)
2463{
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002464 struct mmu_notifier_range range;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002465
Ralph Campbell998427b2020-07-23 15:30:01 -07002466 /*
2467 * Note that the pgmap_owner is passed to the mmu notifier callback so
2468 * that the registered device driver can skip invalidating device
2469 * private page mappings that won't be migrated.
2470 */
Alistair Popple6b49bf62021-06-30 18:54:19 -07002471 mmu_notifier_range_init_owner(&range, MMU_NOTIFY_MIGRATE, 0,
2472 migrate->vma, migrate->vma->vm_mm, migrate->start, migrate->end,
Ralph Campbellc1a06df2020-08-06 23:17:09 -07002473 migrate->pgmap_owner);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002474 mmu_notifier_invalidate_range_start(&range);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002475
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02002476 walk_page_range(migrate->vma->vm_mm, migrate->start, migrate->end,
2477 &migrate_vma_walk_ops, migrate);
2478
2479 mmu_notifier_invalidate_range_end(&range);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002480 migrate->end = migrate->start + (migrate->npages << PAGE_SHIFT);
2481}
2482
2483/*
2484 * migrate_vma_check_page() - check if page is pinned or not
2485 * @page: struct page to check
2486 *
2487 * Pinned pages cannot be migrated. This is the same test as in
2488 * migrate_page_move_mapping(), except that here we allow migration of a
2489 * ZONE_DEVICE page.
2490 */
2491static bool migrate_vma_check_page(struct page *page)
2492{
2493 /*
2494 * One extra ref because caller holds an extra reference, either from
2495 * isolate_lru_page() for a regular page, or migrate_vma_collect() for
2496 * a device page.
2497 */
2498 int extra = 1;
2499
2500 /*
2501 * FIXME support THP (transparent huge page), it is bit more complex to
2502 * check them than regular pages, because they can be mapped with a pmd
2503 * or with a pte (split pte mapping).
2504 */
2505 if (PageCompound(page))
2506 return false;
2507
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002508 /* Page from ZONE_DEVICE have one extra reference */
2509 if (is_zone_device_page(page)) {
2510 /*
2511 * Private page can never be pin as they have no valid pte and
2512 * GUP will fail for those. Yet if there is a pending migration
2513 * a thread might try to wait on the pte migration entry and
2514 * will bump the page reference count. Sadly there is no way to
2515 * differentiate a regular pin from migration wait. Hence to
2516 * avoid 2 racing thread trying to migrate back to CPU to enter
Haitao Shi8958b242020-12-15 20:47:26 -08002517 * infinite loop (one stopping migration because the other is
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002518 * waiting on pte migration entry). We always return true here.
2519 *
2520 * FIXME proper solution is to rework migration_entry_wait() so
2521 * it does not need to take a reference on page.
2522 */
Christoph Hellwig25b29952019-06-13 22:50:49 +02002523 return is_device_private_page(page);
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002524 }
2525
Jérôme Glissedf6ad692017-09-08 16:12:24 -07002526 /* For file back page */
2527 if (page_mapping(page))
2528 extra += 1 + page_has_private(page);
2529
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002530 if ((page_count(page) - extra) > page_mapcount(page))
2531 return false;
2532
2533 return true;
2534}
2535
2536/*
2537 * migrate_vma_prepare() - lock pages and isolate them from the lru
2538 * @migrate: migrate struct containing all migration information
2539 *
2540 * This locks pages that have been collected by migrate_vma_collect(). Once each
2541 * page is locked it is isolated from the lru (for non-device pages). Finally,
2542 * the ref taken by migrate_vma_collect() is dropped, as locked pages cannot be
2543 * migrated by concurrent kernel threads.
2544 */
2545static void migrate_vma_prepare(struct migrate_vma *migrate)
2546{
2547 const unsigned long npages = migrate->npages;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002548 const unsigned long start = migrate->start;
2549 unsigned long addr, i, restore = 0;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002550 bool allow_drain = true;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002551
2552 lru_add_drain();
2553
2554 for (i = 0; (i < npages) && migrate->cpages; i++) {
2555 struct page *page = migrate_pfn_to_page(migrate->src[i]);
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002556 bool remap = true;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002557
2558 if (!page)
2559 continue;
2560
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002561 if (!(migrate->src[i] & MIGRATE_PFN_LOCKED)) {
2562 /*
2563 * Because we are migrating several pages there can be
2564 * a deadlock between 2 concurrent migration where each
2565 * are waiting on each other page lock.
2566 *
2567 * Make migrate_vma() a best effort thing and backoff
2568 * for any page we can not lock right away.
2569 */
2570 if (!trylock_page(page)) {
2571 migrate->src[i] = 0;
2572 migrate->cpages--;
2573 put_page(page);
2574 continue;
2575 }
2576 remap = false;
2577 migrate->src[i] |= MIGRATE_PFN_LOCKED;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002578 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002579
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002580 /* ZONE_DEVICE pages are not on LRU */
2581 if (!is_zone_device_page(page)) {
2582 if (!PageLRU(page) && allow_drain) {
2583 /* Drain CPU's pagevec */
2584 lru_add_drain_all();
2585 allow_drain = false;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002586 }
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002587
2588 if (isolate_lru_page(page)) {
2589 if (remap) {
2590 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2591 migrate->cpages--;
2592 restore++;
2593 } else {
2594 migrate->src[i] = 0;
2595 unlock_page(page);
2596 migrate->cpages--;
2597 put_page(page);
2598 }
2599 continue;
2600 }
2601
2602 /* Drop the reference we took in collect */
2603 put_page(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002604 }
2605
2606 if (!migrate_vma_check_page(page)) {
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002607 if (remap) {
2608 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2609 migrate->cpages--;
2610 restore++;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002611
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002612 if (!is_zone_device_page(page)) {
2613 get_page(page);
2614 putback_lru_page(page);
2615 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002616 } else {
2617 migrate->src[i] = 0;
2618 unlock_page(page);
2619 migrate->cpages--;
2620
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002621 if (!is_zone_device_page(page))
2622 putback_lru_page(page);
2623 else
2624 put_page(page);
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002625 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002626 }
2627 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002628
2629 for (i = 0, addr = start; i < npages && restore; i++, addr += PAGE_SIZE) {
2630 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2631
2632 if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE))
2633 continue;
2634
2635 remove_migration_pte(page, migrate->vma, addr, page);
2636
2637 migrate->src[i] = 0;
2638 unlock_page(page);
2639 put_page(page);
2640 restore--;
2641 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002642}
2643
2644/*
2645 * migrate_vma_unmap() - replace page mapping with special migration pte entry
2646 * @migrate: migrate struct containing all migration information
2647 *
2648 * Replace page mapping (CPU page table pte) with a special migration pte entry
2649 * and check again if it has been pinned. Pinned pages are restored because we
2650 * cannot migrate them.
2651 *
2652 * This is the last step before we call the device driver callback to allocate
2653 * destination memory and copy contents of original page over to new page.
2654 */
2655static void migrate_vma_unmap(struct migrate_vma *migrate)
2656{
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002657 const unsigned long npages = migrate->npages;
2658 const unsigned long start = migrate->start;
2659 unsigned long addr, i, restore = 0;
2660
2661 for (i = 0; i < npages; i++) {
2662 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2663
2664 if (!page || !(migrate->src[i] & MIGRATE_PFN_MIGRATE))
2665 continue;
2666
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002667 if (page_mapped(page)) {
Alistair Popplea98a2f02021-06-30 18:54:16 -07002668 try_to_migrate(page, 0);
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002669 if (page_mapped(page))
2670 goto restore;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002671 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002672
2673 if (migrate_vma_check_page(page))
2674 continue;
2675
2676restore:
2677 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2678 migrate->cpages--;
2679 restore++;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002680 }
2681
2682 for (addr = start, i = 0; i < npages && restore; addr += PAGE_SIZE, i++) {
2683 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2684
2685 if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE))
2686 continue;
2687
2688 remove_migration_ptes(page, page, false);
2689
2690 migrate->src[i] = 0;
2691 unlock_page(page);
2692 restore--;
2693
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002694 if (is_zone_device_page(page))
2695 put_page(page);
2696 else
2697 putback_lru_page(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002698 }
2699}
2700
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002701/**
2702 * migrate_vma_setup() - prepare to migrate a range of memory
Randy Dunlapeaf444d2020-08-11 18:33:08 -07002703 * @args: contains the vma, start, and pfns arrays for the migration
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002704 *
2705 * Returns: negative errno on failures, 0 when 0 or more pages were migrated
2706 * without an error.
2707 *
2708 * Prepare to migrate a range of memory virtual address range by collecting all
2709 * the pages backing each virtual address in the range, saving them inside the
2710 * src array. Then lock those pages and unmap them. Once the pages are locked
2711 * and unmapped, check whether each page is pinned or not. Pages that aren't
2712 * pinned have the MIGRATE_PFN_MIGRATE flag set (by this function) in the
2713 * corresponding src array entry. Then restores any pages that are pinned, by
2714 * remapping and unlocking those pages.
2715 *
2716 * The caller should then allocate destination memory and copy source memory to
2717 * it for all those entries (ie with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE
2718 * flag set). Once these are allocated and copied, the caller must update each
2719 * corresponding entry in the dst array with the pfn value of the destination
2720 * page and with the MIGRATE_PFN_VALID and MIGRATE_PFN_LOCKED flags set
2721 * (destination pages must have their struct pages locked, via lock_page()).
2722 *
2723 * Note that the caller does not have to migrate all the pages that are marked
2724 * with MIGRATE_PFN_MIGRATE flag in src array unless this is a migration from
2725 * device memory to system memory. If the caller cannot migrate a device page
2726 * back to system memory, then it must return VM_FAULT_SIGBUS, which has severe
2727 * consequences for the userspace process, so it must be avoided if at all
2728 * possible.
2729 *
2730 * For empty entries inside CPU page table (pte_none() or pmd_none() is true) we
2731 * do set MIGRATE_PFN_MIGRATE flag inside the corresponding source array thus
Ingo Molnarf0953a12021-05-06 18:06:47 -07002732 * allowing the caller to allocate device memory for those unbacked virtual
2733 * addresses. For this the caller simply has to allocate device memory and
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002734 * properly set the destination entry like for regular migration. Note that
Ingo Molnarf0953a12021-05-06 18:06:47 -07002735 * this can still fail, and thus inside the device driver you must check if the
2736 * migration was successful for those entries after calling migrate_vma_pages(),
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002737 * just like for regular migration.
2738 *
2739 * After that, the callers must call migrate_vma_pages() to go over each entry
2740 * in the src array that has the MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag
2741 * set. If the corresponding entry in dst array has MIGRATE_PFN_VALID flag set,
2742 * then migrate_vma_pages() to migrate struct page information from the source
2743 * struct page to the destination struct page. If it fails to migrate the
2744 * struct page information, then it clears the MIGRATE_PFN_MIGRATE flag in the
2745 * src array.
2746 *
2747 * At this point all successfully migrated pages have an entry in the src
2748 * array with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag set and the dst
2749 * array entry with MIGRATE_PFN_VALID flag set.
2750 *
2751 * Once migrate_vma_pages() returns the caller may inspect which pages were
2752 * successfully migrated, and which were not. Successfully migrated pages will
2753 * have the MIGRATE_PFN_MIGRATE flag set for their src array entry.
2754 *
2755 * It is safe to update device page table after migrate_vma_pages() because
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002756 * both destination and source page are still locked, and the mmap_lock is held
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002757 * in read mode (hence no one can unmap the range being migrated).
2758 *
2759 * Once the caller is done cleaning up things and updating its page table (if it
2760 * chose to do so, this is not an obligation) it finally calls
2761 * migrate_vma_finalize() to update the CPU page table to point to new pages
2762 * for successfully migrated pages or otherwise restore the CPU page table to
2763 * point to the original source pages.
2764 */
2765int migrate_vma_setup(struct migrate_vma *args)
2766{
2767 long nr_pages = (args->end - args->start) >> PAGE_SHIFT;
2768
2769 args->start &= PAGE_MASK;
2770 args->end &= PAGE_MASK;
2771 if (!args->vma || is_vm_hugetlb_page(args->vma) ||
2772 (args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma))
2773 return -EINVAL;
2774 if (nr_pages <= 0)
2775 return -EINVAL;
2776 if (args->start < args->vma->vm_start ||
2777 args->start >= args->vma->vm_end)
2778 return -EINVAL;
2779 if (args->end <= args->vma->vm_start || args->end > args->vma->vm_end)
2780 return -EINVAL;
2781 if (!args->src || !args->dst)
2782 return -EINVAL;
2783
2784 memset(args->src, 0, sizeof(*args->src) * nr_pages);
2785 args->cpages = 0;
2786 args->npages = 0;
2787
2788 migrate_vma_collect(args);
2789
2790 if (args->cpages)
2791 migrate_vma_prepare(args);
2792 if (args->cpages)
2793 migrate_vma_unmap(args);
2794
2795 /*
2796 * At this point pages are locked and unmapped, and thus they have
2797 * stable content and can safely be copied to destination memory that
2798 * is allocated by the drivers.
2799 */
2800 return 0;
2801
2802}
2803EXPORT_SYMBOL(migrate_vma_setup);
2804
Ralph Campbell34290e22020-01-30 22:14:44 -08002805/*
2806 * This code closely matches the code in:
2807 * __handle_mm_fault()
2808 * handle_pte_fault()
2809 * do_anonymous_page()
2810 * to map in an anonymous zero page but the struct page will be a ZONE_DEVICE
2811 * private page.
2812 */
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002813static void migrate_vma_insert_page(struct migrate_vma *migrate,
2814 unsigned long addr,
2815 struct page *page,
Stephen Zhangd85c6db2020-12-14 19:13:20 -08002816 unsigned long *src)
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002817{
2818 struct vm_area_struct *vma = migrate->vma;
2819 struct mm_struct *mm = vma->vm_mm;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002820 bool flush = false;
2821 spinlock_t *ptl;
2822 pte_t entry;
2823 pgd_t *pgdp;
2824 p4d_t *p4dp;
2825 pud_t *pudp;
2826 pmd_t *pmdp;
2827 pte_t *ptep;
2828
2829 /* Only allow populating anonymous memory */
2830 if (!vma_is_anonymous(vma))
2831 goto abort;
2832
2833 pgdp = pgd_offset(mm, addr);
2834 p4dp = p4d_alloc(mm, pgdp, addr);
2835 if (!p4dp)
2836 goto abort;
2837 pudp = pud_alloc(mm, p4dp, addr);
2838 if (!pudp)
2839 goto abort;
2840 pmdp = pmd_alloc(mm, pudp, addr);
2841 if (!pmdp)
2842 goto abort;
2843
2844 if (pmd_trans_huge(*pmdp) || pmd_devmap(*pmdp))
2845 goto abort;
2846
2847 /*
2848 * Use pte_alloc() instead of pte_alloc_map(). We can't run
2849 * pte_offset_map() on pmds where a huge pmd might be created
2850 * from a different thread.
2851 *
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002852 * pte_alloc_map() is safe to use under mmap_write_lock(mm) or when
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002853 * parallel threads are excluded by other means.
2854 *
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002855 * Here we only have mmap_read_lock(mm).
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002856 */
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -08002857 if (pte_alloc(mm, pmdp))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002858 goto abort;
2859
2860 /* See the comment in pte_alloc_one_map() */
2861 if (unlikely(pmd_trans_unstable(pmdp)))
2862 goto abort;
2863
2864 if (unlikely(anon_vma_prepare(vma)))
2865 goto abort;
Johannes Weinerd9eb1ea2020-06-03 16:02:24 -07002866 if (mem_cgroup_charge(page, vma->vm_mm, GFP_KERNEL))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002867 goto abort;
2868
2869 /*
2870 * The memory barrier inside __SetPageUptodate makes sure that
2871 * preceding stores to the page contents become visible before
2872 * the set_pte_at() write.
2873 */
2874 __SetPageUptodate(page);
2875
Jérôme Glissedf6ad692017-09-08 16:12:24 -07002876 if (is_zone_device_page(page)) {
2877 if (is_device_private_page(page)) {
2878 swp_entry_t swp_entry;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002879
Alistair Popple4dd845b2021-06-30 18:54:09 -07002880 if (vma->vm_flags & VM_WRITE)
2881 swp_entry = make_writable_device_private_entry(
2882 page_to_pfn(page));
2883 else
2884 swp_entry = make_readable_device_private_entry(
2885 page_to_pfn(page));
Jérôme Glissedf6ad692017-09-08 16:12:24 -07002886 entry = swp_entry_to_pte(swp_entry);
Miaohe Lin34f5e9b2021-05-04 18:37:10 -07002887 } else {
2888 /*
2889 * For now we only support migrating to un-addressable
2890 * device memory.
2891 */
2892 pr_warn_once("Unsupported ZONE_DEVICE page type.\n");
2893 goto abort;
Jérôme Glissedf6ad692017-09-08 16:12:24 -07002894 }
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002895 } else {
2896 entry = mk_pte(page, vma->vm_page_prot);
2897 if (vma->vm_flags & VM_WRITE)
2898 entry = pte_mkwrite(pte_mkdirty(entry));
2899 }
2900
2901 ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
2902
Ralph Campbell34290e22020-01-30 22:14:44 -08002903 if (check_stable_address_space(mm))
2904 goto unlock_abort;
2905
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002906 if (pte_present(*ptep)) {
2907 unsigned long pfn = pte_pfn(*ptep);
2908
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002909 if (!is_zero_pfn(pfn))
2910 goto unlock_abort;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002911 flush = true;
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002912 } else if (!pte_none(*ptep))
2913 goto unlock_abort;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002914
2915 /*
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002916 * Check for userfaultfd but do not deliver the fault. Instead,
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002917 * just back off.
2918 */
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002919 if (userfaultfd_missing(vma))
2920 goto unlock_abort;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002921
2922 inc_mm_counter(mm, MM_ANONPAGES);
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07002923 page_add_new_anon_rmap(page, vma, addr, false);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002924 if (!is_zone_device_page(page))
Joonsoo Kimb5181542020-08-11 18:30:40 -07002925 lru_cache_add_inactive_or_unevictable(page, vma);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002926 get_page(page);
2927
2928 if (flush) {
2929 flush_cache_page(vma, addr, pte_pfn(*ptep));
2930 ptep_clear_flush_notify(vma, addr, ptep);
2931 set_pte_at_notify(mm, addr, ptep, entry);
2932 update_mmu_cache(vma, addr, ptep);
2933 } else {
2934 /* No need to invalidate - it was non-present before */
2935 set_pte_at(mm, addr, ptep, entry);
2936 update_mmu_cache(vma, addr, ptep);
2937 }
2938
2939 pte_unmap_unlock(ptep, ptl);
2940 *src = MIGRATE_PFN_MIGRATE;
2941 return;
2942
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002943unlock_abort:
2944 pte_unmap_unlock(ptep, ptl);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002945abort:
2946 *src &= ~MIGRATE_PFN_MIGRATE;
2947}
2948
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002949/**
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002950 * migrate_vma_pages() - migrate meta-data from src page to dst page
2951 * @migrate: migrate struct containing all migration information
2952 *
2953 * This migrates struct page meta-data from source struct page to destination
2954 * struct page. This effectively finishes the migration from source page to the
2955 * destination page.
2956 */
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002957void migrate_vma_pages(struct migrate_vma *migrate)
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002958{
2959 const unsigned long npages = migrate->npages;
2960 const unsigned long start = migrate->start;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002961 struct mmu_notifier_range range;
2962 unsigned long addr, i;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002963 bool notified = false;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002964
2965 for (i = 0, addr = start; i < npages; addr += PAGE_SIZE, i++) {
2966 struct page *newpage = migrate_pfn_to_page(migrate->dst[i]);
2967 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2968 struct address_space *mapping;
2969 int r;
2970
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002971 if (!newpage) {
2972 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002973 continue;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002974 }
2975
2976 if (!page) {
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002977 if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002978 continue;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002979 if (!notified) {
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002980 notified = true;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002981
Alistair Popple6b49bf62021-06-30 18:54:19 -07002982 mmu_notifier_range_init_owner(&range,
2983 MMU_NOTIFY_MIGRATE, 0, migrate->vma,
2984 migrate->vma->vm_mm, addr, migrate->end,
Ralph Campbell5e5dda82020-12-14 19:12:55 -08002985 migrate->pgmap_owner);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002986 mmu_notifier_invalidate_range_start(&range);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002987 }
2988 migrate_vma_insert_page(migrate, addr, newpage,
Stephen Zhangd85c6db2020-12-14 19:13:20 -08002989 &migrate->src[i]);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002990 continue;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002991 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002992
2993 mapping = page_mapping(page);
2994
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002995 if (is_zone_device_page(newpage)) {
2996 if (is_device_private_page(newpage)) {
2997 /*
2998 * For now only support private anonymous when
2999 * migrating to un-addressable device memory.
3000 */
3001 if (mapping) {
3002 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
3003 continue;
3004 }
Christoph Hellwig25b29952019-06-13 22:50:49 +02003005 } else {
Jérôme Glissea5430dd2017-09-08 16:12:17 -07003006 /*
3007 * Other types of ZONE_DEVICE page are not
3008 * supported.
3009 */
3010 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
3011 continue;
3012 }
3013 }
3014
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003015 r = migrate_page(mapping, newpage, page, MIGRATE_SYNC_NO_COPY);
3016 if (r != MIGRATEPAGE_SUCCESS)
3017 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
3018 }
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003019
Jérôme Glisse4645b9f2017-11-15 17:34:11 -08003020 /*
3021 * No need to double call mmu_notifier->invalidate_range() callback as
3022 * the above ptep_clear_flush_notify() inside migrate_vma_insert_page()
3023 * did already call it.
3024 */
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003025 if (notified)
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003026 mmu_notifier_invalidate_range_only_end(&range);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003027}
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003028EXPORT_SYMBOL(migrate_vma_pages);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003029
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003030/**
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003031 * migrate_vma_finalize() - restore CPU page table entry
3032 * @migrate: migrate struct containing all migration information
3033 *
3034 * This replaces the special migration pte entry with either a mapping to the
3035 * new page if migration was successful for that page, or to the original page
3036 * otherwise.
3037 *
3038 * This also unlocks the pages and puts them back on the lru, or drops the extra
3039 * refcount, for device pages.
3040 */
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003041void migrate_vma_finalize(struct migrate_vma *migrate)
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003042{
3043 const unsigned long npages = migrate->npages;
3044 unsigned long i;
3045
3046 for (i = 0; i < npages; i++) {
3047 struct page *newpage = migrate_pfn_to_page(migrate->dst[i]);
3048 struct page *page = migrate_pfn_to_page(migrate->src[i]);
3049
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003050 if (!page) {
3051 if (newpage) {
3052 unlock_page(newpage);
3053 put_page(newpage);
3054 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003055 continue;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003056 }
3057
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003058 if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE) || !newpage) {
3059 if (newpage) {
3060 unlock_page(newpage);
3061 put_page(newpage);
3062 }
3063 newpage = page;
3064 }
3065
3066 remove_migration_ptes(page, newpage, false);
3067 unlock_page(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003068
Jérôme Glissea5430dd2017-09-08 16:12:17 -07003069 if (is_zone_device_page(page))
3070 put_page(page);
3071 else
3072 putback_lru_page(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003073
3074 if (newpage != page) {
3075 unlock_page(newpage);
Jérôme Glissea5430dd2017-09-08 16:12:17 -07003076 if (is_zone_device_page(newpage))
3077 put_page(newpage);
3078 else
3079 putback_lru_page(newpage);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003080 }
3081 }
3082}
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003083EXPORT_SYMBOL(migrate_vma_finalize);
Christoph Hellwig9b2ed9c2019-08-14 09:59:28 +02003084#endif /* CONFIG_DEVICE_PRIVATE */
Dave Hansen79c28a42021-09-02 14:59:06 -07003085
Dave Hansen76af6a02021-10-18 15:15:32 -07003086#if defined(CONFIG_HOTPLUG_CPU)
Dave Hansen79c28a42021-09-02 14:59:06 -07003087/* Disable reclaim-based migration. */
3088static void __disable_all_migrate_targets(void)
3089{
3090 int node;
3091
3092 for_each_online_node(node)
3093 node_demotion[node] = NUMA_NO_NODE;
3094}
3095
3096static void disable_all_migrate_targets(void)
3097{
3098 __disable_all_migrate_targets();
3099
3100 /*
3101 * Ensure that the "disable" is visible across the system.
3102 * Readers will see either a combination of before+disable
3103 * state or disable+after. They will never see before and
3104 * after state together.
3105 *
3106 * The before+after state together might have cycles and
3107 * could cause readers to do things like loop until this
3108 * function finishes. This ensures they can only see a
3109 * single "bad" read and would, for instance, only loop
3110 * once.
3111 */
3112 synchronize_rcu();
3113}
3114
3115/*
3116 * Find an automatic demotion target for 'node'.
3117 * Failing here is OK. It might just indicate
3118 * being at the end of a chain.
3119 */
3120static int establish_migrate_target(int node, nodemask_t *used)
3121{
3122 int migration_target;
3123
3124 /*
3125 * Can not set a migration target on a
3126 * node with it already set.
3127 *
3128 * No need for READ_ONCE() here since this
3129 * in the write path for node_demotion[].
3130 * This should be the only thread writing.
3131 */
3132 if (node_demotion[node] != NUMA_NO_NODE)
3133 return NUMA_NO_NODE;
3134
3135 migration_target = find_next_best_node(node, used);
3136 if (migration_target == NUMA_NO_NODE)
3137 return NUMA_NO_NODE;
3138
3139 node_demotion[node] = migration_target;
3140
3141 return migration_target;
3142}
3143
3144/*
3145 * When memory fills up on a node, memory contents can be
3146 * automatically migrated to another node instead of
3147 * discarded at reclaim.
3148 *
3149 * Establish a "migration path" which will start at nodes
3150 * with CPUs and will follow the priorities used to build the
3151 * page allocator zonelists.
3152 *
3153 * The difference here is that cycles must be avoided. If
3154 * node0 migrates to node1, then neither node1, nor anything
3155 * node1 migrates to can migrate to node0.
3156 *
3157 * This function can run simultaneously with readers of
3158 * node_demotion[]. However, it can not run simultaneously
3159 * with itself. Exclusion is provided by memory hotplug events
3160 * being single-threaded.
3161 */
3162static void __set_migration_target_nodes(void)
3163{
3164 nodemask_t next_pass = NODE_MASK_NONE;
3165 nodemask_t this_pass = NODE_MASK_NONE;
3166 nodemask_t used_targets = NODE_MASK_NONE;
3167 int node;
3168
3169 /*
3170 * Avoid any oddities like cycles that could occur
3171 * from changes in the topology. This will leave
3172 * a momentary gap when migration is disabled.
3173 */
3174 disable_all_migrate_targets();
3175
3176 /*
3177 * Allocations go close to CPUs, first. Assume that
3178 * the migration path starts at the nodes with CPUs.
3179 */
3180 next_pass = node_states[N_CPU];
3181again:
3182 this_pass = next_pass;
3183 next_pass = NODE_MASK_NONE;
3184 /*
3185 * To avoid cycles in the migration "graph", ensure
3186 * that migration sources are not future targets by
3187 * setting them in 'used_targets'. Do this only
3188 * once per pass so that multiple source nodes can
3189 * share a target node.
3190 *
3191 * 'used_targets' will become unavailable in future
3192 * passes. This limits some opportunities for
3193 * multiple source nodes to share a destination.
3194 */
3195 nodes_or(used_targets, used_targets, this_pass);
3196 for_each_node_mask(node, this_pass) {
3197 int target_node = establish_migrate_target(node, &used_targets);
3198
3199 if (target_node == NUMA_NO_NODE)
3200 continue;
3201
3202 /*
3203 * Visit targets from this pass in the next pass.
3204 * Eventually, every node will have been part of
3205 * a pass, and will become set in 'used_targets'.
3206 */
3207 node_set(target_node, next_pass);
3208 }
3209 /*
3210 * 'next_pass' contains nodes which became migration
3211 * targets in this pass. Make additional passes until
3212 * no more migrations targets are available.
3213 */
3214 if (!nodes_empty(next_pass))
3215 goto again;
3216}
3217
3218/*
3219 * For callers that do not hold get_online_mems() already.
3220 */
Dave Hansen79c28a42021-09-02 14:59:06 -07003221static void set_migration_target_nodes(void)
3222{
3223 get_online_mems();
3224 __set_migration_target_nodes();
3225 put_online_mems();
3226}
Dave Hansen884a6e52021-09-02 14:59:09 -07003227
3228/*
Dave Hansen884a6e52021-09-02 14:59:09 -07003229 * This leaves migrate-on-reclaim transiently disabled between
3230 * the MEM_GOING_OFFLINE and MEM_OFFLINE events. This runs
3231 * whether reclaim-based migration is enabled or not, which
3232 * ensures that the user can turn reclaim-based migration at
3233 * any time without needing to recalculate migration targets.
3234 *
3235 * These callbacks already hold get_online_mems(). That is why
3236 * __set_migration_target_nodes() can be used as opposed to
3237 * set_migration_target_nodes().
3238 */
3239static int __meminit migrate_on_reclaim_callback(struct notifier_block *self,
Dave Hansen295be912021-10-18 15:15:29 -07003240 unsigned long action, void *_arg)
Dave Hansen884a6e52021-09-02 14:59:09 -07003241{
Dave Hansen295be912021-10-18 15:15:29 -07003242 struct memory_notify *arg = _arg;
3243
3244 /*
3245 * Only update the node migration order when a node is
3246 * changing status, like online->offline. This avoids
3247 * the overhead of synchronize_rcu() in most cases.
3248 */
3249 if (arg->status_change_nid < 0)
3250 return notifier_from_errno(0);
3251
Dave Hansen884a6e52021-09-02 14:59:09 -07003252 switch (action) {
3253 case MEM_GOING_OFFLINE:
3254 /*
3255 * Make sure there are not transient states where
3256 * an offline node is a migration target. This
3257 * will leave migration disabled until the offline
3258 * completes and the MEM_OFFLINE case below runs.
3259 */
3260 disable_all_migrate_targets();
3261 break;
3262 case MEM_OFFLINE:
3263 case MEM_ONLINE:
3264 /*
3265 * Recalculate the target nodes once the node
3266 * reaches its final state (online or offline).
3267 */
3268 __set_migration_target_nodes();
3269 break;
3270 case MEM_CANCEL_OFFLINE:
3271 /*
3272 * MEM_GOING_OFFLINE disabled all the migration
3273 * targets. Reenable them.
3274 */
3275 __set_migration_target_nodes();
3276 break;
3277 case MEM_GOING_ONLINE:
3278 case MEM_CANCEL_ONLINE:
3279 break;
3280 }
3281
3282 return notifier_from_errno(0);
3283}
3284
Dave Hansen76af6a02021-10-18 15:15:32 -07003285/*
3286 * React to hotplug events that might affect the migration targets
3287 * like events that online or offline NUMA nodes.
3288 *
3289 * The ordering is also currently dependent on which nodes have
3290 * CPUs. That means we need CPU on/offline notification too.
3291 */
3292static int migration_online_cpu(unsigned int cpu)
3293{
3294 set_migration_target_nodes();
3295 return 0;
3296}
3297
3298static int migration_offline_cpu(unsigned int cpu)
3299{
3300 set_migration_target_nodes();
3301 return 0;
3302}
3303
Dave Hansen884a6e52021-09-02 14:59:09 -07003304static int __init migrate_on_reclaim_init(void)
3305{
3306 int ret;
3307
Huang Yinga6a02512021-10-18 15:15:35 -07003308 ret = cpuhp_setup_state_nocalls(CPUHP_MM_DEMOTION_DEAD, "mm/demotion:offline",
3309 NULL, migration_offline_cpu);
Dave Hansen884a6e52021-09-02 14:59:09 -07003310 /*
3311 * In the unlikely case that this fails, the automatic
3312 * migration targets may become suboptimal for nodes
3313 * where N_CPU changes. With such a small impact in a
3314 * rare case, do not bother trying to do anything special.
3315 */
3316 WARN_ON(ret < 0);
Huang Yinga6a02512021-10-18 15:15:35 -07003317 ret = cpuhp_setup_state(CPUHP_AP_MM_DEMOTION_ONLINE, "mm/demotion:online",
3318 migration_online_cpu, NULL);
3319 WARN_ON(ret < 0);
Dave Hansen884a6e52021-09-02 14:59:09 -07003320
3321 hotplug_memory_notifier(migrate_on_reclaim_callback, 100);
3322 return 0;
3323}
3324late_initcall(migrate_on_reclaim_init);
Dave Hansen76af6a02021-10-18 15:15:32 -07003325#endif /* CONFIG_HOTPLUG_CPU */