blob: b5fbeb4bf49a8142db86254bcc395665dbc0e3df [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>
Christoph Lameterb20a3502006-03-22 00:09:12 -080052
Michal Nazarewicz0d1836c2010-12-21 17:24:26 -080053#include <asm/tlbflush.h>
54
Mel Gorman7b2a2d42012-10-19 14:07:31 +010055#define CREATE_TRACE_POINTS
56#include <trace/events/migrate.h>
57
Christoph Lameterb20a3502006-03-22 00:09:12 -080058#include "internal.h"
59
Yisheng Xie9e5bcd62017-02-24 14:57:29 -080060int isolate_movable_page(struct page *page, isolate_mode_t mode)
Minchan Kimbda807d2016-07-26 15:23:05 -070061{
62 struct address_space *mapping;
63
64 /*
65 * Avoid burning cycles with pages that are yet under __free_pages(),
66 * or just got freed under us.
67 *
68 * In case we 'win' a race for a movable page being freed under us and
69 * raise its refcount preventing __free_pages() from doing its job
70 * the put_page() at the end of this block will take care of
71 * release this page, thus avoiding a nasty leakage.
72 */
73 if (unlikely(!get_page_unless_zero(page)))
74 goto out;
75
76 /*
77 * Check PageMovable before holding a PG_lock because page's owner
78 * assumes anybody doesn't touch PG_lock of newly allocated page
Wei Yang8bb4e7a2019-03-05 15:46:22 -080079 * so unconditionally grabbing the lock ruins page's owner side.
Minchan Kimbda807d2016-07-26 15:23:05 -070080 */
81 if (unlikely(!__PageMovable(page)))
82 goto out_putpage;
83 /*
84 * As movable pages are not isolated from LRU lists, concurrent
85 * compaction threads can race against page migration functions
86 * as well as race against the releasing a page.
87 *
88 * In order to avoid having an already isolated movable page
89 * being (wrongly) re-isolated while it is under migration,
90 * or to avoid attempting to isolate pages being released,
91 * lets be sure we have the page lock
92 * before proceeding with the movable page isolation steps.
93 */
94 if (unlikely(!trylock_page(page)))
95 goto out_putpage;
96
97 if (!PageMovable(page) || PageIsolated(page))
98 goto out_no_isolated;
99
100 mapping = page_mapping(page);
101 VM_BUG_ON_PAGE(!mapping, page);
102
103 if (!mapping->a_ops->isolate_page(page, mode))
104 goto out_no_isolated;
105
106 /* Driver shouldn't use PG_isolated bit of page->flags */
107 WARN_ON_ONCE(PageIsolated(page));
108 __SetPageIsolated(page);
109 unlock_page(page);
110
Yisheng Xie9e5bcd62017-02-24 14:57:29 -0800111 return 0;
Minchan Kimbda807d2016-07-26 15:23:05 -0700112
113out_no_isolated:
114 unlock_page(page);
115out_putpage:
116 put_page(page);
117out:
Yisheng Xie9e5bcd62017-02-24 14:57:29 -0800118 return -EBUSY;
Minchan Kimbda807d2016-07-26 15:23:05 -0700119}
120
121/* It should be called on page which is PG_movable */
122void putback_movable_page(struct page *page)
123{
124 struct address_space *mapping;
125
126 VM_BUG_ON_PAGE(!PageLocked(page), page);
127 VM_BUG_ON_PAGE(!PageMovable(page), page);
128 VM_BUG_ON_PAGE(!PageIsolated(page), page);
129
130 mapping = page_mapping(page);
131 mapping->a_ops->putback_page(page);
132 __ClearPageIsolated(page);
133}
134
Christoph Lameterb20a3502006-03-22 00:09:12 -0800135/*
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800136 * Put previously isolated pages back onto the appropriate lists
137 * from where they were once taken off for compaction/migration.
138 *
Joonsoo Kim59c82b72014-01-21 15:51:17 -0800139 * This function shall be used whenever the isolated pageset has been
140 * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range()
141 * and isolate_huge_page().
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800142 */
143void putback_movable_pages(struct list_head *l)
144{
145 struct page *page;
146 struct page *page2;
147
148 list_for_each_entry_safe(page, page2, l, lru) {
Naoya Horiguchi31caf662013-09-11 14:21:59 -0700149 if (unlikely(PageHuge(page))) {
150 putback_active_hugepage(page);
151 continue;
152 }
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800153 list_del(&page->lru);
Minchan Kimbda807d2016-07-26 15:23:05 -0700154 /*
155 * We isolated non-lru movable page so here we can use
156 * __PageMovable because LRU page's mapping cannot have
157 * PAGE_MAPPING_MOVABLE.
158 */
Minchan Kimb1123ea62016-07-26 15:23:09 -0700159 if (unlikely(__PageMovable(page))) {
Minchan Kimbda807d2016-07-26 15:23:05 -0700160 VM_BUG_ON_PAGE(!PageIsolated(page), page);
161 lock_page(page);
162 if (PageMovable(page))
163 putback_movable_page(page);
164 else
165 __ClearPageIsolated(page);
166 unlock_page(page);
167 put_page(page);
168 } else {
Naoya Horiguchie8db67e2017-09-08 16:11:12 -0700169 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -0700170 page_is_file_lru(page), -thp_nr_pages(page));
Rabin Vincentfc280fe2017-04-20 14:37:46 -0700171 putback_lru_page(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700172 }
Christoph Lameterb20a3502006-03-22 00:09:12 -0800173 }
Christoph Lameterb20a3502006-03-22 00:09:12 -0800174}
175
Christoph Lameter06972122006-06-23 02:03:35 -0700176/*
177 * Restore a potential migration pte to a working pte entry
178 */
Minchan Kime4b82222017-05-03 14:54:27 -0700179static bool remove_migration_pte(struct page *page, struct vm_area_struct *vma,
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800180 unsigned long addr, void *old)
Christoph Lameter06972122006-06-23 02:03:35 -0700181{
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800182 struct page_vma_mapped_walk pvmw = {
183 .page = old,
184 .vma = vma,
185 .address = addr,
186 .flags = PVMW_SYNC | PVMW_MIGRATION,
187 };
188 struct page *new;
189 pte_t pte;
Christoph Lameter06972122006-06-23 02:03:35 -0700190 swp_entry_t entry;
Christoph Lameter06972122006-06-23 02:03:35 -0700191
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800192 VM_BUG_ON_PAGE(PageTail(page), page);
193 while (page_vma_mapped_walk(&pvmw)) {
Naoya Horiguchi4b0ece62017-03-31 15:11:44 -0700194 if (PageKsm(page))
195 new = page;
196 else
197 new = page - pvmw.page->index +
198 linear_page_index(vma, pvmw.address);
Christoph Lameter06972122006-06-23 02:03:35 -0700199
Zi Yan616b8372017-09-08 16:10:57 -0700200#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
201 /* PMD-mapped THP migration entry */
202 if (!pvmw.pte) {
203 VM_BUG_ON_PAGE(PageHuge(page) || !PageTransCompound(page), page);
204 remove_migration_pmd(&pvmw, new);
205 continue;
206 }
207#endif
208
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800209 get_page(new);
210 pte = pte_mkold(mk_pte(new, READ_ONCE(vma->vm_page_prot)));
211 if (pte_swp_soft_dirty(*pvmw.pte))
212 pte = pte_mksoft_dirty(pte);
Christoph Lameter06972122006-06-23 02:03:35 -0700213
Hugh Dickins486cf462011-10-19 12:50:35 -0700214 /*
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800215 * Recheck VMA as permissions can change since migration started
Hugh Dickins486cf462011-10-19 12:50:35 -0700216 */
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800217 entry = pte_to_swp_entry(*pvmw.pte);
218 if (is_write_migration_entry(entry))
219 pte = maybe_mkwrite(pte, vma);
Peter Xuf45ec5f2020-04-06 20:06:01 -0700220 else if (pte_swp_uffd_wp(*pvmw.pte))
221 pte = pte_mkuffd_wp(pte);
Mel Gormand3cb8bf2014-10-02 19:47:41 +0100222
Ralph Campbell61287632020-09-04 16:36:04 -0700223 if (unlikely(is_device_private_page(new))) {
224 entry = make_device_private_entry(new, pte_write(pte));
225 pte = swp_entry_to_pte(entry);
Ralph Campbell3d321bf82020-09-04 16:36:07 -0700226 if (pte_swp_soft_dirty(*pvmw.pte))
227 pte = pte_swp_mksoft_dirty(pte);
Ralph Campbell61287632020-09-04 16:36:04 -0700228 if (pte_swp_uffd_wp(*pvmw.pte))
229 pte = pte_swp_mkuffd_wp(pte);
Lars Perssond2b2c6dd2019-03-28 20:44:28 -0700230 }
Jérôme Glissea5430dd2017-09-08 16:12:17 -0700231
Andi Kleen3ef8fd72010-10-11 16:03:21 +0200232#ifdef CONFIG_HUGETLB_PAGE
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800233 if (PageHuge(new)) {
234 pte = pte_mkhuge(pte);
235 pte = arch_make_huge_pte(pte, vma, new, 0);
Aneesh Kumar K.V383321a2017-07-06 15:38:41 -0700236 set_huge_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800237 if (PageAnon(new))
238 hugepage_add_anon_rmap(new, vma, pvmw.address);
239 else
240 page_dup_rmap(new, true);
Aneesh Kumar K.V383321a2017-07-06 15:38:41 -0700241 } else
242#endif
243 {
244 set_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700245
Aneesh Kumar K.V383321a2017-07-06 15:38:41 -0700246 if (PageAnon(new))
247 page_add_anon_rmap(new, vma, pvmw.address, false);
248 else
249 page_add_file_rmap(new, false);
250 }
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800251 if (vma->vm_flags & VM_LOCKED && !PageTransCompound(new))
252 mlock_vma_page(new);
Hugh Dickins51afb122015-11-05 18:49:37 -0800253
Kirill A. Shutemove125fe42018-10-05 15:51:41 -0700254 if (PageTransHuge(page) && PageMlocked(page))
255 clear_page_mlock(page);
256
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800257 /* No need to invalidate - it was non-present before */
258 update_mmu_cache(vma, pvmw.address, pvmw.pte);
259 }
260
Minchan Kime4b82222017-05-03 14:54:27 -0700261 return true;
Christoph Lameter06972122006-06-23 02:03:35 -0700262}
263
264/*
Christoph Lameter04e62a22006-06-23 02:03:38 -0700265 * Get rid of all migration entries and replace them by
266 * references to the indicated page.
267 */
Kirill A. Shutemove3884662016-03-17 14:20:07 -0700268void remove_migration_ptes(struct page *old, struct page *new, bool locked)
Christoph Lameter04e62a22006-06-23 02:03:38 -0700269{
Joonsoo Kim051ac832014-01-21 15:49:48 -0800270 struct rmap_walk_control rwc = {
271 .rmap_one = remove_migration_pte,
272 .arg = old,
273 };
274
Kirill A. Shutemove3884662016-03-17 14:20:07 -0700275 if (locked)
276 rmap_walk_locked(new, &rwc);
277 else
278 rmap_walk(new, &rwc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700279}
280
281/*
Christoph Lameter06972122006-06-23 02:03:35 -0700282 * Something used the pte of a page under migration. We need to
283 * get to the page and wait until migration is finished.
284 * When we return from this function the fault will be retried.
Christoph Lameter06972122006-06-23 02:03:35 -0700285 */
Naoya Horiguchie66f17f2015-02-11 15:25:22 -0800286void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700287 spinlock_t *ptl)
Christoph Lameter06972122006-06-23 02:03:35 -0700288{
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700289 pte_t pte;
Christoph Lameter06972122006-06-23 02:03:35 -0700290 swp_entry_t entry;
291 struct page *page;
292
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700293 spin_lock(ptl);
Christoph Lameter06972122006-06-23 02:03:35 -0700294 pte = *ptep;
295 if (!is_swap_pte(pte))
296 goto out;
297
298 entry = pte_to_swp_entry(pte);
299 if (!is_migration_entry(entry))
300 goto out;
301
302 page = migration_entry_to_page(entry);
303
Nick Piggine2867812008-07-25 19:45:30 -0700304 /*
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500305 * Once page cache replacement of page migration started, page_count
Hugh Dickins9a1ea432018-12-28 00:36:14 -0800306 * is zero; but we must not call put_and_wait_on_page_locked() without
307 * a ref. Use get_page_unless_zero(), and just fault again if it fails.
Nick Piggine2867812008-07-25 19:45:30 -0700308 */
309 if (!get_page_unless_zero(page))
310 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700311 pte_unmap_unlock(ptep, ptl);
Matthew Wilcox (Oracle)48054622021-02-24 12:02:02 -0800312 put_and_wait_on_page_locked(page, TASK_UNINTERRUPTIBLE);
Christoph Lameter06972122006-06-23 02:03:35 -0700313 return;
314out:
315 pte_unmap_unlock(ptep, ptl);
316}
317
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700318void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
319 unsigned long address)
320{
321 spinlock_t *ptl = pte_lockptr(mm, pmd);
322 pte_t *ptep = pte_offset_map(pmd, address);
323 __migration_entry_wait(mm, ptep, ptl);
324}
325
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -0800326void migration_entry_wait_huge(struct vm_area_struct *vma,
327 struct mm_struct *mm, pte_t *pte)
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700328{
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -0800329 spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte);
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700330 __migration_entry_wait(mm, pte, ptl);
331}
332
Zi Yan616b8372017-09-08 16:10:57 -0700333#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
334void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd)
335{
336 spinlock_t *ptl;
337 struct page *page;
338
339 ptl = pmd_lock(mm, pmd);
340 if (!is_pmd_migration_entry(*pmd))
341 goto unlock;
342 page = migration_entry_to_page(pmd_to_swp_entry(*pmd));
343 if (!get_page_unless_zero(page))
344 goto unlock;
345 spin_unlock(ptl);
Matthew Wilcox (Oracle)48054622021-02-24 12:02:02 -0800346 put_and_wait_on_page_locked(page, TASK_UNINTERRUPTIBLE);
Zi Yan616b8372017-09-08 16:10:57 -0700347 return;
348unlock:
349 spin_unlock(ptl);
350}
351#endif
352
Jan Karaf9004822019-03-05 15:48:46 -0800353static int expected_page_refs(struct address_space *mapping, struct page *page)
Jan Kara0b3901b2018-12-28 00:39:01 -0800354{
355 int expected_count = 1;
356
357 /*
Ralph Campbellf1f4f3a2020-10-13 16:58:42 -0700358 * Device private pages have an extra refcount as they are
Jan Kara0b3901b2018-12-28 00:39:01 -0800359 * ZONE_DEVICE pages.
360 */
361 expected_count += is_device_private_page(page);
Jan Karaf9004822019-03-05 15:48:46 -0800362 if (mapping)
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -0700363 expected_count += thp_nr_pages(page) + page_has_private(page);
Jan Kara0b3901b2018-12-28 00:39:01 -0800364
365 return expected_count;
366}
367
Christoph Lameterb20a3502006-03-22 00:09:12 -0800368/*
Christoph Lameterc3fcf8a2006-06-23 02:03:32 -0700369 * Replace the page in the mapping.
Christoph Lameter5b5c7122006-06-23 02:03:29 -0700370 *
371 * The number of remaining references must be:
372 * 1 for anonymous pages without a mapping
373 * 2 for pages with a mapping
David Howells266cf652009-04-03 16:42:36 +0100374 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800375 */
Gu Zheng36bc08c2013-07-16 17:56:16 +0800376int migrate_page_move_mapping(struct address_space *mapping,
Keith Busch37109692019-07-18 15:58:46 -0700377 struct page *newpage, struct page *page, int extra_count)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800378{
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500379 XA_STATE(xas, &mapping->i_pages, page_index(page));
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800380 struct zone *oldzone, *newzone;
381 int dirty;
Jan Karaf9004822019-03-05 15:48:46 -0800382 int expected_count = expected_page_refs(mapping, page) + extra_count;
Shakeel Butt5c447d22021-01-23 21:01:15 -0800383 int nr = thp_nr_pages(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -0700384
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700385 if (!mapping) {
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700386 /* Anonymous page without mapping */
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500387 if (page_count(page) != expected_count)
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700388 return -EAGAIN;
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800389
390 /* No turning back from here */
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800391 newpage->index = page->index;
392 newpage->mapping = page->mapping;
393 if (PageSwapBacked(page))
Hugh Dickinsfa9949d2016-05-19 17:12:41 -0700394 __SetPageSwapBacked(newpage);
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800395
Rafael Aquini78bd5202012-12-11 16:02:31 -0800396 return MIGRATEPAGE_SUCCESS;
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700397 }
398
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800399 oldzone = page_zone(page);
400 newzone = page_zone(newpage);
401
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500402 xas_lock_irq(&xas);
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500403 if (page_count(page) != expected_count || xas_load(&xas) != page) {
404 xas_unlock_irq(&xas);
Christoph Lametere23ca002006-04-10 22:52:57 -0700405 return -EAGAIN;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800406 }
407
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700408 if (!page_ref_freeze(page, expected_count)) {
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500409 xas_unlock_irq(&xas);
Nick Piggine2867812008-07-25 19:45:30 -0700410 return -EAGAIN;
411 }
412
Christoph Lameterb20a3502006-03-22 00:09:12 -0800413 /*
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800414 * Now we know that no one else is looking at the page:
415 * no turning back from here.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800416 */
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800417 newpage->index = page->index;
418 newpage->mapping = page->mapping;
Shakeel Butt5c447d22021-01-23 21:01:15 -0800419 page_ref_add(newpage, nr); /* add cache reference */
Nicholas Piggin6326fec2016-12-25 13:00:29 +1000420 if (PageSwapBacked(page)) {
421 __SetPageSwapBacked(newpage);
422 if (PageSwapCache(page)) {
423 SetPageSwapCache(newpage);
424 set_page_private(newpage, page_private(page));
425 }
426 } else {
427 VM_BUG_ON_PAGE(PageSwapCache(page), page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800428 }
429
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800430 /* Move dirty while page refs frozen and newpage not yet exposed */
431 dirty = PageDirty(page);
432 if (dirty) {
433 ClearPageDirty(page);
434 SetPageDirty(newpage);
435 }
436
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500437 xas_store(&xas, newpage);
Naoya Horiguchie71769a2018-04-20 14:55:45 -0700438 if (PageTransHuge(page)) {
439 int i;
Naoya Horiguchie71769a2018-04-20 14:55:45 -0700440
Shakeel Butt5c447d22021-01-23 21:01:15 -0800441 for (i = 1; i < nr; i++) {
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500442 xas_next(&xas);
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700443 xas_store(&xas, newpage);
Naoya Horiguchie71769a2018-04-20 14:55:45 -0700444 }
Naoya Horiguchie71769a2018-04-20 14:55:45 -0700445 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800446
447 /*
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800448 * Drop cache reference from old page by unfreezing
449 * to one less reference.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800450 * We know this isn't the last reference.
451 */
Shakeel Butt5c447d22021-01-23 21:01:15 -0800452 page_ref_unfreeze(page, expected_count - nr);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800453
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500454 xas_unlock(&xas);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800455 /* Leave irq disabled to prevent preemption while updating stats */
456
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700457 /*
458 * If moved to a different zone then also account
459 * the page for that zone. Other VM counters will be
460 * taken care of when we establish references to the
461 * new page and drop references to the old page.
462 *
463 * Note that anonymous pages are accounted for
Mel Gorman4b9d0fa2016-07-28 15:46:17 -0700464 * via NR_FILE_PAGES and NR_ANON_MAPPED if they
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700465 * are mapped to swap space.
466 */
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800467 if (newzone != oldzone) {
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700468 struct lruvec *old_lruvec, *new_lruvec;
469 struct mem_cgroup *memcg;
470
471 memcg = page_memcg(page);
472 old_lruvec = mem_cgroup_lruvec(memcg, oldzone->zone_pgdat);
473 new_lruvec = mem_cgroup_lruvec(memcg, newzone->zone_pgdat);
474
Shakeel Butt5c447d22021-01-23 21:01:15 -0800475 __mod_lruvec_state(old_lruvec, NR_FILE_PAGES, -nr);
476 __mod_lruvec_state(new_lruvec, NR_FILE_PAGES, nr);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800477 if (PageSwapBacked(page) && !PageSwapCache(page)) {
Shakeel Butt5c447d22021-01-23 21:01:15 -0800478 __mod_lruvec_state(old_lruvec, NR_SHMEM, -nr);
479 __mod_lruvec_state(new_lruvec, NR_SHMEM, nr);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800480 }
Shakeel Buttb6038942021-02-24 12:03:55 -0800481#ifdef CONFIG_SWAP
482 if (PageSwapCache(page)) {
483 __mod_lruvec_state(old_lruvec, NR_SWAPCACHE, -nr);
484 __mod_lruvec_state(new_lruvec, NR_SWAPCACHE, nr);
485 }
486#endif
Christoph Hellwigf56753a2020-09-24 08:51:40 +0200487 if (dirty && mapping_can_writeback(mapping)) {
Shakeel Butt5c447d22021-01-23 21:01:15 -0800488 __mod_lruvec_state(old_lruvec, NR_FILE_DIRTY, -nr);
489 __mod_zone_page_state(oldzone, NR_ZONE_WRITE_PENDING, -nr);
490 __mod_lruvec_state(new_lruvec, NR_FILE_DIRTY, nr);
491 __mod_zone_page_state(newzone, NR_ZONE_WRITE_PENDING, nr);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800492 }
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700493 }
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800494 local_irq_enable();
Christoph Lameterb20a3502006-03-22 00:09:12 -0800495
Rafael Aquini78bd5202012-12-11 16:02:31 -0800496 return MIGRATEPAGE_SUCCESS;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800497}
Richard Weinberger1118dce2016-06-16 23:26:14 +0200498EXPORT_SYMBOL(migrate_page_move_mapping);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800499
500/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900501 * The expected number of remaining references is the same as that
502 * of migrate_page_move_mapping().
503 */
504int migrate_huge_page_move_mapping(struct address_space *mapping,
505 struct page *newpage, struct page *page)
506{
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500507 XA_STATE(xas, &mapping->i_pages, page_index(page));
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900508 int expected_count;
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900509
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500510 xas_lock_irq(&xas);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900511 expected_count = 2 + page_has_private(page);
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500512 if (page_count(page) != expected_count || xas_load(&xas) != page) {
513 xas_unlock_irq(&xas);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900514 return -EAGAIN;
515 }
516
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700517 if (!page_ref_freeze(page, expected_count)) {
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500518 xas_unlock_irq(&xas);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900519 return -EAGAIN;
520 }
521
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800522 newpage->index = page->index;
523 newpage->mapping = page->mapping;
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700524
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900525 get_page(newpage);
526
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500527 xas_store(&xas, newpage);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900528
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700529 page_ref_unfreeze(page, expected_count - 1);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900530
Matthew Wilcox89eb9462017-12-04 04:35:16 -0500531 xas_unlock_irq(&xas);
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700532
Rafael Aquini78bd5202012-12-11 16:02:31 -0800533 return MIGRATEPAGE_SUCCESS;
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900534}
535
536/*
Dave Hansen30b0a102013-11-21 14:31:58 -0800537 * Gigantic pages are so large that we do not guarantee that page++ pointer
538 * arithmetic will work across the entire page. We need something more
539 * specialized.
540 */
541static void __copy_gigantic_page(struct page *dst, struct page *src,
542 int nr_pages)
543{
544 int i;
545 struct page *dst_base = dst;
546 struct page *src_base = src;
547
548 for (i = 0; i < nr_pages; ) {
549 cond_resched();
550 copy_highpage(dst, src);
551
552 i++;
553 dst = mem_map_next(dst, dst_base, i);
554 src = mem_map_next(src, src_base, i);
555 }
556}
557
558static void copy_huge_page(struct page *dst, struct page *src)
559{
560 int i;
561 int nr_pages;
562
563 if (PageHuge(src)) {
564 /* hugetlbfs page */
565 struct hstate *h = page_hstate(src);
566 nr_pages = pages_per_huge_page(h);
567
568 if (unlikely(nr_pages > MAX_ORDER_NR_PAGES)) {
569 __copy_gigantic_page(dst, src, nr_pages);
570 return;
571 }
572 } else {
573 /* thp page */
574 BUG_ON(!PageTransHuge(src));
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -0700575 nr_pages = thp_nr_pages(src);
Dave Hansen30b0a102013-11-21 14:31:58 -0800576 }
577
578 for (i = 0; i < nr_pages; i++) {
579 cond_resched();
580 copy_highpage(dst + i, src + i);
581 }
582}
583
584/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800585 * Copy the page to its new location
586 */
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700587void migrate_page_states(struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800588{
Rik van Riel7851a452013-10-07 11:29:23 +0100589 int cpupid;
590
Christoph Lameterb20a3502006-03-22 00:09:12 -0800591 if (PageError(page))
592 SetPageError(newpage);
593 if (PageReferenced(page))
594 SetPageReferenced(newpage);
595 if (PageUptodate(page))
596 SetPageUptodate(newpage);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700597 if (TestClearPageActive(page)) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800598 VM_BUG_ON_PAGE(PageUnevictable(page), page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800599 SetPageActive(newpage);
Lee Schermerhorn418b27e2009-12-14 17:59:54 -0800600 } else if (TestClearPageUnevictable(page))
601 SetPageUnevictable(newpage);
Johannes Weiner1899ad12018-10-26 15:06:04 -0700602 if (PageWorkingset(page))
603 SetPageWorkingset(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800604 if (PageChecked(page))
605 SetPageChecked(newpage);
606 if (PageMappedToDisk(page))
607 SetPageMappedToDisk(newpage);
608
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800609 /* Move dirty on pages not done by migrate_page_move_mapping() */
610 if (PageDirty(page))
611 SetPageDirty(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800612
Vladimir Davydov33c3fc72015-09-09 15:35:45 -0700613 if (page_is_young(page))
614 set_page_young(newpage);
615 if (page_is_idle(page))
616 set_page_idle(newpage);
617
Rik van Riel7851a452013-10-07 11:29:23 +0100618 /*
619 * Copy NUMA information to the new page, to prevent over-eager
620 * future migrations of this same page.
621 */
622 cpupid = page_cpupid_xchg_last(page, -1);
623 page_cpupid_xchg_last(newpage, cpupid);
624
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800625 ksm_migrate_page(newpage, page);
Hugh Dickinsc8d65532013-02-22 16:35:10 -0800626 /*
627 * Please do not reorder this without considering how mm/ksm.c's
628 * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache().
629 */
Naoya Horiguchib3b3a992015-04-15 16:13:15 -0700630 if (PageSwapCache(page))
631 ClearPageSwapCache(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800632 ClearPagePrivate(page);
633 set_page_private(page, 0);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800634
635 /*
636 * If any waiters have accumulated on the new page then
637 * wake them up.
638 */
639 if (PageWriteback(newpage))
640 end_page_writeback(newpage);
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700641
Yang Shi6aeff242020-04-06 20:04:21 -0700642 /*
643 * PG_readahead shares the same bit with PG_reclaim. The above
644 * end_page_writeback() may clear PG_readahead mistakenly, so set the
645 * bit after that.
646 */
647 if (PageReadahead(page))
648 SetPageReadahead(newpage);
649
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700650 copy_page_owner(page, newpage);
Johannes Weiner74485cf2016-03-15 14:57:54 -0700651
Hugh Dickinsa333e3e2020-09-18 21:20:06 -0700652 if (!PageHuge(page))
653 mem_cgroup_migrate(page, newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800654}
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700655EXPORT_SYMBOL(migrate_page_states);
656
657void migrate_page_copy(struct page *newpage, struct page *page)
658{
659 if (PageHuge(page) || PageTransHuge(page))
660 copy_huge_page(newpage, page);
661 else
662 copy_highpage(newpage, page);
663
664 migrate_page_states(newpage, page);
665}
Richard Weinberger1118dce2016-06-16 23:26:14 +0200666EXPORT_SYMBOL(migrate_page_copy);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800667
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700668/************************************************************
669 * Migration functions
670 ***********************************************************/
671
Christoph Lameterb20a3502006-03-22 00:09:12 -0800672/*
Minchan Kimbda807d2016-07-26 15:23:05 -0700673 * Common logic to directly migrate a single LRU page suitable for
David Howells266cf652009-04-03 16:42:36 +0100674 * pages that do not use PagePrivate/PagePrivate2.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800675 *
676 * Pages are locked upon entry and exit.
677 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700678int migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800679 struct page *newpage, struct page *page,
680 enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800681{
682 int rc;
683
684 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
685
Keith Busch37109692019-07-18 15:58:46 -0700686 rc = migrate_page_move_mapping(mapping, newpage, page, 0);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800687
Rafael Aquini78bd5202012-12-11 16:02:31 -0800688 if (rc != MIGRATEPAGE_SUCCESS)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800689 return rc;
690
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700691 if (mode != MIGRATE_SYNC_NO_COPY)
692 migrate_page_copy(newpage, page);
693 else
694 migrate_page_states(newpage, page);
Rafael Aquini78bd5202012-12-11 16:02:31 -0800695 return MIGRATEPAGE_SUCCESS;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800696}
697EXPORT_SYMBOL(migrate_page);
698
David Howells93614012006-09-30 20:45:40 +0200699#ifdef CONFIG_BLOCK
Jan Kara84ade7c2018-12-28 00:39:09 -0800700/* Returns true if all buffers are successfully locked */
701static bool buffer_migrate_lock_buffers(struct buffer_head *head,
702 enum migrate_mode mode)
703{
704 struct buffer_head *bh = head;
705
706 /* Simple case, sync compaction */
707 if (mode != MIGRATE_ASYNC) {
708 do {
Jan Kara84ade7c2018-12-28 00:39:09 -0800709 lock_buffer(bh);
710 bh = bh->b_this_page;
711
712 } while (bh != head);
713
714 return true;
715 }
716
717 /* async case, we cannot block on lock_buffer so use trylock_buffer */
718 do {
Jan Kara84ade7c2018-12-28 00:39:09 -0800719 if (!trylock_buffer(bh)) {
720 /*
721 * We failed to lock the buffer and cannot stall in
722 * async migration. Release the taken locks
723 */
724 struct buffer_head *failed_bh = bh;
Jan Kara84ade7c2018-12-28 00:39:09 -0800725 bh = head;
726 while (bh != failed_bh) {
727 unlock_buffer(bh);
Jan Kara84ade7c2018-12-28 00:39:09 -0800728 bh = bh->b_this_page;
729 }
730 return false;
731 }
732
733 bh = bh->b_this_page;
734 } while (bh != head);
735 return true;
736}
737
Jan Kara89cb0882018-12-28 00:39:12 -0800738static int __buffer_migrate_page(struct address_space *mapping,
739 struct page *newpage, struct page *page, enum migrate_mode mode,
740 bool check_refs)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700741{
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700742 struct buffer_head *bh, *head;
743 int rc;
Jan Karacc4f11e2018-12-28 00:39:05 -0800744 int expected_count;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700745
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700746 if (!page_has_buffers(page))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800747 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700748
Jan Karacc4f11e2018-12-28 00:39:05 -0800749 /* Check whether page does not have extra refs before we do more work */
Jan Karaf9004822019-03-05 15:48:46 -0800750 expected_count = expected_page_refs(mapping, page);
Jan Karacc4f11e2018-12-28 00:39:05 -0800751 if (page_count(page) != expected_count)
752 return -EAGAIN;
753
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700754 head = page_buffers(page);
Jan Karacc4f11e2018-12-28 00:39:05 -0800755 if (!buffer_migrate_lock_buffers(head, mode))
756 return -EAGAIN;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700757
Jan Kara89cb0882018-12-28 00:39:12 -0800758 if (check_refs) {
759 bool busy;
760 bool invalidated = false;
761
762recheck_buffers:
763 busy = false;
764 spin_lock(&mapping->private_lock);
765 bh = head;
766 do {
767 if (atomic_read(&bh->b_count)) {
768 busy = true;
769 break;
770 }
771 bh = bh->b_this_page;
772 } while (bh != head);
Jan Kara89cb0882018-12-28 00:39:12 -0800773 if (busy) {
774 if (invalidated) {
775 rc = -EAGAIN;
776 goto unlock_buffers;
777 }
Jan Karaebdf4de2019-08-02 21:48:47 -0700778 spin_unlock(&mapping->private_lock);
Jan Kara89cb0882018-12-28 00:39:12 -0800779 invalidate_bh_lrus();
780 invalidated = true;
781 goto recheck_buffers;
782 }
783 }
784
Keith Busch37109692019-07-18 15:58:46 -0700785 rc = migrate_page_move_mapping(mapping, newpage, page, 0);
Rafael Aquini78bd5202012-12-11 16:02:31 -0800786 if (rc != MIGRATEPAGE_SUCCESS)
Jan Karacc4f11e2018-12-28 00:39:05 -0800787 goto unlock_buffers;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700788
Guoqing Jiangcd0f3712020-06-01 21:48:06 -0700789 attach_page_private(newpage, detach_page_private(page));
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700790
791 bh = head;
792 do {
793 set_bh_page(bh, newpage, bh_offset(bh));
794 bh = bh->b_this_page;
795
796 } while (bh != head);
797
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700798 if (mode != MIGRATE_SYNC_NO_COPY)
799 migrate_page_copy(newpage, page);
800 else
801 migrate_page_states(newpage, page);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700802
Jan Karacc4f11e2018-12-28 00:39:05 -0800803 rc = MIGRATEPAGE_SUCCESS;
804unlock_buffers:
Jan Karaebdf4de2019-08-02 21:48:47 -0700805 if (check_refs)
806 spin_unlock(&mapping->private_lock);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700807 bh = head;
808 do {
809 unlock_buffer(bh);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700810 bh = bh->b_this_page;
811
812 } while (bh != head);
813
Jan Karacc4f11e2018-12-28 00:39:05 -0800814 return rc;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700815}
Jan Kara89cb0882018-12-28 00:39:12 -0800816
817/*
818 * Migration function for pages with buffers. This function can only be used
819 * if the underlying filesystem guarantees that no other references to "page"
820 * exist. For example attached buffer heads are accessed only under page lock.
821 */
822int buffer_migrate_page(struct address_space *mapping,
823 struct page *newpage, struct page *page, enum migrate_mode mode)
824{
825 return __buffer_migrate_page(mapping, newpage, page, mode, false);
826}
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700827EXPORT_SYMBOL(buffer_migrate_page);
Jan Kara89cb0882018-12-28 00:39:12 -0800828
829/*
830 * Same as above except that this variant is more careful and checks that there
831 * are also no buffer head references. This function is the right one for
832 * mappings where buffer heads are directly looked up and referenced (such as
833 * block device mappings).
834 */
835int buffer_migrate_page_norefs(struct address_space *mapping,
836 struct page *newpage, struct page *page, enum migrate_mode mode)
837{
838 return __buffer_migrate_page(mapping, newpage, page, mode, true);
839}
David Howells93614012006-09-30 20:45:40 +0200840#endif
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700841
Christoph Lameter04e62a22006-06-23 02:03:38 -0700842/*
843 * Writeback a page to clean the dirty state
844 */
845static int writeout(struct address_space *mapping, struct page *page)
846{
847 struct writeback_control wbc = {
848 .sync_mode = WB_SYNC_NONE,
849 .nr_to_write = 1,
850 .range_start = 0,
851 .range_end = LLONG_MAX,
Christoph Lameter04e62a22006-06-23 02:03:38 -0700852 .for_reclaim = 1
853 };
854 int rc;
855
856 if (!mapping->a_ops->writepage)
857 /* No write method for the address space */
858 return -EINVAL;
859
860 if (!clear_page_dirty_for_io(page))
861 /* Someone else already triggered a write */
862 return -EAGAIN;
863
864 /*
865 * A dirty page may imply that the underlying filesystem has
866 * the page on some queue. So the page must be clean for
867 * migration. Writeout may mean we loose the lock and the
868 * page state is no longer what we checked for earlier.
869 * At this point we know that the migration attempt cannot
870 * be successful.
871 */
Kirill A. Shutemove3884662016-03-17 14:20:07 -0700872 remove_migration_ptes(page, page, false);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700873
874 rc = mapping->a_ops->writepage(page, &wbc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700875
876 if (rc != AOP_WRITEPAGE_ACTIVATE)
877 /* unlocked. Relock */
878 lock_page(page);
879
Hugh Dickinsbda85502008-11-19 15:36:36 -0800880 return (rc < 0) ? -EIO : -EAGAIN;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700881}
882
883/*
884 * Default handling if a filesystem does not provide a migration function.
885 */
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700886static int fallback_migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800887 struct page *newpage, struct page *page, enum migrate_mode mode)
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700888{
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800889 if (PageDirty(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800890 /* Only writeback pages in full synchronous migration */
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700891 switch (mode) {
892 case MIGRATE_SYNC:
893 case MIGRATE_SYNC_NO_COPY:
894 break;
895 default:
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800896 return -EBUSY;
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700897 }
Christoph Lameter04e62a22006-06-23 02:03:38 -0700898 return writeout(mapping, page);
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800899 }
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700900
901 /*
902 * Buffers may be managed in a filesystem specific way.
903 * We must have no buffers or drop them.
904 */
David Howells266cf652009-04-03 16:42:36 +0100905 if (page_has_private(page) &&
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700906 !try_to_release_page(page, GFP_KERNEL))
Mel Gorman806031b2019-03-05 15:44:43 -0800907 return mode == MIGRATE_SYNC ? -EAGAIN : -EBUSY;
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700908
Mel Gormana6bc32b2012-01-12 17:19:43 -0800909 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700910}
911
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700912/*
Christoph Lametere24f0b82006-06-23 02:03:51 -0700913 * Move a page to a newly allocated page
914 * The page is locked and all ptes have been successfully removed.
915 *
916 * The new page will have replaced the old page if this function
917 * is successful.
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700918 *
919 * Return value:
920 * < 0 - error code
Rafael Aquini78bd5202012-12-11 16:02:31 -0800921 * MIGRATEPAGE_SUCCESS - success
Christoph Lametere24f0b82006-06-23 02:03:51 -0700922 */
Mel Gorman3fe20112010-05-24 14:32:20 -0700923static int move_to_new_page(struct page *newpage, struct page *page,
Hugh Dickins5c3f9a62015-11-05 18:49:53 -0800924 enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700925{
926 struct address_space *mapping;
Minchan Kimbda807d2016-07-26 15:23:05 -0700927 int rc = -EAGAIN;
928 bool is_lru = !__PageMovable(page);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700929
Hugh Dickins7db76712015-11-05 18:49:49 -0800930 VM_BUG_ON_PAGE(!PageLocked(page), page);
931 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700932
Christoph Lametere24f0b82006-06-23 02:03:51 -0700933 mapping = page_mapping(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700934
935 if (likely(is_lru)) {
936 if (!mapping)
937 rc = migrate_page(mapping, newpage, page, mode);
938 else if (mapping->a_ops->migratepage)
939 /*
940 * Most pages have a mapping and most filesystems
941 * provide a migratepage callback. Anonymous pages
942 * are part of swap space which also has its own
943 * migratepage callback. This is the most common path
944 * for page migration.
945 */
946 rc = mapping->a_ops->migratepage(mapping, newpage,
947 page, mode);
948 else
949 rc = fallback_migrate_page(mapping, newpage,
950 page, mode);
951 } else {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700952 /*
Minchan Kimbda807d2016-07-26 15:23:05 -0700953 * In case of non-lru page, it could be released after
954 * isolation step. In that case, we shouldn't try migration.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700955 */
Minchan Kimbda807d2016-07-26 15:23:05 -0700956 VM_BUG_ON_PAGE(!PageIsolated(page), page);
957 if (!PageMovable(page)) {
958 rc = MIGRATEPAGE_SUCCESS;
959 __ClearPageIsolated(page);
960 goto out;
961 }
962
963 rc = mapping->a_ops->migratepage(mapping, newpage,
964 page, mode);
965 WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS &&
966 !PageIsolated(page));
967 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700968
Hugh Dickins5c3f9a62015-11-05 18:49:53 -0800969 /*
970 * When successful, old pagecache page->mapping must be cleared before
971 * page is freed; but stats require that PageAnon be left as PageAnon.
972 */
973 if (rc == MIGRATEPAGE_SUCCESS) {
Minchan Kimbda807d2016-07-26 15:23:05 -0700974 if (__PageMovable(page)) {
975 VM_BUG_ON_PAGE(!PageIsolated(page), page);
976
977 /*
978 * We clear PG_movable under page_lock so any compactor
979 * cannot try to migrate this page.
980 */
981 __ClearPageIsolated(page);
982 }
983
984 /*
Ralph Campbellc23a0c92020-01-30 22:14:41 -0800985 * Anonymous and movable page->mapping will be cleared by
Minchan Kimbda807d2016-07-26 15:23:05 -0700986 * free_pages_prepare so don't reset it here for keeping
987 * the type to work PageAnon, for example.
988 */
989 if (!PageMappingFlags(page))
Hugh Dickins5c3f9a62015-11-05 18:49:53 -0800990 page->mapping = NULL;
Lars Perssond2b2c6dd2019-03-28 20:44:28 -0700991
Christoph Hellwig25b29952019-06-13 22:50:49 +0200992 if (likely(!is_zone_device_page(newpage)))
Lars Perssond2b2c6dd2019-03-28 20:44:28 -0700993 flush_dcache_page(newpage);
994
Mel Gorman3fe20112010-05-24 14:32:20 -0700995 }
Minchan Kimbda807d2016-07-26 15:23:05 -0700996out:
Christoph Lametere24f0b82006-06-23 02:03:51 -0700997 return rc;
998}
999
Minchan Kim0dabec92011-10-31 17:06:57 -07001000static int __unmap_and_move(struct page *page, struct page *newpage,
Hugh Dickins9c620e22013-02-22 16:35:14 -08001001 int force, enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -07001002{
Minchan Kim0dabec92011-10-31 17:06:57 -07001003 int rc = -EAGAIN;
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001004 int page_was_mapped = 0;
Mel Gorman3f6c8272010-05-24 14:32:17 -07001005 struct anon_vma *anon_vma = NULL;
Minchan Kimbda807d2016-07-26 15:23:05 -07001006 bool is_lru = !__PageMovable(page);
Christoph Lameter95a402c2006-06-23 02:03:53 -07001007
Nick Piggin529ae9a2008-08-02 12:01:03 +02001008 if (!trylock_page(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -08001009 if (!force || mode == MIGRATE_ASYNC)
Minchan Kim0dabec92011-10-31 17:06:57 -07001010 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -08001011
1012 /*
1013 * It's not safe for direct compaction to call lock_page.
1014 * For example, during page readahead pages are added locked
1015 * to the LRU. Later, when the IO completes the pages are
1016 * marked uptodate and unlocked. However, the queueing
1017 * could be merging multiple pages for one bio (e.g.
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -07001018 * mpage_readahead). If an allocation happens for the
Mel Gorman3e7d3442011-01-13 15:45:56 -08001019 * second or third page, the process can end up locking
1020 * the same page twice and deadlocking. Rather than
1021 * trying to be clever about what pages can be locked,
1022 * avoid the use of lock_page for direct compaction
1023 * altogether.
1024 */
1025 if (current->flags & PF_MEMALLOC)
Minchan Kim0dabec92011-10-31 17:06:57 -07001026 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -08001027
Christoph Lametere24f0b82006-06-23 02:03:51 -07001028 lock_page(page);
1029 }
1030
1031 if (PageWriteback(page)) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -07001032 /*
Jianguo Wufed5b642013-04-29 15:07:58 -07001033 * Only in the case of a full synchronous migration is it
Mel Gormana6bc32b2012-01-12 17:19:43 -08001034 * necessary to wait for PageWriteback. In the async case,
1035 * the retry loop is too short and in the sync-light case,
1036 * the overhead of stalling is too much
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -07001037 */
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07001038 switch (mode) {
1039 case MIGRATE_SYNC:
1040 case MIGRATE_SYNC_NO_COPY:
1041 break;
1042 default:
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -07001043 rc = -EBUSY;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07001044 goto out_unlock;
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -07001045 }
1046 if (!force)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07001047 goto out_unlock;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001048 wait_on_page_writeback(page);
1049 }
Hugh Dickins03f15c82015-11-05 18:49:56 -08001050
Christoph Lametere24f0b82006-06-23 02:03:51 -07001051 /*
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001052 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
1053 * we cannot notice that anon_vma is freed while we migrates a page.
Hugh Dickins1ce82b62011-01-13 15:47:30 -08001054 * This get_anon_vma() delays freeing anon_vma pointer until the end
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001055 * of migration. File cache pages are no problem because of page_lock()
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -07001056 * File Caches may use write_page() or lock_page() in migration, then,
1057 * just care Anon page here.
Hugh Dickins03f15c82015-11-05 18:49:56 -08001058 *
1059 * Only page_get_anon_vma() understands the subtleties of
1060 * getting a hold on an anon_vma from outside one of its mms.
1061 * But if we cannot get anon_vma, then we won't need it anyway,
1062 * because that implies that the anon page is no longer mapped
1063 * (and cannot be remapped so long as we hold the page lock).
Christoph Lametere24f0b82006-06-23 02:03:51 -07001064 */
Hugh Dickins03f15c82015-11-05 18:49:56 -08001065 if (PageAnon(page) && !PageKsm(page))
Peter Zijlstra746b18d2011-05-24 17:12:10 -07001066 anon_vma = page_get_anon_vma(page);
Shaohua Li62e1c552008-02-04 22:29:33 -08001067
Hugh Dickins7db76712015-11-05 18:49:49 -08001068 /*
1069 * Block others from accessing the new page when we get around to
1070 * establishing additional references. We are usually the only one
1071 * holding a reference to newpage at this point. We used to have a BUG
1072 * here if trylock_page(newpage) fails, but would like to allow for
1073 * cases where there might be a race with the previous use of newpage.
1074 * This is much like races on refcount of oldpage: just don't BUG().
1075 */
1076 if (unlikely(!trylock_page(newpage)))
1077 goto out_unlock;
1078
Minchan Kimbda807d2016-07-26 15:23:05 -07001079 if (unlikely(!is_lru)) {
1080 rc = move_to_new_page(newpage, page, mode);
1081 goto out_unlock_both;
1082 }
1083
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001084 /*
Shaohua Li62e1c552008-02-04 22:29:33 -08001085 * Corner case handling:
1086 * 1. When a new swap-cache page is read into, it is added to the LRU
1087 * and treated as swapcache but it has no rmap yet.
1088 * Calling try_to_unmap() against a page->mapping==NULL page will
1089 * trigger a BUG. So handle it here.
Yang Shid12b8952020-12-14 19:13:02 -08001090 * 2. An orphaned page (see truncate_cleanup_page) might have
Shaohua Li62e1c552008-02-04 22:29:33 -08001091 * fs-private metadata. The page can be picked up due to memory
1092 * offlining. Everywhere else except page reclaim, the page is
1093 * invisible to the vm, so the page can not be migrated. So try to
1094 * free the metadata, so the page can be freed.
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001095 */
Shaohua Li62e1c552008-02-04 22:29:33 -08001096 if (!page->mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -08001097 VM_BUG_ON_PAGE(PageAnon(page), page);
Hugh Dickins1ce82b62011-01-13 15:47:30 -08001098 if (page_has_private(page)) {
Shaohua Li62e1c552008-02-04 22:29:33 -08001099 try_to_free_buffers(page);
Hugh Dickins7db76712015-11-05 18:49:49 -08001100 goto out_unlock_both;
Shaohua Li62e1c552008-02-04 22:29:33 -08001101 }
Hugh Dickins7db76712015-11-05 18:49:49 -08001102 } else if (page_mapped(page)) {
1103 /* Establish migration ptes */
Hugh Dickins03f15c82015-11-05 18:49:56 -08001104 VM_BUG_ON_PAGE(PageAnon(page) && !PageKsm(page) && !anon_vma,
1105 page);
Shakeel Butt013339d2020-12-14 19:06:39 -08001106 try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK);
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001107 page_was_mapped = 1;
1108 }
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001109
Christoph Lametere6a15302006-06-25 05:46:49 -07001110 if (!page_mapped(page))
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001111 rc = move_to_new_page(newpage, page, mode);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001112
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001113 if (page_was_mapped)
1114 remove_migration_ptes(page,
Kirill A. Shutemove3884662016-03-17 14:20:07 -07001115 rc == MIGRATEPAGE_SUCCESS ? newpage : page, false);
Mel Gorman3f6c8272010-05-24 14:32:17 -07001116
Hugh Dickins7db76712015-11-05 18:49:49 -08001117out_unlock_both:
1118 unlock_page(newpage);
1119out_unlock:
Mel Gorman3f6c8272010-05-24 14:32:17 -07001120 /* Drop an anon_vma reference if we took one */
Rik van Riel76545062010-08-09 17:18:41 -07001121 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -07001122 put_anon_vma(anon_vma);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001123 unlock_page(page);
Minchan Kim0dabec92011-10-31 17:06:57 -07001124out:
Minchan Kimc6c919e2016-07-26 15:23:02 -07001125 /*
1126 * If migration is successful, decrease refcount of the newpage
1127 * which will not free the page because new page owner increased
1128 * refcounter. As well, if it is LRU page, add the page to LRU
David Hildenbrande0a352f2019-02-01 14:21:19 -08001129 * list in here. Use the old state of the isolated source page to
1130 * determine if we migrated a LRU page. newpage was already unlocked
1131 * and possibly modified by its owner - don't rely on the page
1132 * state.
Minchan Kimc6c919e2016-07-26 15:23:02 -07001133 */
1134 if (rc == MIGRATEPAGE_SUCCESS) {
David Hildenbrande0a352f2019-02-01 14:21:19 -08001135 if (unlikely(!is_lru))
Minchan Kimc6c919e2016-07-26 15:23:02 -07001136 put_page(newpage);
1137 else
1138 putback_lru_page(newpage);
1139 }
1140
Minchan Kim0dabec92011-10-31 17:06:57 -07001141 return rc;
1142}
Christoph Lameter95a402c2006-06-23 02:03:53 -07001143
Minchan Kim0dabec92011-10-31 17:06:57 -07001144/*
1145 * Obtain the lock on page, remove all ptes and migrate the page
1146 * to the newly allocated page in newpage.
1147 */
Linus Torvalds6ec44762020-07-08 10:48:35 -07001148static int unmap_and_move(new_page_t get_new_page,
Geert Uytterhoevenef2a5152015-04-14 15:44:22 -07001149 free_page_t put_new_page,
1150 unsigned long private, struct page *page,
Naoya Horiguchiadd05ce2015-06-24 16:56:50 -07001151 int force, enum migrate_mode mode,
Yang Shidd4ae782020-12-14 19:13:06 -08001152 enum migrate_reason reason,
1153 struct list_head *ret)
Minchan Kim0dabec92011-10-31 17:06:57 -07001154{
Hugh Dickins2def7422015-11-05 18:49:46 -08001155 int rc = MIGRATEPAGE_SUCCESS;
Yang Shi74d4a572019-11-30 17:57:12 -08001156 struct page *newpage = NULL;
Minchan Kim0dabec92011-10-31 17:06:57 -07001157
Michal Hocko94723aa2018-04-10 16:30:07 -07001158 if (!thp_migration_supported() && PageTransHuge(page))
Yang Shid532e2e2020-12-14 19:13:16 -08001159 return -ENOSYS;
Michal Hocko94723aa2018-04-10 16:30:07 -07001160
Minchan Kim0dabec92011-10-31 17:06:57 -07001161 if (page_count(page) == 1) {
1162 /* page was freed from under us. So we are done. */
Minchan Kimc6c919e2016-07-26 15:23:02 -07001163 ClearPageActive(page);
1164 ClearPageUnevictable(page);
Minchan Kimbda807d2016-07-26 15:23:05 -07001165 if (unlikely(__PageMovable(page))) {
1166 lock_page(page);
1167 if (!PageMovable(page))
1168 __ClearPageIsolated(page);
1169 unlock_page(page);
1170 }
Minchan Kim0dabec92011-10-31 17:06:57 -07001171 goto out;
1172 }
1173
Yang Shi74d4a572019-11-30 17:57:12 -08001174 newpage = get_new_page(page, private);
1175 if (!newpage)
1176 return -ENOMEM;
1177
Hugh Dickins9c620e22013-02-22 16:35:14 -08001178 rc = __unmap_and_move(page, newpage, force, mode);
Minchan Kimc6c919e2016-07-26 15:23:02 -07001179 if (rc == MIGRATEPAGE_SUCCESS)
Vlastimil Babka7cd12b42016-03-15 14:56:18 -07001180 set_page_owner_migrate_reason(newpage, reason);
Rafael Aquinibf6bddf12012-12-11 16:02:42 -08001181
Minchan Kim0dabec92011-10-31 17:06:57 -07001182out:
Christoph Lametere24f0b82006-06-23 02:03:51 -07001183 if (rc != -EAGAIN) {
Minchan Kim0dabec92011-10-31 17:06:57 -07001184 /*
1185 * A page that has been migrated has all references
1186 * removed and will be freed. A page that has not been
Ralph Campbellc23a0c92020-01-30 22:14:41 -08001187 * migrated will have kept its references and be restored.
Minchan Kim0dabec92011-10-31 17:06:57 -07001188 */
1189 list_del(&page->lru);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001190 }
David Rientjes68711a72014-06-04 16:08:25 -07001191
Christoph Lameter95a402c2006-06-23 02:03:53 -07001192 /*
Minchan Kimc6c919e2016-07-26 15:23:02 -07001193 * If migration is successful, releases reference grabbed during
1194 * isolation. Otherwise, restore the page to right list unless
1195 * we want to retry.
Christoph Lameter95a402c2006-06-23 02:03:53 -07001196 */
Minchan Kimc6c919e2016-07-26 15:23:02 -07001197 if (rc == MIGRATEPAGE_SUCCESS) {
Yang Shidd4ae782020-12-14 19:13:06 -08001198 /*
1199 * Compaction can migrate also non-LRU pages which are
1200 * not accounted to NR_ISOLATED_*. They can be recognized
1201 * as __PageMovable
1202 */
1203 if (likely(!__PageMovable(page)))
1204 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
1205 page_is_file_lru(page), -thp_nr_pages(page));
1206
Oscar Salvador79f5f8f2020-10-15 20:07:09 -07001207 if (reason != MR_MEMORY_FAILURE)
Minchan Kimc6c919e2016-07-26 15:23:02 -07001208 /*
Oscar Salvador79f5f8f2020-10-15 20:07:09 -07001209 * We release the page in page_handle_poison.
Minchan Kimc6c919e2016-07-26 15:23:02 -07001210 */
Oscar Salvador79f5f8f2020-10-15 20:07:09 -07001211 put_page(page);
Minchan Kimc6c919e2016-07-26 15:23:02 -07001212 } else {
Yang Shidd4ae782020-12-14 19:13:06 -08001213 if (rc != -EAGAIN)
1214 list_add_tail(&page->lru, ret);
Minchan Kimbda807d2016-07-26 15:23:05 -07001215
Minchan Kimc6c919e2016-07-26 15:23:02 -07001216 if (put_new_page)
1217 put_new_page(newpage, private);
1218 else
1219 put_page(newpage);
1220 }
David Rientjes68711a72014-06-04 16:08:25 -07001221
Christoph Lametere24f0b82006-06-23 02:03:51 -07001222 return rc;
1223}
1224
1225/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001226 * Counterpart of unmap_and_move_page() for hugepage migration.
1227 *
1228 * This function doesn't wait the completion of hugepage I/O
1229 * because there is no race between I/O and migration for hugepage.
1230 * Note that currently hugepage I/O occurs only in direct I/O
1231 * where no lock is held and PG_writeback is irrelevant,
1232 * and writeback status of all subpages are counted in the reference
1233 * count of the head page (i.e. if all subpages of a 2MB hugepage are
1234 * under direct I/O, the reference of the head page is 512 and a bit more.)
1235 * This means that when we try to migrate hugepage whose subpages are
1236 * doing direct I/O, some references remain after try_to_unmap() and
1237 * hugepage migration fails without data corruption.
1238 *
1239 * There is also no race when direct I/O is issued on the page under migration,
1240 * because then pte is replaced with migration swap entry and direct I/O code
1241 * will wait in the page fault for migration to complete.
1242 */
1243static int unmap_and_move_huge_page(new_page_t get_new_page,
David Rientjes68711a72014-06-04 16:08:25 -07001244 free_page_t put_new_page, unsigned long private,
1245 struct page *hpage, int force,
Yang Shidd4ae782020-12-14 19:13:06 -08001246 enum migrate_mode mode, int reason,
1247 struct list_head *ret)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001248{
Hugh Dickins2def7422015-11-05 18:49:46 -08001249 int rc = -EAGAIN;
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001250 int page_was_mapped = 0;
Joonsoo Kim32665f22014-01-21 15:51:15 -08001251 struct page *new_hpage;
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001252 struct anon_vma *anon_vma = NULL;
Mike Kravetzc0d03812020-04-01 21:11:05 -07001253 struct address_space *mapping = NULL;
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001254
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001255 /*
Anshuman Khandual7ed2c312019-03-05 15:43:44 -08001256 * Migratability of hugepages depends on architectures and their size.
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001257 * This check is necessary because some callers of hugepage migration
1258 * like soft offline and memory hotremove don't walk through page
1259 * tables or check whether the hugepage is pmd-based or not before
1260 * kicking migration.
1261 */
Naoya Horiguchi100873d2014-06-04 16:10:56 -07001262 if (!hugepage_migration_supported(page_hstate(hpage))) {
Yang Shidd4ae782020-12-14 19:13:06 -08001263 list_move_tail(&hpage->lru, ret);
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001264 return -ENOSYS;
Joonsoo Kim32665f22014-01-21 15:51:15 -08001265 }
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001266
Muchun Song71a64f62021-02-04 18:32:17 -08001267 if (page_count(hpage) == 1) {
1268 /* page was freed from under us. So we are done. */
1269 putback_active_hugepage(hpage);
1270 return MIGRATEPAGE_SUCCESS;
1271 }
1272
Michal Hocko666feb22018-04-10 16:30:03 -07001273 new_hpage = get_new_page(hpage, private);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001274 if (!new_hpage)
1275 return -ENOMEM;
1276
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001277 if (!trylock_page(hpage)) {
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07001278 if (!force)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001279 goto out;
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07001280 switch (mode) {
1281 case MIGRATE_SYNC:
1282 case MIGRATE_SYNC_NO_COPY:
1283 break;
1284 default:
1285 goto out;
1286 }
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001287 lock_page(hpage);
1288 }
1289
Mike Kravetzcb6acd02019-02-28 16:22:02 -08001290 /*
1291 * Check for pages which are in the process of being freed. Without
1292 * page_mapping() set, hugetlbfs specific move page routine will not
1293 * be called and we could leak usage counts for subpools.
1294 */
1295 if (page_private(hpage) && !page_mapping(hpage)) {
1296 rc = -EBUSY;
1297 goto out_unlock;
1298 }
1299
Peter Zijlstra746b18d2011-05-24 17:12:10 -07001300 if (PageAnon(hpage))
1301 anon_vma = page_get_anon_vma(hpage);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001302
Hugh Dickins7db76712015-11-05 18:49:49 -08001303 if (unlikely(!trylock_page(new_hpage)))
1304 goto put_anon;
1305
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001306 if (page_mapped(hpage)) {
Mike Kravetz336bf302020-11-13 22:52:16 -08001307 bool mapping_locked = false;
Shakeel Butt013339d2020-12-14 19:06:39 -08001308 enum ttu_flags ttu = TTU_MIGRATION|TTU_IGNORE_MLOCK;
Mike Kravetzc0d03812020-04-01 21:11:05 -07001309
Mike Kravetz336bf302020-11-13 22:52:16 -08001310 if (!PageAnon(hpage)) {
1311 /*
1312 * In shared mappings, try_to_unmap could potentially
1313 * call huge_pmd_unshare. Because of this, take
1314 * semaphore in write mode here and set TTU_RMAP_LOCKED
1315 * to let lower levels know we have taken the lock.
1316 */
1317 mapping = hugetlb_page_mapping_lock_write(hpage);
1318 if (unlikely(!mapping))
1319 goto unlock_put_anon;
1320
1321 mapping_locked = true;
1322 ttu |= TTU_RMAP_LOCKED;
1323 }
1324
1325 try_to_unmap(hpage, ttu);
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001326 page_was_mapped = 1;
Mike Kravetz336bf302020-11-13 22:52:16 -08001327
1328 if (mapping_locked)
1329 i_mmap_unlock_write(mapping);
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001330 }
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001331
1332 if (!page_mapped(hpage))
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001333 rc = move_to_new_page(new_hpage, hpage, mode);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001334
Mike Kravetz336bf302020-11-13 22:52:16 -08001335 if (page_was_mapped)
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001336 remove_migration_ptes(hpage,
Mike Kravetz336bf302020-11-13 22:52:16 -08001337 rc == MIGRATEPAGE_SUCCESS ? new_hpage : hpage, false);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001338
Mike Kravetzc0d03812020-04-01 21:11:05 -07001339unlock_put_anon:
Hugh Dickins7db76712015-11-05 18:49:49 -08001340 unlock_page(new_hpage);
1341
1342put_anon:
Hugh Dickinsfd4a4662011-01-13 15:47:31 -08001343 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -07001344 put_anon_vma(anon_vma);
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -07001345
Hugh Dickins2def7422015-11-05 18:49:46 -08001346 if (rc == MIGRATEPAGE_SUCCESS) {
Michal Hockoab5ac902018-01-31 16:20:48 -08001347 move_hugetlb_state(hpage, new_hpage, reason);
Hugh Dickins2def7422015-11-05 18:49:46 -08001348 put_new_page = NULL;
1349 }
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -07001350
Mike Kravetzcb6acd02019-02-28 16:22:02 -08001351out_unlock:
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001352 unlock_page(hpage);
Hillf Danton09761332011-12-08 14:34:20 -08001353out:
Yang Shidd4ae782020-12-14 19:13:06 -08001354 if (rc == MIGRATEPAGE_SUCCESS)
Naoya Horiguchib8ec1ce2013-09-11 14:22:01 -07001355 putback_active_hugepage(hpage);
Yang Shidd4ae782020-12-14 19:13:06 -08001356 else if (rc != -EAGAIN && rc != MIGRATEPAGE_SUCCESS)
1357 list_move_tail(&hpage->lru, ret);
David Rientjes68711a72014-06-04 16:08:25 -07001358
1359 /*
1360 * If migration was not successful and there's a freeing callback, use
1361 * it. Otherwise, put_page() will drop the reference grabbed during
1362 * isolation.
1363 */
Hugh Dickins2def7422015-11-05 18:49:46 -08001364 if (put_new_page)
David Rientjes68711a72014-06-04 16:08:25 -07001365 put_new_page(new_hpage, private);
1366 else
Naoya Horiguchi3aaa76e2015-09-22 14:59:14 -07001367 putback_active_hugepage(new_hpage);
David Rientjes68711a72014-06-04 16:08:25 -07001368
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001369 return rc;
1370}
1371
Yang Shid532e2e2020-12-14 19:13:16 -08001372static inline int try_split_thp(struct page *page, struct page **page2,
1373 struct list_head *from)
1374{
1375 int rc = 0;
1376
1377 lock_page(page);
1378 rc = split_huge_page_to_list(page, from);
1379 unlock_page(page);
1380 if (!rc)
1381 list_safe_reset_next(page, *page2, lru);
1382
1383 return rc;
1384}
1385
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001386/*
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001387 * migrate_pages - migrate the pages specified in a list, to the free pages
1388 * supplied as the target for the page migration
Christoph Lameterb20a3502006-03-22 00:09:12 -08001389 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001390 * @from: The list of pages to be migrated.
1391 * @get_new_page: The function used to allocate free pages to be used
1392 * as the target of the page migration.
David Rientjes68711a72014-06-04 16:08:25 -07001393 * @put_new_page: The function used to free target pages if migration
1394 * fails, or NULL if no special handling is necessary.
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001395 * @private: Private data to be passed on to get_new_page()
1396 * @mode: The migration mode that specifies the constraints for
1397 * page migration, if any.
1398 * @reason: The reason for page migration.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001399 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001400 * The function returns after 10 attempts or if no pages are movable any more
1401 * because the list has become empty or no retryable pages exist any more.
Yang Shidd4ae782020-12-14 19:13:06 -08001402 * It is caller's responsibility to call putback_movable_pages() to return pages
1403 * to the LRU or free list only if ret != 0.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001404 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001405 * Returns the number of pages that were not migrated, or an error code.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001406 */
Hugh Dickins9c620e22013-02-22 16:35:14 -08001407int migrate_pages(struct list_head *from, new_page_t get_new_page,
David Rientjes68711a72014-06-04 16:08:25 -07001408 free_page_t put_new_page, unsigned long private,
1409 enum migrate_mode mode, int reason)
Christoph Lameterb20a3502006-03-22 00:09:12 -08001410{
Christoph Lametere24f0b82006-06-23 02:03:51 -07001411 int retry = 1;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001412 int thp_retry = 1;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001413 int nr_failed = 0;
Mel Gorman5647bc22012-10-19 10:46:20 +01001414 int nr_succeeded = 0;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001415 int nr_thp_succeeded = 0;
1416 int nr_thp_failed = 0;
1417 int nr_thp_split = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001418 int pass = 0;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001419 bool is_thp = false;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001420 struct page *page;
1421 struct page *page2;
1422 int swapwrite = current->flags & PF_SWAPWRITE;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001423 int rc, nr_subpages;
Yang Shidd4ae782020-12-14 19:13:06 -08001424 LIST_HEAD(ret_pages);
Christoph Lameterb20a3502006-03-22 00:09:12 -08001425
1426 if (!swapwrite)
1427 current->flags |= PF_SWAPWRITE;
1428
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001429 for (pass = 0; pass < 10 && (retry || thp_retry); pass++) {
Christoph Lametere24f0b82006-06-23 02:03:51 -07001430 retry = 0;
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001431 thp_retry = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001432
Christoph Lametere24f0b82006-06-23 02:03:51 -07001433 list_for_each_entry_safe(page, page2, from, lru) {
Michal Hocko94723aa2018-04-10 16:30:07 -07001434retry:
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001435 /*
1436 * THP statistics is based on the source huge page.
1437 * Capture required information that might get lost
1438 * during migration.
1439 */
Zi Yan6c5c7b92020-09-25 21:19:14 -07001440 is_thp = PageTransHuge(page) && !PageHuge(page);
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -07001441 nr_subpages = thp_nr_pages(page);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001442 cond_resched();
Christoph Lameterb20a3502006-03-22 00:09:12 -08001443
Naoya Horiguchi31caf662013-09-11 14:21:59 -07001444 if (PageHuge(page))
1445 rc = unmap_and_move_huge_page(get_new_page,
David Rientjes68711a72014-06-04 16:08:25 -07001446 put_new_page, private, page,
Yang Shidd4ae782020-12-14 19:13:06 -08001447 pass > 2, mode, reason,
1448 &ret_pages);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07001449 else
David Rientjes68711a72014-06-04 16:08:25 -07001450 rc = unmap_and_move(get_new_page, put_new_page,
Naoya Horiguchiadd05ce2015-06-24 16:56:50 -07001451 private, page, pass > 2, mode,
Yang Shidd4ae782020-12-14 19:13:06 -08001452 reason, &ret_pages);
1453 /*
1454 * The rules are:
1455 * Success: non hugetlb page will be freed, hugetlb
1456 * page will be put back
1457 * -EAGAIN: stay on the from list
1458 * -ENOMEM: stay on the from list
1459 * Other errno: put on ret_pages list then splice to
1460 * from list
1461 */
Christoph Lametere24f0b82006-06-23 02:03:51 -07001462 switch(rc) {
Yang Shid532e2e2020-12-14 19:13:16 -08001463 /*
1464 * THP migration might be unsupported or the
1465 * allocation could've failed so we should
1466 * retry on the same page with the THP split
1467 * to base pages.
1468 *
1469 * Head page is retried immediately and tail
1470 * pages are added to the tail of the list so
1471 * we encounter them after the rest of the list
1472 * is processed.
1473 */
1474 case -ENOSYS:
1475 /* THP migration is unsupported */
1476 if (is_thp) {
1477 if (!try_split_thp(page, &page2, from)) {
1478 nr_thp_split++;
1479 goto retry;
1480 }
1481
1482 nr_thp_failed++;
1483 nr_failed += nr_subpages;
1484 break;
1485 }
1486
1487 /* Hugetlb migration is unsupported */
1488 nr_failed++;
1489 break;
Christoph Lameter95a402c2006-06-23 02:03:53 -07001490 case -ENOMEM:
Michal Hocko94723aa2018-04-10 16:30:07 -07001491 /*
Yang Shid532e2e2020-12-14 19:13:16 -08001492 * When memory is low, don't bother to try to migrate
1493 * other pages, just exit.
Michal Hocko94723aa2018-04-10 16:30:07 -07001494 */
Zi Yan6c5c7b92020-09-25 21:19:14 -07001495 if (is_thp) {
Yang Shid532e2e2020-12-14 19:13:16 -08001496 if (!try_split_thp(page, &page2, from)) {
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001497 nr_thp_split++;
Michal Hocko94723aa2018-04-10 16:30:07 -07001498 goto retry;
1499 }
Zi Yan6c5c7b92020-09-25 21:19:14 -07001500
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001501 nr_thp_failed++;
1502 nr_failed += nr_subpages;
1503 goto out;
1504 }
David Rientjesdfef2ef2016-05-20 16:59:05 -07001505 nr_failed++;
Christoph Lameter95a402c2006-06-23 02:03:53 -07001506 goto out;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001507 case -EAGAIN:
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001508 if (is_thp) {
1509 thp_retry++;
1510 break;
1511 }
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001512 retry++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001513 break;
Rafael Aquini78bd5202012-12-11 16:02:31 -08001514 case MIGRATEPAGE_SUCCESS:
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001515 if (is_thp) {
1516 nr_thp_succeeded++;
1517 nr_succeeded += nr_subpages;
1518 break;
1519 }
Mel Gorman5647bc22012-10-19 10:46:20 +01001520 nr_succeeded++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001521 break;
1522 default:
Naoya Horiguchi354a3362014-01-21 15:51:14 -08001523 /*
Yang Shid532e2e2020-12-14 19:13:16 -08001524 * Permanent failure (-EBUSY, etc.):
Naoya Horiguchi354a3362014-01-21 15:51:14 -08001525 * unlike -EAGAIN case, the failed page is
1526 * removed from migration page list and not
1527 * retried in the next outer loop.
1528 */
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001529 if (is_thp) {
1530 nr_thp_failed++;
1531 nr_failed += nr_subpages;
1532 break;
1533 }
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001534 nr_failed++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001535 break;
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001536 }
Christoph Lameterb20a3502006-03-22 00:09:12 -08001537 }
1538 }
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001539 nr_failed += retry + thp_retry;
1540 nr_thp_failed += thp_retry;
Vlastimil Babkaf2f81fb2015-11-05 18:47:03 -08001541 rc = nr_failed;
Christoph Lameter95a402c2006-06-23 02:03:53 -07001542out:
Yang Shidd4ae782020-12-14 19:13:06 -08001543 /*
1544 * Put the permanent failure page back to migration list, they
1545 * will be put back to the right list by the caller.
1546 */
1547 list_splice(&ret_pages, from);
1548
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001549 count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
1550 count_vm_events(PGMIGRATE_FAIL, nr_failed);
1551 count_vm_events(THP_MIGRATION_SUCCESS, nr_thp_succeeded);
1552 count_vm_events(THP_MIGRATION_FAIL, nr_thp_failed);
1553 count_vm_events(THP_MIGRATION_SPLIT, nr_thp_split);
1554 trace_mm_migrate_pages(nr_succeeded, nr_failed, nr_thp_succeeded,
1555 nr_thp_failed, nr_thp_split, mode, reason);
Mel Gorman7b2a2d42012-10-19 14:07:31 +01001556
Christoph Lameterb20a3502006-03-22 00:09:12 -08001557 if (!swapwrite)
1558 current->flags &= ~PF_SWAPWRITE;
1559
Rafael Aquini78bd5202012-12-11 16:02:31 -08001560 return rc;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001561}
1562
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001563struct page *alloc_migration_target(struct page *page, unsigned long private)
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001564{
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001565 struct migration_target_control *mtc;
1566 gfp_t gfp_mask;
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001567 unsigned int order = 0;
1568 struct page *new_page = NULL;
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001569 int nid;
1570 int zidx;
1571
1572 mtc = (struct migration_target_control *)private;
1573 gfp_mask = mtc->gfp_mask;
1574 nid = mtc->nid;
1575 if (nid == NUMA_NO_NODE)
1576 nid = page_to_nid(page);
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001577
Joonsoo Kimd92bbc22020-08-11 18:37:17 -07001578 if (PageHuge(page)) {
1579 struct hstate *h = page_hstate(compound_head(page));
1580
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001581 gfp_mask = htlb_modify_alloc_mask(h, gfp_mask);
1582 return alloc_huge_page_nodemask(h, nid, mtc->nmask, gfp_mask);
Joonsoo Kimd92bbc22020-08-11 18:37:17 -07001583 }
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001584
1585 if (PageTransHuge(page)) {
Joonsoo Kim9933a0c2020-08-11 18:37:20 -07001586 /*
1587 * clear __GFP_RECLAIM to make the migration callback
1588 * consistent with regular THP allocations.
1589 */
1590 gfp_mask &= ~__GFP_RECLAIM;
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001591 gfp_mask |= GFP_TRANSHUGE;
1592 order = HPAGE_PMD_ORDER;
1593 }
Joonsoo Kim19fc7be2020-08-11 18:37:25 -07001594 zidx = zone_idx(page_zone(page));
1595 if (is_highmem_idx(zidx) || zidx == ZONE_MOVABLE)
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001596 gfp_mask |= __GFP_HIGHMEM;
1597
Matthew Wilcox (Oracle)84172f42021-04-29 23:01:15 -07001598 new_page = __alloc_pages(gfp_mask, order, nid, mtc->nmask);
Joonsoo Kimb4b38222020-08-11 18:37:14 -07001599
1600 if (new_page && PageTransHuge(new_page))
1601 prep_transhuge_page(new_page);
1602
1603 return new_page;
1604}
1605
Christoph Lameter742755a2006-06-23 02:03:55 -07001606#ifdef CONFIG_NUMA
Christoph Lameter742755a2006-06-23 02:03:55 -07001607
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001608static int store_status(int __user *status, int start, int value, int nr)
Christoph Lameter742755a2006-06-23 02:03:55 -07001609{
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001610 while (nr-- > 0) {
1611 if (put_user(value, status + start))
1612 return -EFAULT;
1613 start++;
1614 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001615
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001616 return 0;
1617}
Christoph Lameter742755a2006-06-23 02:03:55 -07001618
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001619static int do_move_pages_to_node(struct mm_struct *mm,
1620 struct list_head *pagelist, int node)
1621{
1622 int err;
Joonsoo Kima0976312020-08-11 18:37:28 -07001623 struct migration_target_control mtc = {
1624 .nid = node,
1625 .gfp_mask = GFP_HIGHUSER_MOVABLE | __GFP_THISNODE,
1626 };
Christoph Lameter742755a2006-06-23 02:03:55 -07001627
Joonsoo Kima0976312020-08-11 18:37:28 -07001628 err = migrate_pages(pagelist, alloc_migration_target, NULL,
1629 (unsigned long)&mtc, MIGRATE_SYNC, MR_SYSCALL);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001630 if (err)
1631 putback_movable_pages(pagelist);
1632 return err;
Christoph Lameter742755a2006-06-23 02:03:55 -07001633}
1634
1635/*
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001636 * Resolves the given address to a struct page, isolates it from the LRU and
1637 * puts it to the given pagelist.
Yang Shie0153fc2020-01-04 12:59:46 -08001638 * Returns:
1639 * errno - if the page cannot be found/isolated
1640 * 0 - when it doesn't have to be migrated because it is already on the
1641 * target node
1642 * 1 - when it has been queued
Christoph Lameter742755a2006-06-23 02:03:55 -07001643 */
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001644static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
1645 int node, struct list_head *pagelist, bool migrate_all)
Christoph Lameter742755a2006-06-23 02:03:55 -07001646{
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001647 struct vm_area_struct *vma;
1648 struct page *page;
1649 unsigned int follflags;
Christoph Lameter742755a2006-06-23 02:03:55 -07001650 int err;
Christoph Lameter742755a2006-06-23 02:03:55 -07001651
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001652 mmap_read_lock(mm);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001653 err = -EFAULT;
1654 vma = find_vma(mm, addr);
1655 if (!vma || addr < vma->vm_start || !vma_migratable(vma))
1656 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001657
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001658 /* FOLL_DUMP to ignore special (like zero) pages */
1659 follflags = FOLL_GET | FOLL_DUMP;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001660 page = follow_page(vma, addr, follflags);
Christoph Lameter742755a2006-06-23 02:03:55 -07001661
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001662 err = PTR_ERR(page);
1663 if (IS_ERR(page))
1664 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001665
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001666 err = -ENOENT;
1667 if (!page)
1668 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001669
Brice Gogline78bbfa2008-10-18 20:27:15 -07001670 err = 0;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001671 if (page_to_nid(page) == node)
1672 goto out_putpage;
Christoph Lameter742755a2006-06-23 02:03:55 -07001673
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001674 err = -EACCES;
1675 if (page_mapcount(page) > 1 && !migrate_all)
1676 goto out_putpage;
1677
1678 if (PageHuge(page)) {
1679 if (PageHead(page)) {
1680 isolate_huge_page(page, pagelist);
Yang Shie0153fc2020-01-04 12:59:46 -08001681 err = 1;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001682 }
1683 } else {
1684 struct page *head;
1685
1686 head = compound_head(page);
1687 err = isolate_lru_page(head);
1688 if (err)
1689 goto out_putpage;
1690
Yang Shie0153fc2020-01-04 12:59:46 -08001691 err = 1;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001692 list_add_tail(&head->lru, pagelist);
1693 mod_node_page_state(page_pgdat(head),
Huang Ying9de4f222020-04-06 20:04:41 -07001694 NR_ISOLATED_ANON + page_is_file_lru(head),
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -07001695 thp_nr_pages(head));
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001696 }
1697out_putpage:
1698 /*
1699 * Either remove the duplicate refcount from
1700 * isolate_lru_page() or drop the page ref if it was
1701 * not isolated.
1702 */
1703 put_page(page);
1704out:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001705 mmap_read_unlock(mm);
Christoph Lameter742755a2006-06-23 02:03:55 -07001706 return err;
1707}
1708
Wei Yang7ca87832020-04-06 20:04:12 -07001709static int move_pages_and_store_status(struct mm_struct *mm, int node,
1710 struct list_head *pagelist, int __user *status,
1711 int start, int i, unsigned long nr_pages)
1712{
1713 int err;
1714
Wei Yang5d7ae892020-04-06 20:04:15 -07001715 if (list_empty(pagelist))
1716 return 0;
1717
Wei Yang7ca87832020-04-06 20:04:12 -07001718 err = do_move_pages_to_node(mm, pagelist, node);
1719 if (err) {
1720 /*
1721 * Positive err means the number of failed
1722 * pages to migrate. Since we are going to
1723 * abort and return the number of non-migrated
Long Liab9dd4f2020-12-14 19:12:52 -08001724 * pages, so need to include the rest of the
Wei Yang7ca87832020-04-06 20:04:12 -07001725 * nr_pages that have not been attempted as
1726 * well.
1727 */
1728 if (err > 0)
1729 err += nr_pages - i - 1;
1730 return err;
1731 }
1732 return store_status(status, start, node, i - start);
1733}
1734
Christoph Lameter742755a2006-06-23 02:03:55 -07001735/*
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001736 * Migrate an array of page address onto an array of nodes and fill
1737 * the corresponding array of status.
1738 */
Christoph Lameter3268c632012-03-21 16:34:06 -07001739static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001740 unsigned long nr_pages,
1741 const void __user * __user *pages,
1742 const int __user *nodes,
1743 int __user *status, int flags)
1744{
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001745 int current_node = NUMA_NO_NODE;
1746 LIST_HEAD(pagelist);
1747 int start, i;
1748 int err = 0, err1;
Brice Goglin35282a22009-06-16 15:32:43 -07001749
Minchan Kim361a2a22021-05-04 18:36:57 -07001750 lru_cache_disable();
Brice Goglin35282a22009-06-16 15:32:43 -07001751
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001752 for (i = start = 0; i < nr_pages; i++) {
1753 const void __user *p;
1754 unsigned long addr;
1755 int node;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001756
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001757 err = -EFAULT;
1758 if (get_user(p, pages + i))
1759 goto out_flush;
1760 if (get_user(node, nodes + i))
1761 goto out_flush;
Andrey Konovalov057d33892019-09-25 16:48:30 -07001762 addr = (unsigned long)untagged_addr(p);
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001763
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001764 err = -ENODEV;
1765 if (node < 0 || node >= MAX_NUMNODES)
1766 goto out_flush;
1767 if (!node_state(node, N_MEMORY))
1768 goto out_flush;
Brice Goglin3140a222009-01-06 14:38:57 -08001769
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001770 err = -EACCES;
1771 if (!node_isset(node, task_nodes))
1772 goto out_flush;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001773
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001774 if (current_node == NUMA_NO_NODE) {
1775 current_node = node;
1776 start = i;
1777 } else if (node != current_node) {
Wei Yang7ca87832020-04-06 20:04:12 -07001778 err = move_pages_and_store_status(mm, current_node,
1779 &pagelist, status, start, i, nr_pages);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001780 if (err)
1781 goto out;
1782 start = i;
1783 current_node = node;
Brice Goglin3140a222009-01-06 14:38:57 -08001784 }
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001785
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001786 /*
1787 * Errors in the page lookup or isolation are not fatal and we simply
1788 * report them via status
1789 */
1790 err = add_page_for_migration(mm, addr, current_node,
1791 &pagelist, flags & MPOL_MF_MOVE_ALL);
Yang Shie0153fc2020-01-04 12:59:46 -08001792
Wei Yangd08221a2020-04-06 20:04:18 -07001793 if (err > 0) {
Yang Shie0153fc2020-01-04 12:59:46 -08001794 /* The page is successfully queued for migration */
1795 continue;
1796 }
Brice Goglin3140a222009-01-06 14:38:57 -08001797
Wei Yangd08221a2020-04-06 20:04:18 -07001798 /*
1799 * If the page is already on the target node (!err), store the
1800 * node, otherwise, store the err.
1801 */
1802 err = store_status(status, i, err ? : current_node, 1);
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001803 if (err)
1804 goto out_flush;
Brice Goglin3140a222009-01-06 14:38:57 -08001805
Wei Yang7ca87832020-04-06 20:04:12 -07001806 err = move_pages_and_store_status(mm, current_node, &pagelist,
1807 status, start, i, nr_pages);
Wei Yang4afdace2020-04-06 20:04:09 -07001808 if (err)
1809 goto out;
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001810 current_node = NUMA_NO_NODE;
Brice Goglin3140a222009-01-06 14:38:57 -08001811 }
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001812out_flush:
1813 /* Make sure we do not overwrite the existing error */
Wei Yang7ca87832020-04-06 20:04:12 -07001814 err1 = move_pages_and_store_status(mm, current_node, &pagelist,
1815 status, start, i, nr_pages);
Wei Yangdfe9aa22020-01-30 22:11:14 -08001816 if (err >= 0)
Michal Hockoa49bd4d2018-04-10 16:29:59 -07001817 err = err1;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001818out:
Minchan Kim361a2a22021-05-04 18:36:57 -07001819 lru_cache_enable();
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001820 return err;
1821}
1822
1823/*
Brice Goglin2f007e72008-10-18 20:27:16 -07001824 * Determine the nodes of an array of pages and store it in an array of status.
Christoph Lameter742755a2006-06-23 02:03:55 -07001825 */
Brice Goglin80bba122008-12-09 13:14:23 -08001826static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1827 const void __user **pages, int *status)
Christoph Lameter742755a2006-06-23 02:03:55 -07001828{
Brice Goglin2f007e72008-10-18 20:27:16 -07001829 unsigned long i;
Brice Goglin2f007e72008-10-18 20:27:16 -07001830
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001831 mmap_read_lock(mm);
Christoph Lameter742755a2006-06-23 02:03:55 -07001832
Brice Goglin2f007e72008-10-18 20:27:16 -07001833 for (i = 0; i < nr_pages; i++) {
Brice Goglin80bba122008-12-09 13:14:23 -08001834 unsigned long addr = (unsigned long)(*pages);
Christoph Lameter742755a2006-06-23 02:03:55 -07001835 struct vm_area_struct *vma;
1836 struct page *page;
KOSAKI Motohiroc095adb2008-12-16 16:06:43 +09001837 int err = -EFAULT;
Brice Goglin2f007e72008-10-18 20:27:16 -07001838
1839 vma = find_vma(mm, addr);
Gleb Natapov70384dc2010-10-26 14:22:07 -07001840 if (!vma || addr < vma->vm_start)
Christoph Lameter742755a2006-06-23 02:03:55 -07001841 goto set_status;
1842
Kirill A. Shutemovd8998442015-09-04 15:47:53 -07001843 /* FOLL_DUMP to ignore special (like zero) pages */
1844 page = follow_page(vma, addr, FOLL_DUMP);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001845
1846 err = PTR_ERR(page);
1847 if (IS_ERR(page))
1848 goto set_status;
1849
Kirill A. Shutemovd8998442015-09-04 15:47:53 -07001850 err = page ? page_to_nid(page) : -ENOENT;
Christoph Lameter742755a2006-06-23 02:03:55 -07001851set_status:
Brice Goglin80bba122008-12-09 13:14:23 -08001852 *status = err;
1853
1854 pages++;
1855 status++;
1856 }
1857
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001858 mmap_read_unlock(mm);
Brice Goglin80bba122008-12-09 13:14:23 -08001859}
1860
1861/*
1862 * Determine the nodes of a user array of pages and store it in
1863 * a user array of status.
1864 */
1865static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1866 const void __user * __user *pages,
1867 int __user *status)
1868{
1869#define DO_PAGES_STAT_CHUNK_NR 16
1870 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1871 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
Brice Goglin80bba122008-12-09 13:14:23 -08001872
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001873 while (nr_pages) {
1874 unsigned long chunk_nr;
Brice Goglin80bba122008-12-09 13:14:23 -08001875
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001876 chunk_nr = nr_pages;
1877 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1878 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1879
1880 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
1881 break;
Brice Goglin80bba122008-12-09 13:14:23 -08001882
1883 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1884
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001885 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1886 break;
Christoph Lameter742755a2006-06-23 02:03:55 -07001887
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001888 pages += chunk_nr;
1889 status += chunk_nr;
1890 nr_pages -= chunk_nr;
1891 }
1892 return nr_pages ? -EFAULT : 0;
Christoph Lameter742755a2006-06-23 02:03:55 -07001893}
1894
Miaohe Lin4dc200c2020-10-17 16:14:03 -07001895static struct mm_struct *find_mm_struct(pid_t pid, nodemask_t *mem_nodes)
1896{
1897 struct task_struct *task;
1898 struct mm_struct *mm;
1899
1900 /*
1901 * There is no need to check if current process has the right to modify
1902 * the specified process when they are same.
1903 */
1904 if (!pid) {
1905 mmget(current->mm);
1906 *mem_nodes = cpuset_mems_allowed(current);
1907 return current->mm;
1908 }
1909
1910 /* Find the mm_struct */
1911 rcu_read_lock();
1912 task = find_task_by_vpid(pid);
1913 if (!task) {
1914 rcu_read_unlock();
1915 return ERR_PTR(-ESRCH);
1916 }
1917 get_task_struct(task);
1918
1919 /*
1920 * Check if this process has the right to modify the specified
1921 * process. Use the regular "ptrace_may_access()" checks.
1922 */
1923 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
1924 rcu_read_unlock();
1925 mm = ERR_PTR(-EPERM);
1926 goto out;
1927 }
1928 rcu_read_unlock();
1929
1930 mm = ERR_PTR(security_task_movememory(task));
1931 if (IS_ERR(mm))
1932 goto out;
1933 *mem_nodes = cpuset_mems_allowed(task);
1934 mm = get_task_mm(task);
1935out:
1936 put_task_struct(task);
1937 if (!mm)
1938 mm = ERR_PTR(-EINVAL);
1939 return mm;
1940}
1941
Christoph Lameter742755a2006-06-23 02:03:55 -07001942/*
1943 * Move a list of pages in the address space of the currently executing
1944 * process.
1945 */
Dominik Brodowski7addf442018-03-17 16:08:03 +01001946static int kernel_move_pages(pid_t pid, unsigned long nr_pages,
1947 const void __user * __user *pages,
1948 const int __user *nodes,
1949 int __user *status, int flags)
Christoph Lameter742755a2006-06-23 02:03:55 -07001950{
Christoph Lameter742755a2006-06-23 02:03:55 -07001951 struct mm_struct *mm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001952 int err;
Christoph Lameter3268c632012-03-21 16:34:06 -07001953 nodemask_t task_nodes;
Christoph Lameter742755a2006-06-23 02:03:55 -07001954
1955 /* Check flags */
1956 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1957 return -EINVAL;
1958
1959 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1960 return -EPERM;
1961
Miaohe Lin4dc200c2020-10-17 16:14:03 -07001962 mm = find_mm_struct(pid, &task_nodes);
1963 if (IS_ERR(mm))
1964 return PTR_ERR(mm);
Sasha Levin6e8b09e2012-04-25 16:01:53 -07001965
1966 if (nodes)
1967 err = do_pages_move(mm, task_nodes, nr_pages, pages,
1968 nodes, status, flags);
1969 else
1970 err = do_pages_stat(mm, nr_pages, pages, status);
Christoph Lameter3268c632012-03-21 16:34:06 -07001971
1972 mmput(mm);
1973 return err;
Christoph Lameter742755a2006-06-23 02:03:55 -07001974}
Christoph Lameter742755a2006-06-23 02:03:55 -07001975
Dominik Brodowski7addf442018-03-17 16:08:03 +01001976SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1977 const void __user * __user *, pages,
1978 const int __user *, nodes,
1979 int __user *, status, int, flags)
1980{
1981 return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags);
1982}
1983
1984#ifdef CONFIG_COMPAT
1985COMPAT_SYSCALL_DEFINE6(move_pages, pid_t, pid, compat_ulong_t, nr_pages,
1986 compat_uptr_t __user *, pages32,
1987 const int __user *, nodes,
1988 int __user *, status,
1989 int, flags)
1990{
1991 const void __user * __user *pages;
1992 int i;
1993
1994 pages = compat_alloc_user_space(nr_pages * sizeof(void *));
1995 for (i = 0; i < nr_pages; i++) {
1996 compat_uptr_t p;
1997
1998 if (get_user(p, pages32 + i) ||
1999 put_user(compat_ptr(p), pages + i))
2000 return -EFAULT;
2001 }
2002 return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags);
2003}
2004#endif /* CONFIG_COMPAT */
2005
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002006#ifdef CONFIG_NUMA_BALANCING
2007/*
2008 * Returns true if this is a safe migration target node for misplaced NUMA
2009 * pages. Currently it only checks the watermarks which crude
2010 */
2011static bool migrate_balanced_pgdat(struct pglist_data *pgdat,
Mel Gorman3abef4e2013-02-22 16:34:27 -08002012 unsigned long nr_migrate_pages)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002013{
2014 int z;
Mel Gorman599d0c92016-07-28 15:45:31 -07002015
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002016 for (z = pgdat->nr_zones - 1; z >= 0; z--) {
2017 struct zone *zone = pgdat->node_zones + z;
2018
2019 if (!populated_zone(zone))
2020 continue;
2021
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002022 /* Avoid waking kswapd by allocating pages_to_migrate pages. */
2023 if (!zone_watermark_ok(zone, 0,
2024 high_wmark_pages(zone) +
2025 nr_migrate_pages,
Huang Yingbfe9d002019-11-30 17:57:28 -08002026 ZONE_MOVABLE, 0))
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002027 continue;
2028 return true;
2029 }
2030 return false;
2031}
2032
2033static struct page *alloc_misplaced_dst_page(struct page *page,
Michal Hocko666feb22018-04-10 16:30:03 -07002034 unsigned long data)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002035{
2036 int nid = (int) data;
2037 struct page *newpage;
2038
Vlastimil Babka96db8002015-09-08 15:03:50 -07002039 newpage = __alloc_pages_node(nid,
Johannes Weinere97ca8e52014-03-10 15:49:43 -07002040 (GFP_HIGHUSER_MOVABLE |
2041 __GFP_THISNODE | __GFP_NOMEMALLOC |
2042 __GFP_NORETRY | __GFP_NOWARN) &
Mel Gorman8479eba2016-02-26 15:19:31 -08002043 ~__GFP_RECLAIM, 0);
Hillf Dantonbac03822012-11-27 14:46:24 +00002044
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002045 return newpage;
2046}
2047
Mel Gorman1c30e012014-01-21 15:50:58 -08002048static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
Mel Gormanb32967f2012-11-19 12:35:47 +00002049{
Hugh Dickins340ef392013-02-22 16:34:33 -08002050 int page_lru;
Mel Gormanb32967f2012-11-19 12:35:47 +00002051
Sasha Levin309381fea2014-01-23 15:52:54 -08002052 VM_BUG_ON_PAGE(compound_order(page) && !PageTransHuge(page), page);
Mel Gorman3abef4e2013-02-22 16:34:27 -08002053
Mel Gormanb32967f2012-11-19 12:35:47 +00002054 /* Avoid migrating to a node that is nearly full */
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -07002055 if (!migrate_balanced_pgdat(pgdat, compound_nr(page)))
Hugh Dickins340ef392013-02-22 16:34:33 -08002056 return 0;
Mel Gormanb32967f2012-11-19 12:35:47 +00002057
Hugh Dickins340ef392013-02-22 16:34:33 -08002058 if (isolate_lru_page(page))
2059 return 0;
Mel Gormanb32967f2012-11-19 12:35:47 +00002060
2061 /*
Hugh Dickins340ef392013-02-22 16:34:33 -08002062 * migrate_misplaced_transhuge_page() skips page migration's usual
2063 * check on page_count(), so we must do it here, now that the page
2064 * has been isolated: a GUP pin, or any other pin, prevents migration.
2065 * The expected page count is 3: 1 for page's mapcount and 1 for the
2066 * caller's pin and 1 for the reference taken by isolate_lru_page().
2067 */
2068 if (PageTransHuge(page) && page_count(page) != 3) {
2069 putback_lru_page(page);
2070 return 0;
2071 }
2072
Huang Ying9de4f222020-04-06 20:04:41 -07002073 page_lru = page_is_file_lru(page);
Mel Gorman599d0c92016-07-28 15:45:31 -07002074 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_lru,
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -07002075 thp_nr_pages(page));
Hugh Dickins340ef392013-02-22 16:34:33 -08002076
2077 /*
2078 * Isolating the page has taken another reference, so the
2079 * caller's reference can be safely dropped without the page
2080 * disappearing underneath us during migration.
Mel Gormanb32967f2012-11-19 12:35:47 +00002081 */
2082 put_page(page);
Hugh Dickins340ef392013-02-22 16:34:33 -08002083 return 1;
Mel Gormanb32967f2012-11-19 12:35:47 +00002084}
2085
Mel Gormande466bd2013-12-18 17:08:42 -08002086bool pmd_trans_migrating(pmd_t pmd)
2087{
2088 struct page *page = pmd_page(pmd);
2089 return PageLocked(page);
2090}
2091
Yang Shic77c5cb2020-12-14 19:13:09 -08002092static inline bool is_shared_exec_page(struct vm_area_struct *vma,
2093 struct page *page)
2094{
2095 if (page_mapcount(page) != 1 &&
2096 (page_is_file_lru(page) || vma_is_shmem(vma)) &&
2097 (vma->vm_flags & VM_EXEC))
2098 return true;
2099
2100 return false;
2101}
2102
Mel Gormana8f60772012-11-14 21:41:46 +00002103/*
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002104 * Attempt to migrate a misplaced page to the specified destination
2105 * node. Caller is expected to have an elevated reference count on
2106 * the page that will be dropped by this function before returning.
2107 */
Mel Gorman1bc115d2013-10-07 11:29:05 +01002108int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma,
2109 int node)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002110{
Mel Gormana8f60772012-11-14 21:41:46 +00002111 pg_data_t *pgdat = NODE_DATA(node);
Hugh Dickins340ef392013-02-22 16:34:33 -08002112 int isolated;
Mel Gormanb32967f2012-11-19 12:35:47 +00002113 int nr_remaining;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002114 LIST_HEAD(migratepages);
2115
2116 /*
Mel Gorman1bc115d2013-10-07 11:29:05 +01002117 * Don't migrate file pages that are mapped in multiple processes
2118 * with execute permissions as they are probably shared libraries.
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002119 */
Yang Shic77c5cb2020-12-14 19:13:09 -08002120 if (is_shared_exec_page(vma, page))
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002121 goto out;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002122
Mel Gormana8f60772012-11-14 21:41:46 +00002123 /*
Mel Gorman09a913a2018-04-10 16:29:20 -07002124 * Also do not migrate dirty pages as not all filesystems can move
2125 * dirty pages in MIGRATE_ASYNC mode which is a waste of cycles.
2126 */
Huang Ying9de4f222020-04-06 20:04:41 -07002127 if (page_is_file_lru(page) && PageDirty(page))
Mel Gorman09a913a2018-04-10 16:29:20 -07002128 goto out;
2129
Mel Gormanb32967f2012-11-19 12:35:47 +00002130 isolated = numamigrate_isolate_page(pgdat, page);
2131 if (!isolated)
2132 goto out;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002133
Mel Gormanb32967f2012-11-19 12:35:47 +00002134 list_add(&page->lru, &migratepages);
Hugh Dickins9c620e22013-02-22 16:35:14 -08002135 nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_page,
David Rientjes68711a72014-06-04 16:08:25 -07002136 NULL, node, MIGRATE_ASYNC,
2137 MR_NUMA_MISPLACED);
Mel Gormanb32967f2012-11-19 12:35:47 +00002138 if (nr_remaining) {
Joonsoo Kim59c82b72014-01-21 15:51:17 -08002139 if (!list_empty(&migratepages)) {
2140 list_del(&page->lru);
Mel Gorman599d0c92016-07-28 15:45:31 -07002141 dec_node_page_state(page, NR_ISOLATED_ANON +
Huang Ying9de4f222020-04-06 20:04:41 -07002142 page_is_file_lru(page));
Joonsoo Kim59c82b72014-01-21 15:51:17 -08002143 putback_lru_page(page);
2144 }
Mel Gormanb32967f2012-11-19 12:35:47 +00002145 isolated = 0;
2146 } else
2147 count_vm_numa_event(NUMA_PAGE_MIGRATE);
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002148 BUG_ON(!list_empty(&migratepages));
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002149 return isolated;
Hugh Dickins340ef392013-02-22 16:34:33 -08002150
2151out:
2152 put_page(page);
2153 return 0;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002154}
Mel Gorman220018d2012-12-05 09:32:56 +00002155#endif /* CONFIG_NUMA_BALANCING */
Mel Gormanb32967f2012-11-19 12:35:47 +00002156
Mel Gorman220018d2012-12-05 09:32:56 +00002157#if defined(CONFIG_NUMA_BALANCING) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
Hugh Dickins340ef392013-02-22 16:34:33 -08002158/*
2159 * Migrates a THP to a given target node. page must be locked and is unlocked
2160 * before returning.
2161 */
Mel Gormanb32967f2012-11-19 12:35:47 +00002162int migrate_misplaced_transhuge_page(struct mm_struct *mm,
2163 struct vm_area_struct *vma,
2164 pmd_t *pmd, pmd_t entry,
2165 unsigned long address,
2166 struct page *page, int node)
2167{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002168 spinlock_t *ptl;
Mel Gormanb32967f2012-11-19 12:35:47 +00002169 pg_data_t *pgdat = NODE_DATA(node);
2170 int isolated = 0;
2171 struct page *new_page = NULL;
Huang Ying9de4f222020-04-06 20:04:41 -07002172 int page_lru = page_is_file_lru(page);
Andrea Arcangeli7066f0f2018-10-26 15:10:40 -07002173 unsigned long start = address & HPAGE_PMD_MASK;
Mel Gormanb32967f2012-11-19 12:35:47 +00002174
Yang Shic77c5cb2020-12-14 19:13:09 -08002175 if (is_shared_exec_page(vma, page))
2176 goto out;
2177
Mel Gormanb32967f2012-11-19 12:35:47 +00002178 new_page = alloc_pages_node(node,
Vlastimil Babka25160352016-07-28 15:49:25 -07002179 (GFP_TRANSHUGE_LIGHT | __GFP_THISNODE),
Johannes Weinere97ca8e52014-03-10 15:49:43 -07002180 HPAGE_PMD_ORDER);
Hugh Dickins340ef392013-02-22 16:34:33 -08002181 if (!new_page)
2182 goto out_fail;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002183 prep_transhuge_page(new_page);
Hugh Dickins340ef392013-02-22 16:34:33 -08002184
Mel Gormanb32967f2012-11-19 12:35:47 +00002185 isolated = numamigrate_isolate_page(pgdat, page);
Hugh Dickins340ef392013-02-22 16:34:33 -08002186 if (!isolated) {
Mel Gormanb32967f2012-11-19 12:35:47 +00002187 put_page(new_page);
Hugh Dickins340ef392013-02-22 16:34:33 -08002188 goto out_fail;
Mel Gormanb32967f2012-11-19 12:35:47 +00002189 }
Mel Gormanb0943d62013-12-18 17:08:46 -08002190
Mel Gormanb32967f2012-11-19 12:35:47 +00002191 /* Prepare a page as a migration target */
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -08002192 __SetPageLocked(new_page);
Shaohua Lid44d3632017-05-03 14:52:26 -07002193 if (PageSwapBacked(page))
2194 __SetPageSwapBacked(new_page);
Mel Gormanb32967f2012-11-19 12:35:47 +00002195
2196 /* anon mapping, we can simply copy page->mapping to the new page: */
2197 new_page->mapping = page->mapping;
2198 new_page->index = page->index;
Andrea Arcangeli7eef5f92018-10-26 15:10:43 -07002199 /* flush the cache before copying using the kernel virtual address */
2200 flush_cache_range(vma, start, start + HPAGE_PMD_SIZE);
Mel Gormanb32967f2012-11-19 12:35:47 +00002201 migrate_page_copy(new_page, page);
2202 WARN_ON(PageLRU(new_page));
2203
2204 /* Recheck the target PMD */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002205 ptl = pmd_lock(mm, pmd);
Will Deaconf4e177d2017-07-10 15:48:31 -07002206 if (unlikely(!pmd_same(*pmd, entry) || !page_ref_freeze(page, 2))) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002207 spin_unlock(ptl);
Mel Gormanb32967f2012-11-19 12:35:47 +00002208
2209 /* Reverse changes made by migrate_page_copy() */
2210 if (TestClearPageActive(new_page))
2211 SetPageActive(page);
2212 if (TestClearPageUnevictable(new_page))
2213 SetPageUnevictable(page);
Mel Gormanb32967f2012-11-19 12:35:47 +00002214
2215 unlock_page(new_page);
2216 put_page(new_page); /* Free it */
2217
Mel Gormana54a4072013-10-07 11:28:46 +01002218 /* Retake the callers reference and putback on LRU */
2219 get_page(page);
Mel Gormanb32967f2012-11-19 12:35:47 +00002220 putback_lru_page(page);
Mel Gorman599d0c92016-07-28 15:45:31 -07002221 mod_node_page_state(page_pgdat(page),
Mel Gormana54a4072013-10-07 11:28:46 +01002222 NR_ISOLATED_ANON + page_lru, -HPAGE_PMD_NR);
Mel Gormaneb4489f62013-12-18 17:08:39 -08002223
2224 goto out_unlock;
Mel Gormanb32967f2012-11-19 12:35:47 +00002225 }
2226
Kirill A. Shutemov10102452016-07-26 15:25:29 -07002227 entry = mk_huge_pmd(new_page, vma->vm_page_prot);
Linus Torvaldsf55e1012017-11-29 09:01:01 -08002228 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Mel Gormanb32967f2012-11-19 12:35:47 +00002229
Mel Gorman2b4847e2013-12-18 17:08:32 -08002230 /*
Andrea Arcangelid7c33932018-10-26 15:10:36 -07002231 * Overwrite the old entry under pagetable lock and establish
2232 * the new PTE. Any parallel GUP will either observe the old
2233 * page blocking on the page lock, block on the page table
2234 * lock or observe the new page. The SetPageUptodate on the
2235 * new page and page_add_new_anon_rmap guarantee the copy is
2236 * visible before the pagetable update.
Mel Gorman2b4847e2013-12-18 17:08:32 -08002237 */
Andrea Arcangeli7066f0f2018-10-26 15:10:40 -07002238 page_add_anon_rmap(new_page, vma, start, true);
Andrea Arcangelid7c33932018-10-26 15:10:36 -07002239 /*
2240 * At this point the pmd is numa/protnone (i.e. non present) and the TLB
2241 * has already been flushed globally. So no TLB can be currently
2242 * caching this non present pmd mapping. There's no need to clear the
2243 * pmd before doing set_pmd_at(), nor to flush the TLB after
2244 * set_pmd_at(). Clearing the pmd here would introduce a race
2245 * condition against MADV_DONTNEED, because MADV_DONTNEED only holds the
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002246 * mmap_lock for reading. If the pmd is set to NULL at any given time,
Andrea Arcangelid7c33932018-10-26 15:10:36 -07002247 * MADV_DONTNEED won't wait on the pmd lock and it'll skip clearing this
2248 * pmd.
2249 */
Andrea Arcangeli7066f0f2018-10-26 15:10:40 -07002250 set_pmd_at(mm, start, pmd, entry);
Stephen Rothwellce4a9cc2012-12-10 19:50:57 +11002251 update_mmu_cache_pmd(vma, address, &entry);
Mel Gorman2b4847e2013-12-18 17:08:32 -08002252
Will Deaconf4e177d2017-07-10 15:48:31 -07002253 page_ref_unfreeze(page, 2);
Hugh Dickins51afb122015-11-05 18:49:37 -08002254 mlock_migrate_page(new_page, page);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002255 page_remove_rmap(page, true);
Vlastimil Babka7cd12b42016-03-15 14:56:18 -07002256 set_page_owner_migrate_reason(new_page, MR_NUMA_MISPLACED);
Mel Gorman2b4847e2013-12-18 17:08:32 -08002257
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002258 spin_unlock(ptl);
Mel Gormanb32967f2012-11-19 12:35:47 +00002259
Mel Gorman11de9922014-06-04 16:07:41 -07002260 /* Take an "isolate" reference and put new page on the LRU. */
2261 get_page(new_page);
2262 putback_lru_page(new_page);
2263
Mel Gormanb32967f2012-11-19 12:35:47 +00002264 unlock_page(new_page);
2265 unlock_page(page);
2266 put_page(page); /* Drop the rmap reference */
2267 put_page(page); /* Drop the LRU isolation reference */
2268
2269 count_vm_events(PGMIGRATE_SUCCESS, HPAGE_PMD_NR);
2270 count_vm_numa_events(NUMA_PAGE_MIGRATE, HPAGE_PMD_NR);
2271
Mel Gorman599d0c92016-07-28 15:45:31 -07002272 mod_node_page_state(page_pgdat(page),
Mel Gormanb32967f2012-11-19 12:35:47 +00002273 NR_ISOLATED_ANON + page_lru,
2274 -HPAGE_PMD_NR);
2275 return isolated;
2276
Hugh Dickins340ef392013-02-22 16:34:33 -08002277out_fail:
2278 count_vm_events(PGMIGRATE_FAIL, HPAGE_PMD_NR);
Mel Gorman2b4847e2013-12-18 17:08:32 -08002279 ptl = pmd_lock(mm, pmd);
2280 if (pmd_same(*pmd, entry)) {
Mel Gorman4d942462015-02-12 14:58:28 -08002281 entry = pmd_modify(entry, vma->vm_page_prot);
Andrea Arcangeli7066f0f2018-10-26 15:10:40 -07002282 set_pmd_at(mm, start, pmd, entry);
Mel Gorman2b4847e2013-12-18 17:08:32 -08002283 update_mmu_cache_pmd(vma, address, &entry);
2284 }
2285 spin_unlock(ptl);
Mel Gormana54a4072013-10-07 11:28:46 +01002286
Mel Gormaneb4489f62013-12-18 17:08:39 -08002287out_unlock:
Hugh Dickins340ef392013-02-22 16:34:33 -08002288 unlock_page(page);
Yang Shic77c5cb2020-12-14 19:13:09 -08002289out:
Mel Gormanb32967f2012-11-19 12:35:47 +00002290 put_page(page);
Mel Gormanb32967f2012-11-19 12:35:47 +00002291 return 0;
2292}
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002293#endif /* CONFIG_NUMA_BALANCING */
2294
2295#endif /* CONFIG_NUMA */
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002296
Christoph Hellwig9b2ed9c2019-08-14 09:59:28 +02002297#ifdef CONFIG_DEVICE_PRIVATE
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002298static int migrate_vma_collect_hole(unsigned long start,
2299 unsigned long end,
Steven Priceb7a16c72020-02-03 17:36:03 -08002300 __always_unused int depth,
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002301 struct mm_walk *walk)
2302{
2303 struct migrate_vma *migrate = walk->private;
2304 unsigned long addr;
2305
Ralph Campbell0744f282020-08-11 18:31:41 -07002306 /* Only allow populating anonymous memory. */
2307 if (!vma_is_anonymous(walk->vma)) {
2308 for (addr = start; addr < end; addr += PAGE_SIZE) {
2309 migrate->src[migrate->npages] = 0;
2310 migrate->dst[migrate->npages] = 0;
2311 migrate->npages++;
2312 }
2313 return 0;
2314 }
2315
Ralph Campbell872ea702020-01-30 22:14:38 -08002316 for (addr = start; addr < end; addr += PAGE_SIZE) {
Mark Hairgrovee20d1032017-10-13 15:57:30 -07002317 migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002318 migrate->dst[migrate->npages] = 0;
Mark Hairgrovee20d1032017-10-13 15:57:30 -07002319 migrate->npages++;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002320 migrate->cpages++;
2321 }
2322
2323 return 0;
2324}
2325
2326static int migrate_vma_collect_skip(unsigned long start,
2327 unsigned long end,
2328 struct mm_walk *walk)
2329{
2330 struct migrate_vma *migrate = walk->private;
2331 unsigned long addr;
2332
Ralph Campbell872ea702020-01-30 22:14:38 -08002333 for (addr = start; addr < end; addr += PAGE_SIZE) {
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002334 migrate->dst[migrate->npages] = 0;
2335 migrate->src[migrate->npages++] = 0;
2336 }
2337
2338 return 0;
2339}
2340
2341static int migrate_vma_collect_pmd(pmd_t *pmdp,
2342 unsigned long start,
2343 unsigned long end,
2344 struct mm_walk *walk)
2345{
2346 struct migrate_vma *migrate = walk->private;
2347 struct vm_area_struct *vma = walk->vma;
2348 struct mm_struct *mm = vma->vm_mm;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002349 unsigned long addr = start, unmapped = 0;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002350 spinlock_t *ptl;
2351 pte_t *ptep;
2352
2353again:
2354 if (pmd_none(*pmdp))
Steven Priceb7a16c72020-02-03 17:36:03 -08002355 return migrate_vma_collect_hole(start, end, -1, walk);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002356
2357 if (pmd_trans_huge(*pmdp)) {
2358 struct page *page;
2359
2360 ptl = pmd_lock(mm, pmdp);
2361 if (unlikely(!pmd_trans_huge(*pmdp))) {
2362 spin_unlock(ptl);
2363 goto again;
2364 }
2365
2366 page = pmd_page(*pmdp);
2367 if (is_huge_zero_page(page)) {
2368 spin_unlock(ptl);
2369 split_huge_pmd(vma, pmdp, addr);
2370 if (pmd_trans_unstable(pmdp))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002371 return migrate_vma_collect_skip(start, end,
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002372 walk);
2373 } else {
2374 int ret;
2375
2376 get_page(page);
2377 spin_unlock(ptl);
2378 if (unlikely(!trylock_page(page)))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002379 return migrate_vma_collect_skip(start, end,
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002380 walk);
2381 ret = split_huge_page(page);
2382 unlock_page(page);
2383 put_page(page);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002384 if (ret)
2385 return migrate_vma_collect_skip(start, end,
2386 walk);
2387 if (pmd_none(*pmdp))
Steven Priceb7a16c72020-02-03 17:36:03 -08002388 return migrate_vma_collect_hole(start, end, -1,
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002389 walk);
2390 }
2391 }
2392
2393 if (unlikely(pmd_bad(*pmdp)))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002394 return migrate_vma_collect_skip(start, end, walk);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002395
2396 ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002397 arch_enter_lazy_mmu_mode();
2398
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002399 for (; addr < end; addr += PAGE_SIZE, ptep++) {
Christoph Hellwig800bb1c2020-03-16 20:32:14 +01002400 unsigned long mpfn = 0, pfn;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002401 struct page *page;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002402 swp_entry_t entry;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002403 pte_t pte;
2404
2405 pte = *ptep;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002406
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002407 if (pte_none(pte)) {
Ralph Campbell0744f282020-08-11 18:31:41 -07002408 if (vma_is_anonymous(vma)) {
2409 mpfn = MIGRATE_PFN_MIGRATE;
2410 migrate->cpages++;
2411 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002412 goto next;
2413 }
2414
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002415 if (!pte_present(pte)) {
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002416 /*
2417 * Only care about unaddressable device page special
2418 * page table entry. Other special swap entries are not
2419 * migratable, and we ignore regular swapped page.
2420 */
2421 entry = pte_to_swp_entry(pte);
2422 if (!is_device_private_entry(entry))
2423 goto next;
2424
2425 page = device_private_entry_to_page(entry);
Ralph Campbell51431922020-07-23 15:30:00 -07002426 if (!(migrate->flags &
2427 MIGRATE_VMA_SELECT_DEVICE_PRIVATE) ||
2428 page->pgmap->owner != migrate->pgmap_owner)
Christoph Hellwig800bb1c2020-03-16 20:32:14 +01002429 goto next;
2430
Christoph Hellwig06d462b2019-08-14 09:59:27 +02002431 mpfn = migrate_pfn(page_to_pfn(page)) |
2432 MIGRATE_PFN_MIGRATE;
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002433 if (is_write_device_private_entry(entry))
2434 mpfn |= MIGRATE_PFN_WRITE;
2435 } else {
Ralph Campbell51431922020-07-23 15:30:00 -07002436 if (!(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM))
Christoph Hellwig800bb1c2020-03-16 20:32:14 +01002437 goto next;
Pingfan Liu276f7562019-09-23 15:37:38 -07002438 pfn = pte_pfn(pte);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002439 if (is_zero_pfn(pfn)) {
2440 mpfn = MIGRATE_PFN_MIGRATE;
2441 migrate->cpages++;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002442 goto next;
2443 }
Christoph Hellwig25b29952019-06-13 22:50:49 +02002444 page = vm_normal_page(migrate->vma, addr, pte);
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002445 mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
2446 mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0;
2447 }
2448
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002449 /* FIXME support THP */
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002450 if (!page || !page->mapping || PageTransCompound(page)) {
Pingfan Liu276f7562019-09-23 15:37:38 -07002451 mpfn = 0;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002452 goto next;
2453 }
2454
2455 /*
2456 * By getting a reference on the page we pin it and that blocks
2457 * any kind of migration. Side effect is that it "freezes" the
2458 * pte.
2459 *
2460 * We drop this reference after isolating the page from the lru
2461 * for non device page (device page are not on the lru and thus
2462 * can't be dropped from it).
2463 */
2464 get_page(page);
2465 migrate->cpages++;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002466
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002467 /*
2468 * Optimize for the common case where page is only mapped once
2469 * in one process. If we can lock the page, then we can safely
2470 * set up a special migration page table entry now.
2471 */
2472 if (trylock_page(page)) {
2473 pte_t swp_pte;
2474
2475 mpfn |= MIGRATE_PFN_LOCKED;
2476 ptep_get_and_clear(mm, addr, ptep);
2477
2478 /* Setup special migration page table entry */
Ralph Campbell07707122018-04-10 16:29:27 -07002479 entry = make_migration_entry(page, mpfn &
2480 MIGRATE_PFN_WRITE);
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002481 swp_pte = swp_entry_to_pte(entry);
Alistair Popplead7df762020-09-04 16:36:01 -07002482 if (pte_present(pte)) {
2483 if (pte_soft_dirty(pte))
2484 swp_pte = pte_swp_mksoft_dirty(swp_pte);
2485 if (pte_uffd_wp(pte))
2486 swp_pte = pte_swp_mkuffd_wp(swp_pte);
2487 } else {
2488 if (pte_swp_soft_dirty(pte))
2489 swp_pte = pte_swp_mksoft_dirty(swp_pte);
2490 if (pte_swp_uffd_wp(pte))
2491 swp_pte = pte_swp_mkuffd_wp(swp_pte);
2492 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002493 set_pte_at(mm, addr, ptep, swp_pte);
2494
2495 /*
2496 * This is like regular unmap: we remove the rmap and
2497 * drop page refcount. Page won't be freed, as we took
2498 * a reference just above.
2499 */
2500 page_remove_rmap(page, false);
2501 put_page(page);
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002502
2503 if (pte_present(pte))
2504 unmapped++;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002505 }
2506
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002507next:
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002508 migrate->dst[migrate->npages] = 0;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002509 migrate->src[migrate->npages++] = mpfn;
2510 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002511 arch_leave_lazy_mmu_mode();
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002512 pte_unmap_unlock(ptep - 1, ptl);
2513
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002514 /* Only flush the TLB if we actually modified any entries */
2515 if (unmapped)
2516 flush_tlb_range(walk->vma, start, end);
2517
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002518 return 0;
2519}
2520
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02002521static const struct mm_walk_ops migrate_vma_walk_ops = {
2522 .pmd_entry = migrate_vma_collect_pmd,
2523 .pte_hole = migrate_vma_collect_hole,
2524};
2525
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002526/*
2527 * migrate_vma_collect() - collect pages over a range of virtual addresses
2528 * @migrate: migrate struct containing all migration information
2529 *
2530 * This will walk the CPU page table. For each virtual address backed by a
2531 * valid page, it updates the src array and takes a reference on the page, in
2532 * order to pin the page until we lock it and unmap it.
2533 */
2534static void migrate_vma_collect(struct migrate_vma *migrate)
2535{
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002536 struct mmu_notifier_range range;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002537
Ralph Campbell998427b2020-07-23 15:30:01 -07002538 /*
2539 * Note that the pgmap_owner is passed to the mmu notifier callback so
2540 * that the registered device driver can skip invalidating device
2541 * private page mappings that won't be migrated.
2542 */
Ralph Campbellc1a06df2020-08-06 23:17:09 -07002543 mmu_notifier_range_init_migrate(&range, 0, migrate->vma,
2544 migrate->vma->vm_mm, migrate->start, migrate->end,
2545 migrate->pgmap_owner);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002546 mmu_notifier_invalidate_range_start(&range);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002547
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02002548 walk_page_range(migrate->vma->vm_mm, migrate->start, migrate->end,
2549 &migrate_vma_walk_ops, migrate);
2550
2551 mmu_notifier_invalidate_range_end(&range);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002552 migrate->end = migrate->start + (migrate->npages << PAGE_SHIFT);
2553}
2554
2555/*
2556 * migrate_vma_check_page() - check if page is pinned or not
2557 * @page: struct page to check
2558 *
2559 * Pinned pages cannot be migrated. This is the same test as in
2560 * migrate_page_move_mapping(), except that here we allow migration of a
2561 * ZONE_DEVICE page.
2562 */
2563static bool migrate_vma_check_page(struct page *page)
2564{
2565 /*
2566 * One extra ref because caller holds an extra reference, either from
2567 * isolate_lru_page() for a regular page, or migrate_vma_collect() for
2568 * a device page.
2569 */
2570 int extra = 1;
2571
2572 /*
2573 * FIXME support THP (transparent huge page), it is bit more complex to
2574 * check them than regular pages, because they can be mapped with a pmd
2575 * or with a pte (split pte mapping).
2576 */
2577 if (PageCompound(page))
2578 return false;
2579
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002580 /* Page from ZONE_DEVICE have one extra reference */
2581 if (is_zone_device_page(page)) {
2582 /*
2583 * Private page can never be pin as they have no valid pte and
2584 * GUP will fail for those. Yet if there is a pending migration
2585 * a thread might try to wait on the pte migration entry and
2586 * will bump the page reference count. Sadly there is no way to
2587 * differentiate a regular pin from migration wait. Hence to
2588 * avoid 2 racing thread trying to migrate back to CPU to enter
Haitao Shi8958b242020-12-15 20:47:26 -08002589 * infinite loop (one stopping migration because the other is
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002590 * waiting on pte migration entry). We always return true here.
2591 *
2592 * FIXME proper solution is to rework migration_entry_wait() so
2593 * it does not need to take a reference on page.
2594 */
Christoph Hellwig25b29952019-06-13 22:50:49 +02002595 return is_device_private_page(page);
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002596 }
2597
Jérôme Glissedf6ad692017-09-08 16:12:24 -07002598 /* For file back page */
2599 if (page_mapping(page))
2600 extra += 1 + page_has_private(page);
2601
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002602 if ((page_count(page) - extra) > page_mapcount(page))
2603 return false;
2604
2605 return true;
2606}
2607
2608/*
2609 * migrate_vma_prepare() - lock pages and isolate them from the lru
2610 * @migrate: migrate struct containing all migration information
2611 *
2612 * This locks pages that have been collected by migrate_vma_collect(). Once each
2613 * page is locked it is isolated from the lru (for non-device pages). Finally,
2614 * the ref taken by migrate_vma_collect() is dropped, as locked pages cannot be
2615 * migrated by concurrent kernel threads.
2616 */
2617static void migrate_vma_prepare(struct migrate_vma *migrate)
2618{
2619 const unsigned long npages = migrate->npages;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002620 const unsigned long start = migrate->start;
2621 unsigned long addr, i, restore = 0;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002622 bool allow_drain = true;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002623
2624 lru_add_drain();
2625
2626 for (i = 0; (i < npages) && migrate->cpages; i++) {
2627 struct page *page = migrate_pfn_to_page(migrate->src[i]);
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002628 bool remap = true;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002629
2630 if (!page)
2631 continue;
2632
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002633 if (!(migrate->src[i] & MIGRATE_PFN_LOCKED)) {
2634 /*
2635 * Because we are migrating several pages there can be
2636 * a deadlock between 2 concurrent migration where each
2637 * are waiting on each other page lock.
2638 *
2639 * Make migrate_vma() a best effort thing and backoff
2640 * for any page we can not lock right away.
2641 */
2642 if (!trylock_page(page)) {
2643 migrate->src[i] = 0;
2644 migrate->cpages--;
2645 put_page(page);
2646 continue;
2647 }
2648 remap = false;
2649 migrate->src[i] |= MIGRATE_PFN_LOCKED;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002650 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002651
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002652 /* ZONE_DEVICE pages are not on LRU */
2653 if (!is_zone_device_page(page)) {
2654 if (!PageLRU(page) && allow_drain) {
2655 /* Drain CPU's pagevec */
2656 lru_add_drain_all();
2657 allow_drain = false;
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002658 }
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002659
2660 if (isolate_lru_page(page)) {
2661 if (remap) {
2662 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2663 migrate->cpages--;
2664 restore++;
2665 } else {
2666 migrate->src[i] = 0;
2667 unlock_page(page);
2668 migrate->cpages--;
2669 put_page(page);
2670 }
2671 continue;
2672 }
2673
2674 /* Drop the reference we took in collect */
2675 put_page(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002676 }
2677
2678 if (!migrate_vma_check_page(page)) {
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002679 if (remap) {
2680 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2681 migrate->cpages--;
2682 restore++;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002683
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002684 if (!is_zone_device_page(page)) {
2685 get_page(page);
2686 putback_lru_page(page);
2687 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002688 } else {
2689 migrate->src[i] = 0;
2690 unlock_page(page);
2691 migrate->cpages--;
2692
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002693 if (!is_zone_device_page(page))
2694 putback_lru_page(page);
2695 else
2696 put_page(page);
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002697 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002698 }
2699 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002700
2701 for (i = 0, addr = start; i < npages && restore; i++, addr += PAGE_SIZE) {
2702 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2703
2704 if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE))
2705 continue;
2706
2707 remove_migration_pte(page, migrate->vma, addr, page);
2708
2709 migrate->src[i] = 0;
2710 unlock_page(page);
2711 put_page(page);
2712 restore--;
2713 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002714}
2715
2716/*
2717 * migrate_vma_unmap() - replace page mapping with special migration pte entry
2718 * @migrate: migrate struct containing all migration information
2719 *
2720 * Replace page mapping (CPU page table pte) with a special migration pte entry
2721 * and check again if it has been pinned. Pinned pages are restored because we
2722 * cannot migrate them.
2723 *
2724 * This is the last step before we call the device driver callback to allocate
2725 * destination memory and copy contents of original page over to new page.
2726 */
2727static void migrate_vma_unmap(struct migrate_vma *migrate)
2728{
Shakeel Butt013339d2020-12-14 19:06:39 -08002729 int flags = TTU_MIGRATION | TTU_IGNORE_MLOCK;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002730 const unsigned long npages = migrate->npages;
2731 const unsigned long start = migrate->start;
2732 unsigned long addr, i, restore = 0;
2733
2734 for (i = 0; i < npages; i++) {
2735 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2736
2737 if (!page || !(migrate->src[i] & MIGRATE_PFN_MIGRATE))
2738 continue;
2739
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002740 if (page_mapped(page)) {
2741 try_to_unmap(page, flags);
2742 if (page_mapped(page))
2743 goto restore;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002744 }
Jérôme Glisse8c3328f2017-09-08 16:12:13 -07002745
2746 if (migrate_vma_check_page(page))
2747 continue;
2748
2749restore:
2750 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
2751 migrate->cpages--;
2752 restore++;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002753 }
2754
2755 for (addr = start, i = 0; i < npages && restore; addr += PAGE_SIZE, i++) {
2756 struct page *page = migrate_pfn_to_page(migrate->src[i]);
2757
2758 if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE))
2759 continue;
2760
2761 remove_migration_ptes(page, page, false);
2762
2763 migrate->src[i] = 0;
2764 unlock_page(page);
2765 restore--;
2766
Jérôme Glissea5430dd2017-09-08 16:12:17 -07002767 if (is_zone_device_page(page))
2768 put_page(page);
2769 else
2770 putback_lru_page(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07002771 }
2772}
2773
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002774/**
2775 * migrate_vma_setup() - prepare to migrate a range of memory
Randy Dunlapeaf444d2020-08-11 18:33:08 -07002776 * @args: contains the vma, start, and pfns arrays for the migration
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002777 *
2778 * Returns: negative errno on failures, 0 when 0 or more pages were migrated
2779 * without an error.
2780 *
2781 * Prepare to migrate a range of memory virtual address range by collecting all
2782 * the pages backing each virtual address in the range, saving them inside the
2783 * src array. Then lock those pages and unmap them. Once the pages are locked
2784 * and unmapped, check whether each page is pinned or not. Pages that aren't
2785 * pinned have the MIGRATE_PFN_MIGRATE flag set (by this function) in the
2786 * corresponding src array entry. Then restores any pages that are pinned, by
2787 * remapping and unlocking those pages.
2788 *
2789 * The caller should then allocate destination memory and copy source memory to
2790 * it for all those entries (ie with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE
2791 * flag set). Once these are allocated and copied, the caller must update each
2792 * corresponding entry in the dst array with the pfn value of the destination
2793 * page and with the MIGRATE_PFN_VALID and MIGRATE_PFN_LOCKED flags set
2794 * (destination pages must have their struct pages locked, via lock_page()).
2795 *
2796 * Note that the caller does not have to migrate all the pages that are marked
2797 * with MIGRATE_PFN_MIGRATE flag in src array unless this is a migration from
2798 * device memory to system memory. If the caller cannot migrate a device page
2799 * back to system memory, then it must return VM_FAULT_SIGBUS, which has severe
2800 * consequences for the userspace process, so it must be avoided if at all
2801 * possible.
2802 *
2803 * For empty entries inside CPU page table (pte_none() or pmd_none() is true) we
2804 * do set MIGRATE_PFN_MIGRATE flag inside the corresponding source array thus
2805 * allowing the caller to allocate device memory for those unback virtual
2806 * address. For this the caller simply has to allocate device memory and
2807 * properly set the destination entry like for regular migration. Note that
2808 * this can still fails and thus inside the device driver must check if the
2809 * migration was successful for those entries after calling migrate_vma_pages()
2810 * just like for regular migration.
2811 *
2812 * After that, the callers must call migrate_vma_pages() to go over each entry
2813 * in the src array that has the MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag
2814 * set. If the corresponding entry in dst array has MIGRATE_PFN_VALID flag set,
2815 * then migrate_vma_pages() to migrate struct page information from the source
2816 * struct page to the destination struct page. If it fails to migrate the
2817 * struct page information, then it clears the MIGRATE_PFN_MIGRATE flag in the
2818 * src array.
2819 *
2820 * At this point all successfully migrated pages have an entry in the src
2821 * array with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag set and the dst
2822 * array entry with MIGRATE_PFN_VALID flag set.
2823 *
2824 * Once migrate_vma_pages() returns the caller may inspect which pages were
2825 * successfully migrated, and which were not. Successfully migrated pages will
2826 * have the MIGRATE_PFN_MIGRATE flag set for their src array entry.
2827 *
2828 * It is safe to update device page table after migrate_vma_pages() because
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002829 * both destination and source page are still locked, and the mmap_lock is held
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02002830 * in read mode (hence no one can unmap the range being migrated).
2831 *
2832 * Once the caller is done cleaning up things and updating its page table (if it
2833 * chose to do so, this is not an obligation) it finally calls
2834 * migrate_vma_finalize() to update the CPU page table to point to new pages
2835 * for successfully migrated pages or otherwise restore the CPU page table to
2836 * point to the original source pages.
2837 */
2838int migrate_vma_setup(struct migrate_vma *args)
2839{
2840 long nr_pages = (args->end - args->start) >> PAGE_SHIFT;
2841
2842 args->start &= PAGE_MASK;
2843 args->end &= PAGE_MASK;
2844 if (!args->vma || is_vm_hugetlb_page(args->vma) ||
2845 (args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma))
2846 return -EINVAL;
2847 if (nr_pages <= 0)
2848 return -EINVAL;
2849 if (args->start < args->vma->vm_start ||
2850 args->start >= args->vma->vm_end)
2851 return -EINVAL;
2852 if (args->end <= args->vma->vm_start || args->end > args->vma->vm_end)
2853 return -EINVAL;
2854 if (!args->src || !args->dst)
2855 return -EINVAL;
2856
2857 memset(args->src, 0, sizeof(*args->src) * nr_pages);
2858 args->cpages = 0;
2859 args->npages = 0;
2860
2861 migrate_vma_collect(args);
2862
2863 if (args->cpages)
2864 migrate_vma_prepare(args);
2865 if (args->cpages)
2866 migrate_vma_unmap(args);
2867
2868 /*
2869 * At this point pages are locked and unmapped, and thus they have
2870 * stable content and can safely be copied to destination memory that
2871 * is allocated by the drivers.
2872 */
2873 return 0;
2874
2875}
2876EXPORT_SYMBOL(migrate_vma_setup);
2877
Ralph Campbell34290e22020-01-30 22:14:44 -08002878/*
2879 * This code closely matches the code in:
2880 * __handle_mm_fault()
2881 * handle_pte_fault()
2882 * do_anonymous_page()
2883 * to map in an anonymous zero page but the struct page will be a ZONE_DEVICE
2884 * private page.
2885 */
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002886static void migrate_vma_insert_page(struct migrate_vma *migrate,
2887 unsigned long addr,
2888 struct page *page,
Stephen Zhangd85c6db2020-12-14 19:13:20 -08002889 unsigned long *src)
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002890{
2891 struct vm_area_struct *vma = migrate->vma;
2892 struct mm_struct *mm = vma->vm_mm;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002893 bool flush = false;
2894 spinlock_t *ptl;
2895 pte_t entry;
2896 pgd_t *pgdp;
2897 p4d_t *p4dp;
2898 pud_t *pudp;
2899 pmd_t *pmdp;
2900 pte_t *ptep;
2901
2902 /* Only allow populating anonymous memory */
2903 if (!vma_is_anonymous(vma))
2904 goto abort;
2905
2906 pgdp = pgd_offset(mm, addr);
2907 p4dp = p4d_alloc(mm, pgdp, addr);
2908 if (!p4dp)
2909 goto abort;
2910 pudp = pud_alloc(mm, p4dp, addr);
2911 if (!pudp)
2912 goto abort;
2913 pmdp = pmd_alloc(mm, pudp, addr);
2914 if (!pmdp)
2915 goto abort;
2916
2917 if (pmd_trans_huge(*pmdp) || pmd_devmap(*pmdp))
2918 goto abort;
2919
2920 /*
2921 * Use pte_alloc() instead of pte_alloc_map(). We can't run
2922 * pte_offset_map() on pmds where a huge pmd might be created
2923 * from a different thread.
2924 *
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002925 * pte_alloc_map() is safe to use under mmap_write_lock(mm) or when
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002926 * parallel threads are excluded by other means.
2927 *
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002928 * Here we only have mmap_read_lock(mm).
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002929 */
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -08002930 if (pte_alloc(mm, pmdp))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002931 goto abort;
2932
2933 /* See the comment in pte_alloc_one_map() */
2934 if (unlikely(pmd_trans_unstable(pmdp)))
2935 goto abort;
2936
2937 if (unlikely(anon_vma_prepare(vma)))
2938 goto abort;
Johannes Weinerd9eb1ea2020-06-03 16:02:24 -07002939 if (mem_cgroup_charge(page, vma->vm_mm, GFP_KERNEL))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002940 goto abort;
2941
2942 /*
2943 * The memory barrier inside __SetPageUptodate makes sure that
2944 * preceding stores to the page contents become visible before
2945 * the set_pte_at() write.
2946 */
2947 __SetPageUptodate(page);
2948
Jérôme Glissedf6ad692017-09-08 16:12:24 -07002949 if (is_zone_device_page(page)) {
2950 if (is_device_private_page(page)) {
2951 swp_entry_t swp_entry;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002952
Jérôme Glissedf6ad692017-09-08 16:12:24 -07002953 swp_entry = make_device_private_entry(page, vma->vm_flags & VM_WRITE);
2954 entry = swp_entry_to_pte(swp_entry);
Jérôme Glissedf6ad692017-09-08 16:12:24 -07002955 }
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002956 } else {
2957 entry = mk_pte(page, vma->vm_page_prot);
2958 if (vma->vm_flags & VM_WRITE)
2959 entry = pte_mkwrite(pte_mkdirty(entry));
2960 }
2961
2962 ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
2963
Ralph Campbell34290e22020-01-30 22:14:44 -08002964 if (check_stable_address_space(mm))
2965 goto unlock_abort;
2966
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002967 if (pte_present(*ptep)) {
2968 unsigned long pfn = pte_pfn(*ptep);
2969
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002970 if (!is_zero_pfn(pfn))
2971 goto unlock_abort;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002972 flush = true;
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002973 } else if (!pte_none(*ptep))
2974 goto unlock_abort;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002975
2976 /*
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002977 * Check for userfaultfd but do not deliver the fault. Instead,
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002978 * just back off.
2979 */
Ralph Campbellc23a0c92020-01-30 22:14:41 -08002980 if (userfaultfd_missing(vma))
2981 goto unlock_abort;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002982
2983 inc_mm_counter(mm, MM_ANONPAGES);
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07002984 page_add_new_anon_rmap(page, vma, addr, false);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002985 if (!is_zone_device_page(page))
Joonsoo Kimb5181542020-08-11 18:30:40 -07002986 lru_cache_add_inactive_or_unevictable(page, vma);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07002987 get_page(page);
2988
2989 if (flush) {
2990 flush_cache_page(vma, addr, pte_pfn(*ptep));
2991 ptep_clear_flush_notify(vma, addr, ptep);
2992 set_pte_at_notify(mm, addr, ptep, entry);
2993 update_mmu_cache(vma, addr, ptep);
2994 } else {
2995 /* No need to invalidate - it was non-present before */
2996 set_pte_at(mm, addr, ptep, entry);
2997 update_mmu_cache(vma, addr, ptep);
2998 }
2999
3000 pte_unmap_unlock(ptep, ptl);
3001 *src = MIGRATE_PFN_MIGRATE;
3002 return;
3003
Ralph Campbellc23a0c92020-01-30 22:14:41 -08003004unlock_abort:
3005 pte_unmap_unlock(ptep, ptl);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003006abort:
3007 *src &= ~MIGRATE_PFN_MIGRATE;
3008}
3009
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003010/**
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003011 * migrate_vma_pages() - migrate meta-data from src page to dst page
3012 * @migrate: migrate struct containing all migration information
3013 *
3014 * This migrates struct page meta-data from source struct page to destination
3015 * struct page. This effectively finishes the migration from source page to the
3016 * destination page.
3017 */
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003018void migrate_vma_pages(struct migrate_vma *migrate)
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003019{
3020 const unsigned long npages = migrate->npages;
3021 const unsigned long start = migrate->start;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003022 struct mmu_notifier_range range;
3023 unsigned long addr, i;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003024 bool notified = false;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003025
3026 for (i = 0, addr = start; i < npages; addr += PAGE_SIZE, i++) {
3027 struct page *newpage = migrate_pfn_to_page(migrate->dst[i]);
3028 struct page *page = migrate_pfn_to_page(migrate->src[i]);
3029 struct address_space *mapping;
3030 int r;
3031
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003032 if (!newpage) {
3033 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003034 continue;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003035 }
3036
3037 if (!page) {
Ralph Campbellc23a0c92020-01-30 22:14:41 -08003038 if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE))
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003039 continue;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003040 if (!notified) {
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003041 notified = true;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003042
Ralph Campbell5e5dda82020-12-14 19:12:55 -08003043 mmu_notifier_range_init_migrate(&range, 0,
3044 migrate->vma, migrate->vma->vm_mm,
3045 addr, migrate->end,
3046 migrate->pgmap_owner);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003047 mmu_notifier_invalidate_range_start(&range);
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003048 }
3049 migrate_vma_insert_page(migrate, addr, newpage,
Stephen Zhangd85c6db2020-12-14 19:13:20 -08003050 &migrate->src[i]);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003051 continue;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003052 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003053
3054 mapping = page_mapping(page);
3055
Jérôme Glissea5430dd2017-09-08 16:12:17 -07003056 if (is_zone_device_page(newpage)) {
3057 if (is_device_private_page(newpage)) {
3058 /*
3059 * For now only support private anonymous when
3060 * migrating to un-addressable device memory.
3061 */
3062 if (mapping) {
3063 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
3064 continue;
3065 }
Christoph Hellwig25b29952019-06-13 22:50:49 +02003066 } else {
Jérôme Glissea5430dd2017-09-08 16:12:17 -07003067 /*
3068 * Other types of ZONE_DEVICE page are not
3069 * supported.
3070 */
3071 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
3072 continue;
3073 }
3074 }
3075
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003076 r = migrate_page(mapping, newpage, page, MIGRATE_SYNC_NO_COPY);
3077 if (r != MIGRATEPAGE_SUCCESS)
3078 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
3079 }
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003080
Jérôme Glisse4645b9f2017-11-15 17:34:11 -08003081 /*
3082 * No need to double call mmu_notifier->invalidate_range() callback as
3083 * the above ptep_clear_flush_notify() inside migrate_vma_insert_page()
3084 * did already call it.
3085 */
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003086 if (notified)
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003087 mmu_notifier_invalidate_range_only_end(&range);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003088}
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003089EXPORT_SYMBOL(migrate_vma_pages);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003090
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003091/**
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003092 * migrate_vma_finalize() - restore CPU page table entry
3093 * @migrate: migrate struct containing all migration information
3094 *
3095 * This replaces the special migration pte entry with either a mapping to the
3096 * new page if migration was successful for that page, or to the original page
3097 * otherwise.
3098 *
3099 * This also unlocks the pages and puts them back on the lru, or drops the extra
3100 * refcount, for device pages.
3101 */
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003102void migrate_vma_finalize(struct migrate_vma *migrate)
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003103{
3104 const unsigned long npages = migrate->npages;
3105 unsigned long i;
3106
3107 for (i = 0; i < npages; i++) {
3108 struct page *newpage = migrate_pfn_to_page(migrate->dst[i]);
3109 struct page *page = migrate_pfn_to_page(migrate->src[i]);
3110
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003111 if (!page) {
3112 if (newpage) {
3113 unlock_page(newpage);
3114 put_page(newpage);
3115 }
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003116 continue;
Jérôme Glisse8315ada2017-09-08 16:12:21 -07003117 }
3118
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003119 if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE) || !newpage) {
3120 if (newpage) {
3121 unlock_page(newpage);
3122 put_page(newpage);
3123 }
3124 newpage = page;
3125 }
3126
3127 remove_migration_ptes(page, newpage, false);
3128 unlock_page(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003129
Jérôme Glissea5430dd2017-09-08 16:12:17 -07003130 if (is_zone_device_page(page))
3131 put_page(page);
3132 else
3133 putback_lru_page(page);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003134
3135 if (newpage != page) {
3136 unlock_page(newpage);
Jérôme Glissea5430dd2017-09-08 16:12:17 -07003137 if (is_zone_device_page(newpage))
3138 put_page(newpage);
3139 else
3140 putback_lru_page(newpage);
Jérôme Glisse8763cb42017-09-08 16:12:09 -07003141 }
3142 }
3143}
Christoph Hellwiga7d1f222019-08-14 09:59:19 +02003144EXPORT_SYMBOL(migrate_vma_finalize);
Christoph Hellwig9b2ed9c2019-08-14 09:59:28 +02003145#endif /* CONFIG_DEVICE_PRIVATE */