blob: 2c63ac06791bbdf0e382e669812fcca67bf3cc55 [file] [log] [blame]
Christoph Lameterb20a3502006-03-22 00:09:12 -08001/*
Hugh Dickins14e0f9b2015-11-05 18:49:43 -08002 * Memory Migration functionality - linux/mm/migrate.c
Christoph Lameterb20a3502006-03-22 00:09:12 -08003 *
4 * Copyright (C) 2006 Silicon Graphics, Inc., Christoph Lameter
5 *
6 * Page migration was first developed in the context of the memory hotplug
7 * project. The main authors of the migration code are:
8 *
9 * IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
10 * Hirokazu Takahashi <taka@valinux.co.jp>
11 * Dave Hansen <haveblue@us.ibm.com>
Christoph Lametercde53532008-07-04 09:59:22 -070012 * Christoph Lameter
Christoph Lameterb20a3502006-03-22 00:09:12 -080013 */
14
15#include <linux/migrate.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040016#include <linux/export.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080017#include <linux/swap.h>
Christoph Lameter06972122006-06-23 02:03:35 -070018#include <linux/swapops.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080019#include <linux/pagemap.h>
Christoph Lametere23ca002006-04-10 22:52:57 -070020#include <linux/buffer_head.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080021#include <linux/mm_inline.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070022#include <linux/nsproxy.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080023#include <linux/pagevec.h>
Hugh Dickinse9995ef2009-12-14 17:59:31 -080024#include <linux/ksm.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080025#include <linux/rmap.h>
26#include <linux/topology.h>
27#include <linux/cpu.h>
28#include <linux/cpuset.h>
Christoph Lameter04e62a22006-06-23 02:03:38 -070029#include <linux/writeback.h>
Christoph Lameter742755a2006-06-23 02:03:55 -070030#include <linux/mempolicy.h>
31#include <linux/vmalloc.h>
David Quigley86c3a762006-06-23 02:04:02 -070032#include <linux/security.h>
Hugh Dickins42cb14b2015-11-05 18:50:05 -080033#include <linux/backing-dev.h>
Minchan Kimbda807d2016-07-26 15:23:05 -070034#include <linux/compaction.h>
Adrian Bunk4f5ca262008-07-23 21:27:02 -070035#include <linux/syscalls.h>
Naoya Horiguchi290408d2010-09-08 10:19:35 +090036#include <linux/hugetlb.h>
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -070037#include <linux/hugetlb_cgroup.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/gfp.h>
Rafael Aquinibf6bddf12012-12-11 16:02:42 -080039#include <linux/balloon_compaction.h>
Mel Gormanf714f4f2013-12-18 17:08:33 -080040#include <linux/mmu_notifier.h>
Vladimir Davydov33c3fc72015-09-09 15:35:45 -070041#include <linux/page_idle.h>
Vlastimil Babkad435edc2016-03-15 14:56:15 -070042#include <linux/page_owner.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080043
Michal Nazarewicz0d1836c2010-12-21 17:24:26 -080044#include <asm/tlbflush.h>
45
Mel Gorman7b2a2d42012-10-19 14:07:31 +010046#define CREATE_TRACE_POINTS
47#include <trace/events/migrate.h>
48
Christoph Lameterb20a3502006-03-22 00:09:12 -080049#include "internal.h"
50
Christoph Lameterb20a3502006-03-22 00:09:12 -080051/*
Christoph Lameter742755a2006-06-23 02:03:55 -070052 * migrate_prep() needs to be called before we start compiling a list of pages
Mel Gorman748446b2010-05-24 14:32:27 -070053 * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is
54 * undesirable, use migrate_prep_local()
Christoph Lameterb20a3502006-03-22 00:09:12 -080055 */
56int migrate_prep(void)
57{
Christoph Lameterb20a3502006-03-22 00:09:12 -080058 /*
59 * Clear the LRU lists so pages can be isolated.
60 * Note that pages may be moved off the LRU after we have
61 * drained them. Those pages will fail to migrate like other
62 * pages that may be busy.
63 */
64 lru_add_drain_all();
65
66 return 0;
67}
68
Mel Gorman748446b2010-05-24 14:32:27 -070069/* Do the necessary work of migrate_prep but not if it involves other CPUs */
70int migrate_prep_local(void)
71{
72 lru_add_drain();
73
74 return 0;
75}
76
Yisheng Xie9e5bcd62017-02-24 14:57:29 -080077int isolate_movable_page(struct page *page, isolate_mode_t mode)
Minchan Kimbda807d2016-07-26 15:23:05 -070078{
79 struct address_space *mapping;
80
81 /*
82 * Avoid burning cycles with pages that are yet under __free_pages(),
83 * or just got freed under us.
84 *
85 * In case we 'win' a race for a movable page being freed under us and
86 * raise its refcount preventing __free_pages() from doing its job
87 * the put_page() at the end of this block will take care of
88 * release this page, thus avoiding a nasty leakage.
89 */
90 if (unlikely(!get_page_unless_zero(page)))
91 goto out;
92
93 /*
94 * Check PageMovable before holding a PG_lock because page's owner
95 * assumes anybody doesn't touch PG_lock of newly allocated page
96 * so unconditionally grapping the lock ruins page's owner side.
97 */
98 if (unlikely(!__PageMovable(page)))
99 goto out_putpage;
100 /*
101 * As movable pages are not isolated from LRU lists, concurrent
102 * compaction threads can race against page migration functions
103 * as well as race against the releasing a page.
104 *
105 * In order to avoid having an already isolated movable page
106 * being (wrongly) re-isolated while it is under migration,
107 * or to avoid attempting to isolate pages being released,
108 * lets be sure we have the page lock
109 * before proceeding with the movable page isolation steps.
110 */
111 if (unlikely(!trylock_page(page)))
112 goto out_putpage;
113
114 if (!PageMovable(page) || PageIsolated(page))
115 goto out_no_isolated;
116
117 mapping = page_mapping(page);
118 VM_BUG_ON_PAGE(!mapping, page);
119
120 if (!mapping->a_ops->isolate_page(page, mode))
121 goto out_no_isolated;
122
123 /* Driver shouldn't use PG_isolated bit of page->flags */
124 WARN_ON_ONCE(PageIsolated(page));
125 __SetPageIsolated(page);
126 unlock_page(page);
127
Yisheng Xie9e5bcd62017-02-24 14:57:29 -0800128 return 0;
Minchan Kimbda807d2016-07-26 15:23:05 -0700129
130out_no_isolated:
131 unlock_page(page);
132out_putpage:
133 put_page(page);
134out:
Yisheng Xie9e5bcd62017-02-24 14:57:29 -0800135 return -EBUSY;
Minchan Kimbda807d2016-07-26 15:23:05 -0700136}
137
138/* It should be called on page which is PG_movable */
139void putback_movable_page(struct page *page)
140{
141 struct address_space *mapping;
142
143 VM_BUG_ON_PAGE(!PageLocked(page), page);
144 VM_BUG_ON_PAGE(!PageMovable(page), page);
145 VM_BUG_ON_PAGE(!PageIsolated(page), page);
146
147 mapping = page_mapping(page);
148 mapping->a_ops->putback_page(page);
149 __ClearPageIsolated(page);
150}
151
Christoph Lameterb20a3502006-03-22 00:09:12 -0800152/*
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800153 * Put previously isolated pages back onto the appropriate lists
154 * from where they were once taken off for compaction/migration.
155 *
Joonsoo Kim59c82b72014-01-21 15:51:17 -0800156 * This function shall be used whenever the isolated pageset has been
157 * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range()
158 * and isolate_huge_page().
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800159 */
160void putback_movable_pages(struct list_head *l)
161{
162 struct page *page;
163 struct page *page2;
164
165 list_for_each_entry_safe(page, page2, l, lru) {
Naoya Horiguchi31caf662013-09-11 14:21:59 -0700166 if (unlikely(PageHuge(page))) {
167 putback_active_hugepage(page);
168 continue;
169 }
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800170 list_del(&page->lru);
Minchan Kimbda807d2016-07-26 15:23:05 -0700171 /*
172 * We isolated non-lru movable page so here we can use
173 * __PageMovable because LRU page's mapping cannot have
174 * PAGE_MAPPING_MOVABLE.
175 */
Minchan Kimb1123ea62016-07-26 15:23:09 -0700176 if (unlikely(__PageMovable(page))) {
Minchan Kimbda807d2016-07-26 15:23:05 -0700177 VM_BUG_ON_PAGE(!PageIsolated(page), page);
178 lock_page(page);
179 if (PageMovable(page))
180 putback_movable_page(page);
181 else
182 __ClearPageIsolated(page);
183 unlock_page(page);
184 put_page(page);
185 } else {
Rafael Aquinibf6bddf12012-12-11 16:02:42 -0800186 putback_lru_page(page);
Ming Ling6afcf8e2016-12-12 16:42:26 -0800187 dec_node_page_state(page, NR_ISOLATED_ANON +
188 page_is_file_cache(page));
Minchan Kimbda807d2016-07-26 15:23:05 -0700189 }
Christoph Lameterb20a3502006-03-22 00:09:12 -0800190 }
Christoph Lameterb20a3502006-03-22 00:09:12 -0800191}
192
Christoph Lameter06972122006-06-23 02:03:35 -0700193/*
194 * Restore a potential migration pte to a working pte entry
195 */
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800196static int remove_migration_pte(struct page *page, struct vm_area_struct *vma,
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800197 unsigned long addr, void *old)
Christoph Lameter06972122006-06-23 02:03:35 -0700198{
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800199 struct page_vma_mapped_walk pvmw = {
200 .page = old,
201 .vma = vma,
202 .address = addr,
203 .flags = PVMW_SYNC | PVMW_MIGRATION,
204 };
205 struct page *new;
206 pte_t pte;
Christoph Lameter06972122006-06-23 02:03:35 -0700207 swp_entry_t entry;
Christoph Lameter06972122006-06-23 02:03:35 -0700208
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800209 VM_BUG_ON_PAGE(PageTail(page), page);
210 while (page_vma_mapped_walk(&pvmw)) {
211 new = page - pvmw.page->index +
212 linear_page_index(vma, pvmw.address);
Christoph Lameter06972122006-06-23 02:03:35 -0700213
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800214 get_page(new);
215 pte = pte_mkold(mk_pte(new, READ_ONCE(vma->vm_page_prot)));
216 if (pte_swp_soft_dirty(*pvmw.pte))
217 pte = pte_mksoft_dirty(pte);
Christoph Lameter06972122006-06-23 02:03:35 -0700218
Hugh Dickins486cf462011-10-19 12:50:35 -0700219 /*
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800220 * Recheck VMA as permissions can change since migration started
Hugh Dickins486cf462011-10-19 12:50:35 -0700221 */
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800222 entry = pte_to_swp_entry(*pvmw.pte);
223 if (is_write_migration_entry(entry))
224 pte = maybe_mkwrite(pte, vma);
Mel Gormand3cb8bf2014-10-02 19:47:41 +0100225
Andi Kleen3ef8fd72010-10-11 16:03:21 +0200226#ifdef CONFIG_HUGETLB_PAGE
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800227 if (PageHuge(new)) {
228 pte = pte_mkhuge(pte);
229 pte = arch_make_huge_pte(pte, vma, new, 0);
230 }
Andi Kleen3ef8fd72010-10-11 16:03:21 +0200231#endif
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800232 flush_dcache_page(new);
233 set_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700234
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800235 if (PageHuge(new)) {
236 if (PageAnon(new))
237 hugepage_add_anon_rmap(new, vma, pvmw.address);
238 else
239 page_dup_rmap(new, true);
240 } else if (PageAnon(new))
241 page_add_anon_rmap(new, vma, pvmw.address, false);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900242 else
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800243 page_add_file_rmap(new, false);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700244
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800245 if (vma->vm_flags & VM_LOCKED && !PageTransCompound(new))
246 mlock_vma_page(new);
Hugh Dickins51afb122015-11-05 18:49:37 -0800247
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800248 /* No need to invalidate - it was non-present before */
249 update_mmu_cache(vma, pvmw.address, pvmw.pte);
250 }
251
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800252 return SWAP_AGAIN;
Christoph Lameter06972122006-06-23 02:03:35 -0700253}
254
255/*
Christoph Lameter04e62a22006-06-23 02:03:38 -0700256 * Get rid of all migration entries and replace them by
257 * references to the indicated page.
258 */
Kirill A. Shutemove3884662016-03-17 14:20:07 -0700259void remove_migration_ptes(struct page *old, struct page *new, bool locked)
Christoph Lameter04e62a22006-06-23 02:03:38 -0700260{
Joonsoo Kim051ac832014-01-21 15:49:48 -0800261 struct rmap_walk_control rwc = {
262 .rmap_one = remove_migration_pte,
263 .arg = old,
264 };
265
Kirill A. Shutemove3884662016-03-17 14:20:07 -0700266 if (locked)
267 rmap_walk_locked(new, &rwc);
268 else
269 rmap_walk(new, &rwc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700270}
271
272/*
Christoph Lameter06972122006-06-23 02:03:35 -0700273 * Something used the pte of a page under migration. We need to
274 * get to the page and wait until migration is finished.
275 * When we return from this function the fault will be retried.
Christoph Lameter06972122006-06-23 02:03:35 -0700276 */
Naoya Horiguchie66f17f2015-02-11 15:25:22 -0800277void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700278 spinlock_t *ptl)
Christoph Lameter06972122006-06-23 02:03:35 -0700279{
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700280 pte_t pte;
Christoph Lameter06972122006-06-23 02:03:35 -0700281 swp_entry_t entry;
282 struct page *page;
283
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700284 spin_lock(ptl);
Christoph Lameter06972122006-06-23 02:03:35 -0700285 pte = *ptep;
286 if (!is_swap_pte(pte))
287 goto out;
288
289 entry = pte_to_swp_entry(pte);
290 if (!is_migration_entry(entry))
291 goto out;
292
293 page = migration_entry_to_page(entry);
294
Nick Piggine2867812008-07-25 19:45:30 -0700295 /*
296 * Once radix-tree replacement of page migration started, page_count
297 * *must* be zero. And, we don't want to call wait_on_page_locked()
298 * against a page without get_page().
299 * So, we use get_page_unless_zero(), here. Even failed, page fault
300 * will occur again.
301 */
302 if (!get_page_unless_zero(page))
303 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700304 pte_unmap_unlock(ptep, ptl);
305 wait_on_page_locked(page);
306 put_page(page);
307 return;
308out:
309 pte_unmap_unlock(ptep, ptl);
310}
311
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700312void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
313 unsigned long address)
314{
315 spinlock_t *ptl = pte_lockptr(mm, pmd);
316 pte_t *ptep = pte_offset_map(pmd, address);
317 __migration_entry_wait(mm, ptep, ptl);
318}
319
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -0800320void migration_entry_wait_huge(struct vm_area_struct *vma,
321 struct mm_struct *mm, pte_t *pte)
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700322{
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -0800323 spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte);
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700324 __migration_entry_wait(mm, pte, ptl);
325}
326
Mel Gormanb969c4a2012-01-12 17:19:34 -0800327#ifdef CONFIG_BLOCK
328/* Returns true if all buffers are successfully locked */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800329static bool buffer_migrate_lock_buffers(struct buffer_head *head,
330 enum migrate_mode mode)
Mel Gormanb969c4a2012-01-12 17:19:34 -0800331{
332 struct buffer_head *bh = head;
333
334 /* Simple case, sync compaction */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800335 if (mode != MIGRATE_ASYNC) {
Mel Gormanb969c4a2012-01-12 17:19:34 -0800336 do {
337 get_bh(bh);
338 lock_buffer(bh);
339 bh = bh->b_this_page;
340
341 } while (bh != head);
342
343 return true;
344 }
345
346 /* async case, we cannot block on lock_buffer so use trylock_buffer */
347 do {
348 get_bh(bh);
349 if (!trylock_buffer(bh)) {
350 /*
351 * We failed to lock the buffer and cannot stall in
352 * async migration. Release the taken locks
353 */
354 struct buffer_head *failed_bh = bh;
355 put_bh(failed_bh);
356 bh = head;
357 while (bh != failed_bh) {
358 unlock_buffer(bh);
359 put_bh(bh);
360 bh = bh->b_this_page;
361 }
362 return false;
363 }
364
365 bh = bh->b_this_page;
366 } while (bh != head);
367 return true;
368}
369#else
370static inline bool buffer_migrate_lock_buffers(struct buffer_head *head,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800371 enum migrate_mode mode)
Mel Gormanb969c4a2012-01-12 17:19:34 -0800372{
373 return true;
374}
375#endif /* CONFIG_BLOCK */
376
Christoph Lameterb20a3502006-03-22 00:09:12 -0800377/*
Christoph Lameterc3fcf8a2006-06-23 02:03:32 -0700378 * Replace the page in the mapping.
Christoph Lameter5b5c7122006-06-23 02:03:29 -0700379 *
380 * The number of remaining references must be:
381 * 1 for anonymous pages without a mapping
382 * 2 for pages with a mapping
David Howells266cf652009-04-03 16:42:36 +0100383 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800384 */
Gu Zheng36bc08c2013-07-16 17:56:16 +0800385int migrate_page_move_mapping(struct address_space *mapping,
Mel Gormanb969c4a2012-01-12 17:19:34 -0800386 struct page *newpage, struct page *page,
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500387 struct buffer_head *head, enum migrate_mode mode,
388 int extra_count)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800389{
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800390 struct zone *oldzone, *newzone;
391 int dirty;
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500392 int expected_count = 1 + extra_count;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800393 void **pslot;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800394
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700395 if (!mapping) {
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700396 /* Anonymous page without mapping */
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500397 if (page_count(page) != expected_count)
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700398 return -EAGAIN;
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800399
400 /* No turning back from here */
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800401 newpage->index = page->index;
402 newpage->mapping = page->mapping;
403 if (PageSwapBacked(page))
Hugh Dickinsfa9949d2016-05-19 17:12:41 -0700404 __SetPageSwapBacked(newpage);
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800405
Rafael Aquini78bd5202012-12-11 16:02:31 -0800406 return MIGRATEPAGE_SUCCESS;
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700407 }
408
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800409 oldzone = page_zone(page);
410 newzone = page_zone(newpage);
411
Nick Piggin19fd6232008-07-25 19:45:32 -0700412 spin_lock_irq(&mapping->tree_lock);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800413
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800414 pslot = radix_tree_lookup_slot(&mapping->page_tree,
415 page_index(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800416
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500417 expected_count += 1 + page_has_private(page);
Nick Piggine2867812008-07-25 19:45:30 -0700418 if (page_count(page) != expected_count ||
Mel Gorman29c1f672011-01-13 15:47:21 -0800419 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700420 spin_unlock_irq(&mapping->tree_lock);
Christoph Lametere23ca002006-04-10 22:52:57 -0700421 return -EAGAIN;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800422 }
423
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700424 if (!page_ref_freeze(page, expected_count)) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700425 spin_unlock_irq(&mapping->tree_lock);
Nick Piggine2867812008-07-25 19:45:30 -0700426 return -EAGAIN;
427 }
428
Christoph Lameterb20a3502006-03-22 00:09:12 -0800429 /*
Mel Gormanb969c4a2012-01-12 17:19:34 -0800430 * In the async migration case of moving a page with buffers, lock the
431 * buffers using trylock before the mapping is moved. If the mapping
432 * was moved, we later failed to lock the buffers and could not move
433 * the mapping back due to an elevated page count, we would have to
434 * block waiting on other references to be dropped.
435 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800436 if (mode == MIGRATE_ASYNC && head &&
437 !buffer_migrate_lock_buffers(head, mode)) {
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700438 page_ref_unfreeze(page, expected_count);
Mel Gormanb969c4a2012-01-12 17:19:34 -0800439 spin_unlock_irq(&mapping->tree_lock);
440 return -EAGAIN;
441 }
442
443 /*
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800444 * Now we know that no one else is looking at the page:
445 * no turning back from here.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800446 */
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800447 newpage->index = page->index;
448 newpage->mapping = page->mapping;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800449 get_page(newpage); /* add cache reference */
Nicholas Piggin6326fec2016-12-25 13:00:29 +1000450 if (PageSwapBacked(page)) {
451 __SetPageSwapBacked(newpage);
452 if (PageSwapCache(page)) {
453 SetPageSwapCache(newpage);
454 set_page_private(newpage, page_private(page));
455 }
456 } else {
457 VM_BUG_ON_PAGE(PageSwapCache(page), page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800458 }
459
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800460 /* Move dirty while page refs frozen and newpage not yet exposed */
461 dirty = PageDirty(page);
462 if (dirty) {
463 ClearPageDirty(page);
464 SetPageDirty(newpage);
465 }
466
Johannes Weiner6d75f362016-12-12 16:43:43 -0800467 radix_tree_replace_slot(&mapping->page_tree, pslot, newpage);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800468
469 /*
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800470 * Drop cache reference from old page by unfreezing
471 * to one less reference.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800472 * We know this isn't the last reference.
473 */
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700474 page_ref_unfreeze(page, expected_count - 1);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800475
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800476 spin_unlock(&mapping->tree_lock);
477 /* Leave irq disabled to prevent preemption while updating stats */
478
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700479 /*
480 * If moved to a different zone then also account
481 * the page for that zone. Other VM counters will be
482 * taken care of when we establish references to the
483 * new page and drop references to the old page.
484 *
485 * Note that anonymous pages are accounted for
Mel Gorman4b9d0fa2016-07-28 15:46:17 -0700486 * via NR_FILE_PAGES and NR_ANON_MAPPED if they
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700487 * are mapped to swap space.
488 */
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800489 if (newzone != oldzone) {
Mel Gorman11fb9982016-07-28 15:46:20 -0700490 __dec_node_state(oldzone->zone_pgdat, NR_FILE_PAGES);
491 __inc_node_state(newzone->zone_pgdat, NR_FILE_PAGES);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800492 if (PageSwapBacked(page) && !PageSwapCache(page)) {
Mel Gorman11fb9982016-07-28 15:46:20 -0700493 __dec_node_state(oldzone->zone_pgdat, NR_SHMEM);
494 __inc_node_state(newzone->zone_pgdat, NR_SHMEM);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800495 }
496 if (dirty && mapping_cap_account_dirty(mapping)) {
Mel Gorman11fb9982016-07-28 15:46:20 -0700497 __dec_node_state(oldzone->zone_pgdat, NR_FILE_DIRTY);
Mel Gorman5a1c84b2016-07-28 15:47:31 -0700498 __dec_zone_state(oldzone, NR_ZONE_WRITE_PENDING);
Mel Gorman11fb9982016-07-28 15:46:20 -0700499 __inc_node_state(newzone->zone_pgdat, NR_FILE_DIRTY);
Mel Gorman5a1c84b2016-07-28 15:47:31 -0700500 __inc_zone_state(newzone, NR_ZONE_WRITE_PENDING);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800501 }
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700502 }
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800503 local_irq_enable();
Christoph Lameterb20a3502006-03-22 00:09:12 -0800504
Rafael Aquini78bd5202012-12-11 16:02:31 -0800505 return MIGRATEPAGE_SUCCESS;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800506}
Richard Weinberger1118dce2016-06-16 23:26:14 +0200507EXPORT_SYMBOL(migrate_page_move_mapping);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800508
509/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900510 * The expected number of remaining references is the same as that
511 * of migrate_page_move_mapping().
512 */
513int migrate_huge_page_move_mapping(struct address_space *mapping,
514 struct page *newpage, struct page *page)
515{
516 int expected_count;
517 void **pslot;
518
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900519 spin_lock_irq(&mapping->tree_lock);
520
521 pslot = radix_tree_lookup_slot(&mapping->page_tree,
522 page_index(page));
523
524 expected_count = 2 + page_has_private(page);
525 if (page_count(page) != expected_count ||
Mel Gorman29c1f672011-01-13 15:47:21 -0800526 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900527 spin_unlock_irq(&mapping->tree_lock);
528 return -EAGAIN;
529 }
530
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700531 if (!page_ref_freeze(page, expected_count)) {
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900532 spin_unlock_irq(&mapping->tree_lock);
533 return -EAGAIN;
534 }
535
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800536 newpage->index = page->index;
537 newpage->mapping = page->mapping;
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700538
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900539 get_page(newpage);
540
Johannes Weiner6d75f362016-12-12 16:43:43 -0800541 radix_tree_replace_slot(&mapping->page_tree, pslot, newpage);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900542
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700543 page_ref_unfreeze(page, expected_count - 1);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900544
545 spin_unlock_irq(&mapping->tree_lock);
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700546
Rafael Aquini78bd5202012-12-11 16:02:31 -0800547 return MIGRATEPAGE_SUCCESS;
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900548}
549
550/*
Dave Hansen30b0a102013-11-21 14:31:58 -0800551 * Gigantic pages are so large that we do not guarantee that page++ pointer
552 * arithmetic will work across the entire page. We need something more
553 * specialized.
554 */
555static void __copy_gigantic_page(struct page *dst, struct page *src,
556 int nr_pages)
557{
558 int i;
559 struct page *dst_base = dst;
560 struct page *src_base = src;
561
562 for (i = 0; i < nr_pages; ) {
563 cond_resched();
564 copy_highpage(dst, src);
565
566 i++;
567 dst = mem_map_next(dst, dst_base, i);
568 src = mem_map_next(src, src_base, i);
569 }
570}
571
572static void copy_huge_page(struct page *dst, struct page *src)
573{
574 int i;
575 int nr_pages;
576
577 if (PageHuge(src)) {
578 /* hugetlbfs page */
579 struct hstate *h = page_hstate(src);
580 nr_pages = pages_per_huge_page(h);
581
582 if (unlikely(nr_pages > MAX_ORDER_NR_PAGES)) {
583 __copy_gigantic_page(dst, src, nr_pages);
584 return;
585 }
586 } else {
587 /* thp page */
588 BUG_ON(!PageTransHuge(src));
589 nr_pages = hpage_nr_pages(src);
590 }
591
592 for (i = 0; i < nr_pages; i++) {
593 cond_resched();
594 copy_highpage(dst + i, src + i);
595 }
596}
597
598/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800599 * Copy the page to its new location
600 */
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900601void migrate_page_copy(struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800602{
Rik van Riel7851a452013-10-07 11:29:23 +0100603 int cpupid;
604
Mel Gormanb32967f2012-11-19 12:35:47 +0000605 if (PageHuge(page) || PageTransHuge(page))
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900606 copy_huge_page(newpage, page);
607 else
608 copy_highpage(newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800609
610 if (PageError(page))
611 SetPageError(newpage);
612 if (PageReferenced(page))
613 SetPageReferenced(newpage);
614 if (PageUptodate(page))
615 SetPageUptodate(newpage);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700616 if (TestClearPageActive(page)) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800617 VM_BUG_ON_PAGE(PageUnevictable(page), page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800618 SetPageActive(newpage);
Lee Schermerhorn418b27e2009-12-14 17:59:54 -0800619 } else if (TestClearPageUnevictable(page))
620 SetPageUnevictable(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800621 if (PageChecked(page))
622 SetPageChecked(newpage);
623 if (PageMappedToDisk(page))
624 SetPageMappedToDisk(newpage);
625
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800626 /* Move dirty on pages not done by migrate_page_move_mapping() */
627 if (PageDirty(page))
628 SetPageDirty(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800629
Vladimir Davydov33c3fc72015-09-09 15:35:45 -0700630 if (page_is_young(page))
631 set_page_young(newpage);
632 if (page_is_idle(page))
633 set_page_idle(newpage);
634
Rik van Riel7851a452013-10-07 11:29:23 +0100635 /*
636 * Copy NUMA information to the new page, to prevent over-eager
637 * future migrations of this same page.
638 */
639 cpupid = page_cpupid_xchg_last(page, -1);
640 page_cpupid_xchg_last(newpage, cpupid);
641
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800642 ksm_migrate_page(newpage, page);
Hugh Dickinsc8d65532013-02-22 16:35:10 -0800643 /*
644 * Please do not reorder this without considering how mm/ksm.c's
645 * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache().
646 */
Naoya Horiguchib3b3a992015-04-15 16:13:15 -0700647 if (PageSwapCache(page))
648 ClearPageSwapCache(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800649 ClearPagePrivate(page);
650 set_page_private(page, 0);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800651
652 /*
653 * If any waiters have accumulated on the new page then
654 * wake them up.
655 */
656 if (PageWriteback(newpage))
657 end_page_writeback(newpage);
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700658
659 copy_page_owner(page, newpage);
Johannes Weiner74485cf2016-03-15 14:57:54 -0700660
661 mem_cgroup_migrate(page, newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800662}
Richard Weinberger1118dce2016-06-16 23:26:14 +0200663EXPORT_SYMBOL(migrate_page_copy);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800664
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700665/************************************************************
666 * Migration functions
667 ***********************************************************/
668
Christoph Lameterb20a3502006-03-22 00:09:12 -0800669/*
Minchan Kimbda807d2016-07-26 15:23:05 -0700670 * Common logic to directly migrate a single LRU page suitable for
David Howells266cf652009-04-03 16:42:36 +0100671 * pages that do not use PagePrivate/PagePrivate2.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800672 *
673 * Pages are locked upon entry and exit.
674 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700675int migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800676 struct page *newpage, struct page *page,
677 enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800678{
679 int rc;
680
681 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
682
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500683 rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode, 0);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800684
Rafael Aquini78bd5202012-12-11 16:02:31 -0800685 if (rc != MIGRATEPAGE_SUCCESS)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800686 return rc;
687
688 migrate_page_copy(newpage, page);
Rafael Aquini78bd5202012-12-11 16:02:31 -0800689 return MIGRATEPAGE_SUCCESS;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800690}
691EXPORT_SYMBOL(migrate_page);
692
David Howells93614012006-09-30 20:45:40 +0200693#ifdef CONFIG_BLOCK
Christoph Lameterb20a3502006-03-22 00:09:12 -0800694/*
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700695 * Migration function for pages with buffers. This function can only be used
696 * if the underlying filesystem guarantees that no other references to "page"
697 * exist.
698 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700699int buffer_migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800700 struct page *newpage, struct page *page, enum migrate_mode mode)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700701{
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700702 struct buffer_head *bh, *head;
703 int rc;
704
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700705 if (!page_has_buffers(page))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800706 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700707
708 head = page_buffers(page);
709
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500710 rc = migrate_page_move_mapping(mapping, newpage, page, head, mode, 0);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700711
Rafael Aquini78bd5202012-12-11 16:02:31 -0800712 if (rc != MIGRATEPAGE_SUCCESS)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700713 return rc;
714
Mel Gormanb969c4a2012-01-12 17:19:34 -0800715 /*
716 * In the async case, migrate_page_move_mapping locked the buffers
717 * with an IRQ-safe spinlock held. In the sync case, the buffers
718 * need to be locked now
719 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800720 if (mode != MIGRATE_ASYNC)
721 BUG_ON(!buffer_migrate_lock_buffers(head, mode));
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700722
723 ClearPagePrivate(page);
724 set_page_private(newpage, page_private(page));
725 set_page_private(page, 0);
726 put_page(page);
727 get_page(newpage);
728
729 bh = head;
730 do {
731 set_bh_page(bh, newpage, bh_offset(bh));
732 bh = bh->b_this_page;
733
734 } while (bh != head);
735
736 SetPagePrivate(newpage);
737
738 migrate_page_copy(newpage, page);
739
740 bh = head;
741 do {
742 unlock_buffer(bh);
743 put_bh(bh);
744 bh = bh->b_this_page;
745
746 } while (bh != head);
747
Rafael Aquini78bd5202012-12-11 16:02:31 -0800748 return MIGRATEPAGE_SUCCESS;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700749}
750EXPORT_SYMBOL(buffer_migrate_page);
David Howells93614012006-09-30 20:45:40 +0200751#endif
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700752
Christoph Lameter04e62a22006-06-23 02:03:38 -0700753/*
754 * Writeback a page to clean the dirty state
755 */
756static int writeout(struct address_space *mapping, struct page *page)
757{
758 struct writeback_control wbc = {
759 .sync_mode = WB_SYNC_NONE,
760 .nr_to_write = 1,
761 .range_start = 0,
762 .range_end = LLONG_MAX,
Christoph Lameter04e62a22006-06-23 02:03:38 -0700763 .for_reclaim = 1
764 };
765 int rc;
766
767 if (!mapping->a_ops->writepage)
768 /* No write method for the address space */
769 return -EINVAL;
770
771 if (!clear_page_dirty_for_io(page))
772 /* Someone else already triggered a write */
773 return -EAGAIN;
774
775 /*
776 * A dirty page may imply that the underlying filesystem has
777 * the page on some queue. So the page must be clean for
778 * migration. Writeout may mean we loose the lock and the
779 * page state is no longer what we checked for earlier.
780 * At this point we know that the migration attempt cannot
781 * be successful.
782 */
Kirill A. Shutemove3884662016-03-17 14:20:07 -0700783 remove_migration_ptes(page, page, false);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700784
785 rc = mapping->a_ops->writepage(page, &wbc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700786
787 if (rc != AOP_WRITEPAGE_ACTIVATE)
788 /* unlocked. Relock */
789 lock_page(page);
790
Hugh Dickinsbda85502008-11-19 15:36:36 -0800791 return (rc < 0) ? -EIO : -EAGAIN;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700792}
793
794/*
795 * Default handling if a filesystem does not provide a migration function.
796 */
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700797static int fallback_migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800798 struct page *newpage, struct page *page, enum migrate_mode mode)
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700799{
Mel Gormanb969c4a2012-01-12 17:19:34 -0800800 if (PageDirty(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800801 /* Only writeback pages in full synchronous migration */
802 if (mode != MIGRATE_SYNC)
Mel Gormanb969c4a2012-01-12 17:19:34 -0800803 return -EBUSY;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700804 return writeout(mapping, page);
Mel Gormanb969c4a2012-01-12 17:19:34 -0800805 }
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700806
807 /*
808 * Buffers may be managed in a filesystem specific way.
809 * We must have no buffers or drop them.
810 */
David Howells266cf652009-04-03 16:42:36 +0100811 if (page_has_private(page) &&
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700812 !try_to_release_page(page, GFP_KERNEL))
813 return -EAGAIN;
814
Mel Gormana6bc32b2012-01-12 17:19:43 -0800815 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700816}
817
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700818/*
Christoph Lametere24f0b82006-06-23 02:03:51 -0700819 * Move a page to a newly allocated page
820 * The page is locked and all ptes have been successfully removed.
821 *
822 * The new page will have replaced the old page if this function
823 * is successful.
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700824 *
825 * Return value:
826 * < 0 - error code
Rafael Aquini78bd5202012-12-11 16:02:31 -0800827 * MIGRATEPAGE_SUCCESS - success
Christoph Lametere24f0b82006-06-23 02:03:51 -0700828 */
Mel Gorman3fe20112010-05-24 14:32:20 -0700829static int move_to_new_page(struct page *newpage, struct page *page,
Hugh Dickins5c3f9a62015-11-05 18:49:53 -0800830 enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700831{
832 struct address_space *mapping;
Minchan Kimbda807d2016-07-26 15:23:05 -0700833 int rc = -EAGAIN;
834 bool is_lru = !__PageMovable(page);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700835
Hugh Dickins7db76712015-11-05 18:49:49 -0800836 VM_BUG_ON_PAGE(!PageLocked(page), page);
837 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700838
Christoph Lametere24f0b82006-06-23 02:03:51 -0700839 mapping = page_mapping(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700840
841 if (likely(is_lru)) {
842 if (!mapping)
843 rc = migrate_page(mapping, newpage, page, mode);
844 else if (mapping->a_ops->migratepage)
845 /*
846 * Most pages have a mapping and most filesystems
847 * provide a migratepage callback. Anonymous pages
848 * are part of swap space which also has its own
849 * migratepage callback. This is the most common path
850 * for page migration.
851 */
852 rc = mapping->a_ops->migratepage(mapping, newpage,
853 page, mode);
854 else
855 rc = fallback_migrate_page(mapping, newpage,
856 page, mode);
857 } else {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700858 /*
Minchan Kimbda807d2016-07-26 15:23:05 -0700859 * In case of non-lru page, it could be released after
860 * isolation step. In that case, we shouldn't try migration.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700861 */
Minchan Kimbda807d2016-07-26 15:23:05 -0700862 VM_BUG_ON_PAGE(!PageIsolated(page), page);
863 if (!PageMovable(page)) {
864 rc = MIGRATEPAGE_SUCCESS;
865 __ClearPageIsolated(page);
866 goto out;
867 }
868
869 rc = mapping->a_ops->migratepage(mapping, newpage,
870 page, mode);
871 WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS &&
872 !PageIsolated(page));
873 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700874
Hugh Dickins5c3f9a62015-11-05 18:49:53 -0800875 /*
876 * When successful, old pagecache page->mapping must be cleared before
877 * page is freed; but stats require that PageAnon be left as PageAnon.
878 */
879 if (rc == MIGRATEPAGE_SUCCESS) {
Minchan Kimbda807d2016-07-26 15:23:05 -0700880 if (__PageMovable(page)) {
881 VM_BUG_ON_PAGE(!PageIsolated(page), page);
882
883 /*
884 * We clear PG_movable under page_lock so any compactor
885 * cannot try to migrate this page.
886 */
887 __ClearPageIsolated(page);
888 }
889
890 /*
891 * Anonymous and movable page->mapping will be cleard by
892 * free_pages_prepare so don't reset it here for keeping
893 * the type to work PageAnon, for example.
894 */
895 if (!PageMappingFlags(page))
Hugh Dickins5c3f9a62015-11-05 18:49:53 -0800896 page->mapping = NULL;
Mel Gorman3fe20112010-05-24 14:32:20 -0700897 }
Minchan Kimbda807d2016-07-26 15:23:05 -0700898out:
Christoph Lametere24f0b82006-06-23 02:03:51 -0700899 return rc;
900}
901
Minchan Kim0dabec92011-10-31 17:06:57 -0700902static int __unmap_and_move(struct page *page, struct page *newpage,
Hugh Dickins9c620e22013-02-22 16:35:14 -0800903 int force, enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700904{
Minchan Kim0dabec92011-10-31 17:06:57 -0700905 int rc = -EAGAIN;
Hugh Dickins2ebba6b2014-12-12 16:56:19 -0800906 int page_was_mapped = 0;
Mel Gorman3f6c8272010-05-24 14:32:17 -0700907 struct anon_vma *anon_vma = NULL;
Minchan Kimbda807d2016-07-26 15:23:05 -0700908 bool is_lru = !__PageMovable(page);
Christoph Lameter95a402c2006-06-23 02:03:53 -0700909
Nick Piggin529ae9a2008-08-02 12:01:03 +0200910 if (!trylock_page(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800911 if (!force || mode == MIGRATE_ASYNC)
Minchan Kim0dabec92011-10-31 17:06:57 -0700912 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800913
914 /*
915 * It's not safe for direct compaction to call lock_page.
916 * For example, during page readahead pages are added locked
917 * to the LRU. Later, when the IO completes the pages are
918 * marked uptodate and unlocked. However, the queueing
919 * could be merging multiple pages for one bio (e.g.
920 * mpage_readpages). If an allocation happens for the
921 * second or third page, the process can end up locking
922 * the same page twice and deadlocking. Rather than
923 * trying to be clever about what pages can be locked,
924 * avoid the use of lock_page for direct compaction
925 * altogether.
926 */
927 if (current->flags & PF_MEMALLOC)
Minchan Kim0dabec92011-10-31 17:06:57 -0700928 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800929
Christoph Lametere24f0b82006-06-23 02:03:51 -0700930 lock_page(page);
931 }
932
933 if (PageWriteback(page)) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700934 /*
Jianguo Wufed5b642013-04-29 15:07:58 -0700935 * Only in the case of a full synchronous migration is it
Mel Gormana6bc32b2012-01-12 17:19:43 -0800936 * necessary to wait for PageWriteback. In the async case,
937 * the retry loop is too short and in the sync-light case,
938 * the overhead of stalling is too much
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700939 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800940 if (mode != MIGRATE_SYNC) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700941 rc = -EBUSY;
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700942 goto out_unlock;
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700943 }
944 if (!force)
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700945 goto out_unlock;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700946 wait_on_page_writeback(page);
947 }
Hugh Dickins03f15c82015-11-05 18:49:56 -0800948
Christoph Lametere24f0b82006-06-23 02:03:51 -0700949 /*
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700950 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
951 * we cannot notice that anon_vma is freed while we migrates a page.
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800952 * This get_anon_vma() delays freeing anon_vma pointer until the end
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700953 * of migration. File cache pages are no problem because of page_lock()
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700954 * File Caches may use write_page() or lock_page() in migration, then,
955 * just care Anon page here.
Hugh Dickins03f15c82015-11-05 18:49:56 -0800956 *
957 * Only page_get_anon_vma() understands the subtleties of
958 * getting a hold on an anon_vma from outside one of its mms.
959 * But if we cannot get anon_vma, then we won't need it anyway,
960 * because that implies that the anon page is no longer mapped
961 * (and cannot be remapped so long as we hold the page lock).
Christoph Lametere24f0b82006-06-23 02:03:51 -0700962 */
Hugh Dickins03f15c82015-11-05 18:49:56 -0800963 if (PageAnon(page) && !PageKsm(page))
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700964 anon_vma = page_get_anon_vma(page);
Shaohua Li62e1c552008-02-04 22:29:33 -0800965
Hugh Dickins7db76712015-11-05 18:49:49 -0800966 /*
967 * Block others from accessing the new page when we get around to
968 * establishing additional references. We are usually the only one
969 * holding a reference to newpage at this point. We used to have a BUG
970 * here if trylock_page(newpage) fails, but would like to allow for
971 * cases where there might be a race with the previous use of newpage.
972 * This is much like races on refcount of oldpage: just don't BUG().
973 */
974 if (unlikely(!trylock_page(newpage)))
975 goto out_unlock;
976
Minchan Kimbda807d2016-07-26 15:23:05 -0700977 if (unlikely(!is_lru)) {
978 rc = move_to_new_page(newpage, page, mode);
979 goto out_unlock_both;
980 }
981
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700982 /*
Shaohua Li62e1c552008-02-04 22:29:33 -0800983 * Corner case handling:
984 * 1. When a new swap-cache page is read into, it is added to the LRU
985 * and treated as swapcache but it has no rmap yet.
986 * Calling try_to_unmap() against a page->mapping==NULL page will
987 * trigger a BUG. So handle it here.
988 * 2. An orphaned page (see truncate_complete_page) might have
989 * fs-private metadata. The page can be picked up due to memory
990 * offlining. Everywhere else except page reclaim, the page is
991 * invisible to the vm, so the page can not be migrated. So try to
992 * free the metadata, so the page can be freed.
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700993 */
Shaohua Li62e1c552008-02-04 22:29:33 -0800994 if (!page->mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800995 VM_BUG_ON_PAGE(PageAnon(page), page);
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800996 if (page_has_private(page)) {
Shaohua Li62e1c552008-02-04 22:29:33 -0800997 try_to_free_buffers(page);
Hugh Dickins7db76712015-11-05 18:49:49 -0800998 goto out_unlock_both;
Shaohua Li62e1c552008-02-04 22:29:33 -0800999 }
Hugh Dickins7db76712015-11-05 18:49:49 -08001000 } else if (page_mapped(page)) {
1001 /* Establish migration ptes */
Hugh Dickins03f15c82015-11-05 18:49:56 -08001002 VM_BUG_ON_PAGE(PageAnon(page) && !PageKsm(page) && !anon_vma,
1003 page);
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001004 try_to_unmap(page,
Wanpeng Lida1b13c2015-09-08 15:03:27 -07001005 TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001006 page_was_mapped = 1;
1007 }
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001008
Christoph Lametere6a15302006-06-25 05:46:49 -07001009 if (!page_mapped(page))
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001010 rc = move_to_new_page(newpage, page, mode);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001011
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001012 if (page_was_mapped)
1013 remove_migration_ptes(page,
Kirill A. Shutemove3884662016-03-17 14:20:07 -07001014 rc == MIGRATEPAGE_SUCCESS ? newpage : page, false);
Mel Gorman3f6c8272010-05-24 14:32:17 -07001015
Hugh Dickins7db76712015-11-05 18:49:49 -08001016out_unlock_both:
1017 unlock_page(newpage);
1018out_unlock:
Mel Gorman3f6c8272010-05-24 14:32:17 -07001019 /* Drop an anon_vma reference if we took one */
Rik van Riel76545062010-08-09 17:18:41 -07001020 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -07001021 put_anon_vma(anon_vma);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001022 unlock_page(page);
Minchan Kim0dabec92011-10-31 17:06:57 -07001023out:
Minchan Kimc6c919e2016-07-26 15:23:02 -07001024 /*
1025 * If migration is successful, decrease refcount of the newpage
1026 * which will not free the page because new page owner increased
1027 * refcounter. As well, if it is LRU page, add the page to LRU
1028 * list in here.
1029 */
1030 if (rc == MIGRATEPAGE_SUCCESS) {
Minchan Kimb1123ea62016-07-26 15:23:09 -07001031 if (unlikely(__PageMovable(newpage)))
Minchan Kimc6c919e2016-07-26 15:23:02 -07001032 put_page(newpage);
1033 else
1034 putback_lru_page(newpage);
1035 }
1036
Minchan Kim0dabec92011-10-31 17:06:57 -07001037 return rc;
1038}
Christoph Lameter95a402c2006-06-23 02:03:53 -07001039
Minchan Kim0dabec92011-10-31 17:06:57 -07001040/*
Geert Uytterhoevenef2a5152015-04-14 15:44:22 -07001041 * gcc 4.7 and 4.8 on arm get an ICEs when inlining unmap_and_move(). Work
1042 * around it.
1043 */
1044#if (GCC_VERSION >= 40700 && GCC_VERSION < 40900) && defined(CONFIG_ARM)
1045#define ICE_noinline noinline
1046#else
1047#define ICE_noinline
1048#endif
1049
1050/*
Minchan Kim0dabec92011-10-31 17:06:57 -07001051 * Obtain the lock on page, remove all ptes and migrate the page
1052 * to the newly allocated page in newpage.
1053 */
Geert Uytterhoevenef2a5152015-04-14 15:44:22 -07001054static ICE_noinline int unmap_and_move(new_page_t get_new_page,
1055 free_page_t put_new_page,
1056 unsigned long private, struct page *page,
Naoya Horiguchiadd05ce2015-06-24 16:56:50 -07001057 int force, enum migrate_mode mode,
1058 enum migrate_reason reason)
Minchan Kim0dabec92011-10-31 17:06:57 -07001059{
Hugh Dickins2def7422015-11-05 18:49:46 -08001060 int rc = MIGRATEPAGE_SUCCESS;
Minchan Kim0dabec92011-10-31 17:06:57 -07001061 int *result = NULL;
Hugh Dickins2def7422015-11-05 18:49:46 -08001062 struct page *newpage;
Minchan Kim0dabec92011-10-31 17:06:57 -07001063
Hugh Dickins2def7422015-11-05 18:49:46 -08001064 newpage = get_new_page(page, private, &result);
Minchan Kim0dabec92011-10-31 17:06:57 -07001065 if (!newpage)
1066 return -ENOMEM;
1067
1068 if (page_count(page) == 1) {
1069 /* page was freed from under us. So we are done. */
Minchan Kimc6c919e2016-07-26 15:23:02 -07001070 ClearPageActive(page);
1071 ClearPageUnevictable(page);
Minchan Kimbda807d2016-07-26 15:23:05 -07001072 if (unlikely(__PageMovable(page))) {
1073 lock_page(page);
1074 if (!PageMovable(page))
1075 __ClearPageIsolated(page);
1076 unlock_page(page);
1077 }
Minchan Kimc6c919e2016-07-26 15:23:02 -07001078 if (put_new_page)
1079 put_new_page(newpage, private);
1080 else
1081 put_page(newpage);
Minchan Kim0dabec92011-10-31 17:06:57 -07001082 goto out;
1083 }
1084
Kirill A. Shutemov4d2fa962016-01-15 16:54:00 -08001085 if (unlikely(PageTransHuge(page))) {
1086 lock_page(page);
1087 rc = split_huge_page(page);
1088 unlock_page(page);
1089 if (rc)
Minchan Kim0dabec92011-10-31 17:06:57 -07001090 goto out;
Kirill A. Shutemov4d2fa962016-01-15 16:54:00 -08001091 }
Minchan Kim0dabec92011-10-31 17:06:57 -07001092
Hugh Dickins9c620e22013-02-22 16:35:14 -08001093 rc = __unmap_and_move(page, newpage, force, mode);
Minchan Kimc6c919e2016-07-26 15:23:02 -07001094 if (rc == MIGRATEPAGE_SUCCESS)
Vlastimil Babka7cd12b42016-03-15 14:56:18 -07001095 set_page_owner_migrate_reason(newpage, reason);
Rafael Aquinibf6bddf12012-12-11 16:02:42 -08001096
Minchan Kim0dabec92011-10-31 17:06:57 -07001097out:
Christoph Lametere24f0b82006-06-23 02:03:51 -07001098 if (rc != -EAGAIN) {
Minchan Kim0dabec92011-10-31 17:06:57 -07001099 /*
1100 * A page that has been migrated has all references
1101 * removed and will be freed. A page that has not been
1102 * migrated will have kepts its references and be
1103 * restored.
1104 */
1105 list_del(&page->lru);
Ming Ling6afcf8e2016-12-12 16:42:26 -08001106
1107 /*
1108 * Compaction can migrate also non-LRU pages which are
1109 * not accounted to NR_ISOLATED_*. They can be recognized
1110 * as __PageMovable
1111 */
1112 if (likely(!__PageMovable(page)))
1113 dec_node_page_state(page, NR_ISOLATED_ANON +
1114 page_is_file_cache(page));
Christoph Lametere24f0b82006-06-23 02:03:51 -07001115 }
David Rientjes68711a72014-06-04 16:08:25 -07001116
Christoph Lameter95a402c2006-06-23 02:03:53 -07001117 /*
Minchan Kimc6c919e2016-07-26 15:23:02 -07001118 * If migration is successful, releases reference grabbed during
1119 * isolation. Otherwise, restore the page to right list unless
1120 * we want to retry.
Christoph Lameter95a402c2006-06-23 02:03:53 -07001121 */
Minchan Kimc6c919e2016-07-26 15:23:02 -07001122 if (rc == MIGRATEPAGE_SUCCESS) {
1123 put_page(page);
1124 if (reason == MR_MEMORY_FAILURE) {
1125 /*
1126 * Set PG_HWPoison on just freed page
1127 * intentionally. Although it's rather weird,
1128 * it's how HWPoison flag works at the moment.
1129 */
1130 if (!test_set_page_hwpoison(page))
1131 num_poisoned_pages_inc();
1132 }
1133 } else {
Minchan Kimbda807d2016-07-26 15:23:05 -07001134 if (rc != -EAGAIN) {
1135 if (likely(!__PageMovable(page))) {
1136 putback_lru_page(page);
1137 goto put_new;
1138 }
1139
1140 lock_page(page);
1141 if (PageMovable(page))
1142 putback_movable_page(page);
1143 else
1144 __ClearPageIsolated(page);
1145 unlock_page(page);
1146 put_page(page);
1147 }
1148put_new:
Minchan Kimc6c919e2016-07-26 15:23:02 -07001149 if (put_new_page)
1150 put_new_page(newpage, private);
1151 else
1152 put_page(newpage);
1153 }
David Rientjes68711a72014-06-04 16:08:25 -07001154
Christoph Lameter742755a2006-06-23 02:03:55 -07001155 if (result) {
1156 if (rc)
1157 *result = rc;
1158 else
1159 *result = page_to_nid(newpage);
1160 }
Christoph Lametere24f0b82006-06-23 02:03:51 -07001161 return rc;
1162}
1163
1164/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001165 * Counterpart of unmap_and_move_page() for hugepage migration.
1166 *
1167 * This function doesn't wait the completion of hugepage I/O
1168 * because there is no race between I/O and migration for hugepage.
1169 * Note that currently hugepage I/O occurs only in direct I/O
1170 * where no lock is held and PG_writeback is irrelevant,
1171 * and writeback status of all subpages are counted in the reference
1172 * count of the head page (i.e. if all subpages of a 2MB hugepage are
1173 * under direct I/O, the reference of the head page is 512 and a bit more.)
1174 * This means that when we try to migrate hugepage whose subpages are
1175 * doing direct I/O, some references remain after try_to_unmap() and
1176 * hugepage migration fails without data corruption.
1177 *
1178 * There is also no race when direct I/O is issued on the page under migration,
1179 * because then pte is replaced with migration swap entry and direct I/O code
1180 * will wait in the page fault for migration to complete.
1181 */
1182static int unmap_and_move_huge_page(new_page_t get_new_page,
David Rientjes68711a72014-06-04 16:08:25 -07001183 free_page_t put_new_page, unsigned long private,
1184 struct page *hpage, int force,
Vlastimil Babka7cd12b42016-03-15 14:56:18 -07001185 enum migrate_mode mode, int reason)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001186{
Hugh Dickins2def7422015-11-05 18:49:46 -08001187 int rc = -EAGAIN;
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001188 int *result = NULL;
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001189 int page_was_mapped = 0;
Joonsoo Kim32665f22014-01-21 15:51:15 -08001190 struct page *new_hpage;
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001191 struct anon_vma *anon_vma = NULL;
1192
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001193 /*
1194 * Movability of hugepages depends on architectures and hugepage size.
1195 * This check is necessary because some callers of hugepage migration
1196 * like soft offline and memory hotremove don't walk through page
1197 * tables or check whether the hugepage is pmd-based or not before
1198 * kicking migration.
1199 */
Naoya Horiguchi100873d2014-06-04 16:10:56 -07001200 if (!hugepage_migration_supported(page_hstate(hpage))) {
Joonsoo Kim32665f22014-01-21 15:51:15 -08001201 putback_active_hugepage(hpage);
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001202 return -ENOSYS;
Joonsoo Kim32665f22014-01-21 15:51:15 -08001203 }
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001204
Joonsoo Kim32665f22014-01-21 15:51:15 -08001205 new_hpage = get_new_page(hpage, private, &result);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001206 if (!new_hpage)
1207 return -ENOMEM;
1208
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001209 if (!trylock_page(hpage)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -08001210 if (!force || mode != MIGRATE_SYNC)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001211 goto out;
1212 lock_page(hpage);
1213 }
1214
Peter Zijlstra746b18d2011-05-24 17:12:10 -07001215 if (PageAnon(hpage))
1216 anon_vma = page_get_anon_vma(hpage);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001217
Hugh Dickins7db76712015-11-05 18:49:49 -08001218 if (unlikely(!trylock_page(new_hpage)))
1219 goto put_anon;
1220
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001221 if (page_mapped(hpage)) {
1222 try_to_unmap(hpage,
1223 TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
1224 page_was_mapped = 1;
1225 }
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001226
1227 if (!page_mapped(hpage))
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001228 rc = move_to_new_page(new_hpage, hpage, mode);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001229
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001230 if (page_was_mapped)
1231 remove_migration_ptes(hpage,
Kirill A. Shutemove3884662016-03-17 14:20:07 -07001232 rc == MIGRATEPAGE_SUCCESS ? new_hpage : hpage, false);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001233
Hugh Dickins7db76712015-11-05 18:49:49 -08001234 unlock_page(new_hpage);
1235
1236put_anon:
Hugh Dickinsfd4a4662011-01-13 15:47:31 -08001237 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -07001238 put_anon_vma(anon_vma);
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -07001239
Hugh Dickins2def7422015-11-05 18:49:46 -08001240 if (rc == MIGRATEPAGE_SUCCESS) {
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -07001241 hugetlb_cgroup_migrate(hpage, new_hpage);
Hugh Dickins2def7422015-11-05 18:49:46 -08001242 put_new_page = NULL;
Vlastimil Babka7cd12b42016-03-15 14:56:18 -07001243 set_page_owner_migrate_reason(new_hpage, reason);
Hugh Dickins2def7422015-11-05 18:49:46 -08001244 }
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -07001245
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001246 unlock_page(hpage);
Hillf Danton09761332011-12-08 14:34:20 -08001247out:
Naoya Horiguchib8ec1ce2013-09-11 14:22:01 -07001248 if (rc != -EAGAIN)
1249 putback_active_hugepage(hpage);
David Rientjes68711a72014-06-04 16:08:25 -07001250
1251 /*
1252 * If migration was not successful and there's a freeing callback, use
1253 * it. Otherwise, put_page() will drop the reference grabbed during
1254 * isolation.
1255 */
Hugh Dickins2def7422015-11-05 18:49:46 -08001256 if (put_new_page)
David Rientjes68711a72014-06-04 16:08:25 -07001257 put_new_page(new_hpage, private);
1258 else
Naoya Horiguchi3aaa76e2015-09-22 14:59:14 -07001259 putback_active_hugepage(new_hpage);
David Rientjes68711a72014-06-04 16:08:25 -07001260
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001261 if (result) {
1262 if (rc)
1263 *result = rc;
1264 else
1265 *result = page_to_nid(new_hpage);
1266 }
1267 return rc;
1268}
1269
1270/*
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001271 * migrate_pages - migrate the pages specified in a list, to the free pages
1272 * supplied as the target for the page migration
Christoph Lameterb20a3502006-03-22 00:09:12 -08001273 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001274 * @from: The list of pages to be migrated.
1275 * @get_new_page: The function used to allocate free pages to be used
1276 * as the target of the page migration.
David Rientjes68711a72014-06-04 16:08:25 -07001277 * @put_new_page: The function used to free target pages if migration
1278 * fails, or NULL if no special handling is necessary.
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001279 * @private: Private data to be passed on to get_new_page()
1280 * @mode: The migration mode that specifies the constraints for
1281 * page migration, if any.
1282 * @reason: The reason for page migration.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001283 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001284 * The function returns after 10 attempts or if no pages are movable any more
1285 * because the list has become empty or no retryable pages exist any more.
Hugh Dickins14e0f9b2015-11-05 18:49:43 -08001286 * The caller should call putback_movable_pages() to return pages to the LRU
Minchan Kim28bd6572011-01-25 15:07:26 -08001287 * or free list only if ret != 0.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001288 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001289 * Returns the number of pages that were not migrated, or an error code.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001290 */
Hugh Dickins9c620e22013-02-22 16:35:14 -08001291int migrate_pages(struct list_head *from, new_page_t get_new_page,
David Rientjes68711a72014-06-04 16:08:25 -07001292 free_page_t put_new_page, unsigned long private,
1293 enum migrate_mode mode, int reason)
Christoph Lameterb20a3502006-03-22 00:09:12 -08001294{
Christoph Lametere24f0b82006-06-23 02:03:51 -07001295 int retry = 1;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001296 int nr_failed = 0;
Mel Gorman5647bc22012-10-19 10:46:20 +01001297 int nr_succeeded = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001298 int pass = 0;
1299 struct page *page;
1300 struct page *page2;
1301 int swapwrite = current->flags & PF_SWAPWRITE;
1302 int rc;
1303
1304 if (!swapwrite)
1305 current->flags |= PF_SWAPWRITE;
1306
Christoph Lametere24f0b82006-06-23 02:03:51 -07001307 for(pass = 0; pass < 10 && retry; pass++) {
1308 retry = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001309
Christoph Lametere24f0b82006-06-23 02:03:51 -07001310 list_for_each_entry_safe(page, page2, from, lru) {
Christoph Lametere24f0b82006-06-23 02:03:51 -07001311 cond_resched();
Christoph Lameterb20a3502006-03-22 00:09:12 -08001312
Naoya Horiguchi31caf662013-09-11 14:21:59 -07001313 if (PageHuge(page))
1314 rc = unmap_and_move_huge_page(get_new_page,
David Rientjes68711a72014-06-04 16:08:25 -07001315 put_new_page, private, page,
Vlastimil Babka7cd12b42016-03-15 14:56:18 -07001316 pass > 2, mode, reason);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07001317 else
David Rientjes68711a72014-06-04 16:08:25 -07001318 rc = unmap_and_move(get_new_page, put_new_page,
Naoya Horiguchiadd05ce2015-06-24 16:56:50 -07001319 private, page, pass > 2, mode,
1320 reason);
Christoph Lameterb20a3502006-03-22 00:09:12 -08001321
Christoph Lametere24f0b82006-06-23 02:03:51 -07001322 switch(rc) {
Christoph Lameter95a402c2006-06-23 02:03:53 -07001323 case -ENOMEM:
David Rientjesdfef2ef2016-05-20 16:59:05 -07001324 nr_failed++;
Christoph Lameter95a402c2006-06-23 02:03:53 -07001325 goto out;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001326 case -EAGAIN:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001327 retry++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001328 break;
Rafael Aquini78bd5202012-12-11 16:02:31 -08001329 case MIGRATEPAGE_SUCCESS:
Mel Gorman5647bc22012-10-19 10:46:20 +01001330 nr_succeeded++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001331 break;
1332 default:
Naoya Horiguchi354a3362014-01-21 15:51:14 -08001333 /*
1334 * Permanent failure (-EBUSY, -ENOSYS, etc.):
1335 * unlike -EAGAIN case, the failed page is
1336 * removed from migration page list and not
1337 * retried in the next outer loop.
1338 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001339 nr_failed++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001340 break;
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001341 }
Christoph Lameterb20a3502006-03-22 00:09:12 -08001342 }
1343 }
Vlastimil Babkaf2f81fb2015-11-05 18:47:03 -08001344 nr_failed += retry;
1345 rc = nr_failed;
Christoph Lameter95a402c2006-06-23 02:03:53 -07001346out:
Mel Gorman5647bc22012-10-19 10:46:20 +01001347 if (nr_succeeded)
1348 count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
1349 if (nr_failed)
1350 count_vm_events(PGMIGRATE_FAIL, nr_failed);
Mel Gorman7b2a2d42012-10-19 14:07:31 +01001351 trace_mm_migrate_pages(nr_succeeded, nr_failed, mode, reason);
1352
Christoph Lameterb20a3502006-03-22 00:09:12 -08001353 if (!swapwrite)
1354 current->flags &= ~PF_SWAPWRITE;
1355
Rafael Aquini78bd5202012-12-11 16:02:31 -08001356 return rc;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001357}
1358
Christoph Lameter742755a2006-06-23 02:03:55 -07001359#ifdef CONFIG_NUMA
1360/*
1361 * Move a list of individual pages
1362 */
1363struct page_to_node {
1364 unsigned long addr;
1365 struct page *page;
1366 int node;
1367 int status;
1368};
1369
1370static struct page *new_page_node(struct page *p, unsigned long private,
1371 int **result)
1372{
1373 struct page_to_node *pm = (struct page_to_node *)private;
1374
1375 while (pm->node != MAX_NUMNODES && pm->page != p)
1376 pm++;
1377
1378 if (pm->node == MAX_NUMNODES)
1379 return NULL;
1380
1381 *result = &pm->status;
1382
Naoya Horiguchie632a932013-09-11 14:22:04 -07001383 if (PageHuge(p))
1384 return alloc_huge_page_node(page_hstate(compound_head(p)),
1385 pm->node);
1386 else
Vlastimil Babka96db8002015-09-08 15:03:50 -07001387 return __alloc_pages_node(pm->node,
Johannes Weinere97ca8e52014-03-10 15:49:43 -07001388 GFP_HIGHUSER_MOVABLE | __GFP_THISNODE, 0);
Christoph Lameter742755a2006-06-23 02:03:55 -07001389}
1390
1391/*
1392 * Move a set of pages as indicated in the pm array. The addr
1393 * field must be set to the virtual address of the page to be moved
1394 * and the node number must contain a valid target node.
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001395 * The pm array ends with node = MAX_NUMNODES.
Christoph Lameter742755a2006-06-23 02:03:55 -07001396 */
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001397static int do_move_page_to_node_array(struct mm_struct *mm,
1398 struct page_to_node *pm,
1399 int migrate_all)
Christoph Lameter742755a2006-06-23 02:03:55 -07001400{
1401 int err;
1402 struct page_to_node *pp;
1403 LIST_HEAD(pagelist);
1404
1405 down_read(&mm->mmap_sem);
1406
1407 /*
1408 * Build a list of pages to migrate
1409 */
Christoph Lameter742755a2006-06-23 02:03:55 -07001410 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
1411 struct vm_area_struct *vma;
1412 struct page *page;
1413
Christoph Lameter742755a2006-06-23 02:03:55 -07001414 err = -EFAULT;
1415 vma = find_vma(mm, pp->addr);
Gleb Natapov70384dc2010-10-26 14:22:07 -07001416 if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
Christoph Lameter742755a2006-06-23 02:03:55 -07001417 goto set_status;
1418
Kirill A. Shutemovd8998442015-09-04 15:47:53 -07001419 /* FOLL_DUMP to ignore special (like zero) pages */
1420 page = follow_page(vma, pp->addr,
1421 FOLL_GET | FOLL_SPLIT | FOLL_DUMP);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001422
1423 err = PTR_ERR(page);
1424 if (IS_ERR(page))
1425 goto set_status;
1426
Christoph Lameter742755a2006-06-23 02:03:55 -07001427 err = -ENOENT;
1428 if (!page)
1429 goto set_status;
1430
Christoph Lameter742755a2006-06-23 02:03:55 -07001431 pp->page = page;
1432 err = page_to_nid(page);
1433
1434 if (err == pp->node)
1435 /*
1436 * Node already in the right place
1437 */
1438 goto put_and_set;
1439
1440 err = -EACCES;
1441 if (page_mapcount(page) > 1 &&
1442 !migrate_all)
1443 goto put_and_set;
1444
Naoya Horiguchie632a932013-09-11 14:22:04 -07001445 if (PageHuge(page)) {
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08001446 if (PageHead(page))
1447 isolate_huge_page(page, &pagelist);
Naoya Horiguchie632a932013-09-11 14:22:04 -07001448 goto put_and_set;
1449 }
1450
Nick Piggin62695a82008-10-18 20:26:09 -07001451 err = isolate_lru_page(page);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001452 if (!err) {
Nick Piggin62695a82008-10-18 20:26:09 -07001453 list_add_tail(&page->lru, &pagelist);
Mel Gorman599d0c92016-07-28 15:45:31 -07001454 inc_node_page_state(page, NR_ISOLATED_ANON +
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001455 page_is_file_cache(page));
1456 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001457put_and_set:
1458 /*
1459 * Either remove the duplicate refcount from
1460 * isolate_lru_page() or drop the page ref if it was
1461 * not isolated.
1462 */
1463 put_page(page);
1464set_status:
1465 pp->status = err;
1466 }
1467
Brice Gogline78bbfa2008-10-18 20:27:15 -07001468 err = 0;
Minchan Kimcf608ac2010-10-26 14:21:29 -07001469 if (!list_empty(&pagelist)) {
David Rientjes68711a72014-06-04 16:08:25 -07001470 err = migrate_pages(&pagelist, new_page_node, NULL,
Hugh Dickins9c620e22013-02-22 16:35:14 -08001471 (unsigned long)pm, MIGRATE_SYNC, MR_SYSCALL);
Minchan Kimcf608ac2010-10-26 14:21:29 -07001472 if (err)
Naoya Horiguchie632a932013-09-11 14:22:04 -07001473 putback_movable_pages(&pagelist);
Minchan Kimcf608ac2010-10-26 14:21:29 -07001474 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001475
1476 up_read(&mm->mmap_sem);
1477 return err;
1478}
1479
1480/*
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001481 * Migrate an array of page address onto an array of nodes and fill
1482 * the corresponding array of status.
1483 */
Christoph Lameter3268c632012-03-21 16:34:06 -07001484static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001485 unsigned long nr_pages,
1486 const void __user * __user *pages,
1487 const int __user *nodes,
1488 int __user *status, int flags)
1489{
Brice Goglin3140a222009-01-06 14:38:57 -08001490 struct page_to_node *pm;
Brice Goglin3140a222009-01-06 14:38:57 -08001491 unsigned long chunk_nr_pages;
1492 unsigned long chunk_start;
1493 int err;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001494
Brice Goglin3140a222009-01-06 14:38:57 -08001495 err = -ENOMEM;
1496 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
1497 if (!pm)
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001498 goto out;
Brice Goglin35282a22009-06-16 15:32:43 -07001499
1500 migrate_prep();
1501
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001502 /*
Brice Goglin3140a222009-01-06 14:38:57 -08001503 * Store a chunk of page_to_node array in a page,
1504 * but keep the last one as a marker
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001505 */
Brice Goglin3140a222009-01-06 14:38:57 -08001506 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001507
Brice Goglin3140a222009-01-06 14:38:57 -08001508 for (chunk_start = 0;
1509 chunk_start < nr_pages;
1510 chunk_start += chunk_nr_pages) {
1511 int j;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001512
Brice Goglin3140a222009-01-06 14:38:57 -08001513 if (chunk_start + chunk_nr_pages > nr_pages)
1514 chunk_nr_pages = nr_pages - chunk_start;
1515
1516 /* fill the chunk pm with addrs and nodes from user-space */
1517 for (j = 0; j < chunk_nr_pages; j++) {
1518 const void __user *p;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001519 int node;
1520
Brice Goglin3140a222009-01-06 14:38:57 -08001521 err = -EFAULT;
1522 if (get_user(p, pages + j + chunk_start))
1523 goto out_pm;
1524 pm[j].addr = (unsigned long) p;
1525
1526 if (get_user(node, nodes + j + chunk_start))
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001527 goto out_pm;
1528
1529 err = -ENODEV;
Linus Torvalds6f5a55f2010-02-05 16:16:50 -08001530 if (node < 0 || node >= MAX_NUMNODES)
1531 goto out_pm;
1532
Lai Jiangshan389162c2012-12-12 13:51:30 -08001533 if (!node_state(node, N_MEMORY))
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001534 goto out_pm;
1535
1536 err = -EACCES;
1537 if (!node_isset(node, task_nodes))
1538 goto out_pm;
1539
Brice Goglin3140a222009-01-06 14:38:57 -08001540 pm[j].node = node;
1541 }
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001542
Brice Goglin3140a222009-01-06 14:38:57 -08001543 /* End marker for this chunk */
1544 pm[chunk_nr_pages].node = MAX_NUMNODES;
1545
1546 /* Migrate this chunk */
1547 err = do_move_page_to_node_array(mm, pm,
1548 flags & MPOL_MF_MOVE_ALL);
1549 if (err < 0)
1550 goto out_pm;
1551
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001552 /* Return status information */
Brice Goglin3140a222009-01-06 14:38:57 -08001553 for (j = 0; j < chunk_nr_pages; j++)
1554 if (put_user(pm[j].status, status + j + chunk_start)) {
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001555 err = -EFAULT;
Brice Goglin3140a222009-01-06 14:38:57 -08001556 goto out_pm;
1557 }
1558 }
1559 err = 0;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001560
1561out_pm:
Brice Goglin3140a222009-01-06 14:38:57 -08001562 free_page((unsigned long)pm);
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001563out:
1564 return err;
1565}
1566
1567/*
Brice Goglin2f007e72008-10-18 20:27:16 -07001568 * Determine the nodes of an array of pages and store it in an array of status.
Christoph Lameter742755a2006-06-23 02:03:55 -07001569 */
Brice Goglin80bba122008-12-09 13:14:23 -08001570static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1571 const void __user **pages, int *status)
Christoph Lameter742755a2006-06-23 02:03:55 -07001572{
Brice Goglin2f007e72008-10-18 20:27:16 -07001573 unsigned long i;
Brice Goglin2f007e72008-10-18 20:27:16 -07001574
Christoph Lameter742755a2006-06-23 02:03:55 -07001575 down_read(&mm->mmap_sem);
1576
Brice Goglin2f007e72008-10-18 20:27:16 -07001577 for (i = 0; i < nr_pages; i++) {
Brice Goglin80bba122008-12-09 13:14:23 -08001578 unsigned long addr = (unsigned long)(*pages);
Christoph Lameter742755a2006-06-23 02:03:55 -07001579 struct vm_area_struct *vma;
1580 struct page *page;
KOSAKI Motohiroc095adb2008-12-16 16:06:43 +09001581 int err = -EFAULT;
Brice Goglin2f007e72008-10-18 20:27:16 -07001582
1583 vma = find_vma(mm, addr);
Gleb Natapov70384dc2010-10-26 14:22:07 -07001584 if (!vma || addr < vma->vm_start)
Christoph Lameter742755a2006-06-23 02:03:55 -07001585 goto set_status;
1586
Kirill A. Shutemovd8998442015-09-04 15:47:53 -07001587 /* FOLL_DUMP to ignore special (like zero) pages */
1588 page = follow_page(vma, addr, FOLL_DUMP);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001589
1590 err = PTR_ERR(page);
1591 if (IS_ERR(page))
1592 goto set_status;
1593
Kirill A. Shutemovd8998442015-09-04 15:47:53 -07001594 err = page ? page_to_nid(page) : -ENOENT;
Christoph Lameter742755a2006-06-23 02:03:55 -07001595set_status:
Brice Goglin80bba122008-12-09 13:14:23 -08001596 *status = err;
1597
1598 pages++;
1599 status++;
1600 }
1601
1602 up_read(&mm->mmap_sem);
1603}
1604
1605/*
1606 * Determine the nodes of a user array of pages and store it in
1607 * a user array of status.
1608 */
1609static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1610 const void __user * __user *pages,
1611 int __user *status)
1612{
1613#define DO_PAGES_STAT_CHUNK_NR 16
1614 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1615 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
Brice Goglin80bba122008-12-09 13:14:23 -08001616
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001617 while (nr_pages) {
1618 unsigned long chunk_nr;
Brice Goglin80bba122008-12-09 13:14:23 -08001619
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001620 chunk_nr = nr_pages;
1621 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1622 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1623
1624 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
1625 break;
Brice Goglin80bba122008-12-09 13:14:23 -08001626
1627 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1628
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001629 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1630 break;
Christoph Lameter742755a2006-06-23 02:03:55 -07001631
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001632 pages += chunk_nr;
1633 status += chunk_nr;
1634 nr_pages -= chunk_nr;
1635 }
1636 return nr_pages ? -EFAULT : 0;
Christoph Lameter742755a2006-06-23 02:03:55 -07001637}
1638
1639/*
1640 * Move a list of pages in the address space of the currently executing
1641 * process.
1642 */
Heiko Carstens938bb9f2009-01-14 14:14:30 +01001643SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1644 const void __user * __user *, pages,
1645 const int __user *, nodes,
1646 int __user *, status, int, flags)
Christoph Lameter742755a2006-06-23 02:03:55 -07001647{
David Howellsc69e8d92008-11-14 10:39:19 +11001648 const struct cred *cred = current_cred(), *tcred;
Christoph Lameter742755a2006-06-23 02:03:55 -07001649 struct task_struct *task;
Christoph Lameter742755a2006-06-23 02:03:55 -07001650 struct mm_struct *mm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001651 int err;
Christoph Lameter3268c632012-03-21 16:34:06 -07001652 nodemask_t task_nodes;
Christoph Lameter742755a2006-06-23 02:03:55 -07001653
1654 /* Check flags */
1655 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1656 return -EINVAL;
1657
1658 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1659 return -EPERM;
1660
1661 /* Find the mm_struct */
Greg Thelena879bf52011-02-25 14:44:13 -08001662 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07001663 task = pid ? find_task_by_vpid(pid) : current;
Christoph Lameter742755a2006-06-23 02:03:55 -07001664 if (!task) {
Greg Thelena879bf52011-02-25 14:44:13 -08001665 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001666 return -ESRCH;
1667 }
Christoph Lameter3268c632012-03-21 16:34:06 -07001668 get_task_struct(task);
Christoph Lameter742755a2006-06-23 02:03:55 -07001669
1670 /*
1671 * Check if this process has the right to modify the specified
1672 * process. The right exists if the process has administrative
1673 * capabilities, superuser privileges or the same
1674 * userid as the target process.
1675 */
David Howellsc69e8d92008-11-14 10:39:19 +11001676 tcred = __task_cred(task);
Eric W. Biedermanb38a86e2012-03-12 15:48:24 -07001677 if (!uid_eq(cred->euid, tcred->suid) && !uid_eq(cred->euid, tcred->uid) &&
1678 !uid_eq(cred->uid, tcred->suid) && !uid_eq(cred->uid, tcred->uid) &&
Christoph Lameter742755a2006-06-23 02:03:55 -07001679 !capable(CAP_SYS_NICE)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001680 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001681 err = -EPERM;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001682 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001683 }
David Howellsc69e8d92008-11-14 10:39:19 +11001684 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001685
David Quigley86c3a762006-06-23 02:04:02 -07001686 err = security_task_movememory(task);
1687 if (err)
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001688 goto out;
David Quigley86c3a762006-06-23 02:04:02 -07001689
Christoph Lameter3268c632012-03-21 16:34:06 -07001690 task_nodes = cpuset_mems_allowed(task);
1691 mm = get_task_mm(task);
1692 put_task_struct(task);
1693
Sasha Levin6e8b09e2012-04-25 16:01:53 -07001694 if (!mm)
1695 return -EINVAL;
1696
1697 if (nodes)
1698 err = do_pages_move(mm, task_nodes, nr_pages, pages,
1699 nodes, status, flags);
1700 else
1701 err = do_pages_stat(mm, nr_pages, pages, status);
Christoph Lameter3268c632012-03-21 16:34:06 -07001702
1703 mmput(mm);
1704 return err;
David Quigley86c3a762006-06-23 02:04:02 -07001705
Christoph Lameter742755a2006-06-23 02:03:55 -07001706out:
Christoph Lameter3268c632012-03-21 16:34:06 -07001707 put_task_struct(task);
Christoph Lameter742755a2006-06-23 02:03:55 -07001708 return err;
1709}
Christoph Lameter742755a2006-06-23 02:03:55 -07001710
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001711#ifdef CONFIG_NUMA_BALANCING
1712/*
1713 * Returns true if this is a safe migration target node for misplaced NUMA
1714 * pages. Currently it only checks the watermarks which crude
1715 */
1716static bool migrate_balanced_pgdat(struct pglist_data *pgdat,
Mel Gorman3abef4e2013-02-22 16:34:27 -08001717 unsigned long nr_migrate_pages)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001718{
1719 int z;
Mel Gorman599d0c92016-07-28 15:45:31 -07001720
1721 if (!pgdat_reclaimable(pgdat))
1722 return false;
1723
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001724 for (z = pgdat->nr_zones - 1; z >= 0; z--) {
1725 struct zone *zone = pgdat->node_zones + z;
1726
1727 if (!populated_zone(zone))
1728 continue;
1729
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001730 /* Avoid waking kswapd by allocating pages_to_migrate pages. */
1731 if (!zone_watermark_ok(zone, 0,
1732 high_wmark_pages(zone) +
1733 nr_migrate_pages,
1734 0, 0))
1735 continue;
1736 return true;
1737 }
1738 return false;
1739}
1740
1741static struct page *alloc_misplaced_dst_page(struct page *page,
1742 unsigned long data,
1743 int **result)
1744{
1745 int nid = (int) data;
1746 struct page *newpage;
1747
Vlastimil Babka96db8002015-09-08 15:03:50 -07001748 newpage = __alloc_pages_node(nid,
Johannes Weinere97ca8e52014-03-10 15:49:43 -07001749 (GFP_HIGHUSER_MOVABLE |
1750 __GFP_THISNODE | __GFP_NOMEMALLOC |
1751 __GFP_NORETRY | __GFP_NOWARN) &
Mel Gorman8479eba2016-02-26 15:19:31 -08001752 ~__GFP_RECLAIM, 0);
Hillf Dantonbac03822012-11-27 14:46:24 +00001753
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001754 return newpage;
1755}
1756
1757/*
Mel Gormana8f60772012-11-14 21:41:46 +00001758 * page migration rate limiting control.
1759 * Do not migrate more than @pages_to_migrate in a @migrate_interval_millisecs
1760 * window of time. Default here says do not migrate more than 1280M per second.
1761 */
1762static unsigned int migrate_interval_millisecs __read_mostly = 100;
1763static unsigned int ratelimit_pages __read_mostly = 128 << (20 - PAGE_SHIFT);
1764
Mel Gormanb32967f2012-11-19 12:35:47 +00001765/* Returns true if the node is migrate rate-limited after the update */
Mel Gorman1c30e012014-01-21 15:50:58 -08001766static bool numamigrate_update_ratelimit(pg_data_t *pgdat,
1767 unsigned long nr_pages)
Mel Gormanb32967f2012-11-19 12:35:47 +00001768{
Mel Gormanb32967f2012-11-19 12:35:47 +00001769 /*
1770 * Rate-limit the amount of data that is being migrated to a node.
1771 * Optimal placement is no good if the memory bus is saturated and
1772 * all the time is being spent migrating!
1773 */
Mel Gormanb32967f2012-11-19 12:35:47 +00001774 if (time_after(jiffies, pgdat->numabalancing_migrate_next_window)) {
Mel Gorman1c5e9c22014-01-21 15:50:59 -08001775 spin_lock(&pgdat->numabalancing_migrate_lock);
Mel Gormanb32967f2012-11-19 12:35:47 +00001776 pgdat->numabalancing_migrate_nr_pages = 0;
1777 pgdat->numabalancing_migrate_next_window = jiffies +
1778 msecs_to_jiffies(migrate_interval_millisecs);
Mel Gorman1c5e9c22014-01-21 15:50:59 -08001779 spin_unlock(&pgdat->numabalancing_migrate_lock);
Mel Gormanb32967f2012-11-19 12:35:47 +00001780 }
Mel Gormanaf1839d2014-01-21 15:51:01 -08001781 if (pgdat->numabalancing_migrate_nr_pages > ratelimit_pages) {
1782 trace_mm_numa_migrate_ratelimit(current, pgdat->node_id,
1783 nr_pages);
Mel Gorman1c5e9c22014-01-21 15:50:59 -08001784 return true;
Mel Gormanaf1839d2014-01-21 15:51:01 -08001785 }
Mel Gorman1c5e9c22014-01-21 15:50:59 -08001786
1787 /*
1788 * This is an unlocked non-atomic update so errors are possible.
1789 * The consequences are failing to migrate when we potentiall should
1790 * have which is not severe enough to warrant locking. If it is ever
1791 * a problem, it can be converted to a per-cpu counter.
1792 */
1793 pgdat->numabalancing_migrate_nr_pages += nr_pages;
1794 return false;
Mel Gormanb32967f2012-11-19 12:35:47 +00001795}
1796
Mel Gorman1c30e012014-01-21 15:50:58 -08001797static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
Mel Gormanb32967f2012-11-19 12:35:47 +00001798{
Hugh Dickins340ef392013-02-22 16:34:33 -08001799 int page_lru;
Mel Gormanb32967f2012-11-19 12:35:47 +00001800
Sasha Levin309381fea2014-01-23 15:52:54 -08001801 VM_BUG_ON_PAGE(compound_order(page) && !PageTransHuge(page), page);
Mel Gorman3abef4e2013-02-22 16:34:27 -08001802
Mel Gormanb32967f2012-11-19 12:35:47 +00001803 /* Avoid migrating to a node that is nearly full */
Hugh Dickins340ef392013-02-22 16:34:33 -08001804 if (!migrate_balanced_pgdat(pgdat, 1UL << compound_order(page)))
1805 return 0;
Mel Gormanb32967f2012-11-19 12:35:47 +00001806
Hugh Dickins340ef392013-02-22 16:34:33 -08001807 if (isolate_lru_page(page))
1808 return 0;
Mel Gormanb32967f2012-11-19 12:35:47 +00001809
1810 /*
Hugh Dickins340ef392013-02-22 16:34:33 -08001811 * migrate_misplaced_transhuge_page() skips page migration's usual
1812 * check on page_count(), so we must do it here, now that the page
1813 * has been isolated: a GUP pin, or any other pin, prevents migration.
1814 * The expected page count is 3: 1 for page's mapcount and 1 for the
1815 * caller's pin and 1 for the reference taken by isolate_lru_page().
1816 */
1817 if (PageTransHuge(page) && page_count(page) != 3) {
1818 putback_lru_page(page);
1819 return 0;
1820 }
1821
1822 page_lru = page_is_file_cache(page);
Mel Gorman599d0c92016-07-28 15:45:31 -07001823 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_lru,
Hugh Dickins340ef392013-02-22 16:34:33 -08001824 hpage_nr_pages(page));
1825
1826 /*
1827 * Isolating the page has taken another reference, so the
1828 * caller's reference can be safely dropped without the page
1829 * disappearing underneath us during migration.
Mel Gormanb32967f2012-11-19 12:35:47 +00001830 */
1831 put_page(page);
Hugh Dickins340ef392013-02-22 16:34:33 -08001832 return 1;
Mel Gormanb32967f2012-11-19 12:35:47 +00001833}
1834
Mel Gormande466bd2013-12-18 17:08:42 -08001835bool pmd_trans_migrating(pmd_t pmd)
1836{
1837 struct page *page = pmd_page(pmd);
1838 return PageLocked(page);
1839}
1840
Mel Gormana8f60772012-11-14 21:41:46 +00001841/*
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001842 * Attempt to migrate a misplaced page to the specified destination
1843 * node. Caller is expected to have an elevated reference count on
1844 * the page that will be dropped by this function before returning.
1845 */
Mel Gorman1bc115d2013-10-07 11:29:05 +01001846int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma,
1847 int node)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001848{
Mel Gormana8f60772012-11-14 21:41:46 +00001849 pg_data_t *pgdat = NODE_DATA(node);
Hugh Dickins340ef392013-02-22 16:34:33 -08001850 int isolated;
Mel Gormanb32967f2012-11-19 12:35:47 +00001851 int nr_remaining;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001852 LIST_HEAD(migratepages);
1853
1854 /*
Mel Gorman1bc115d2013-10-07 11:29:05 +01001855 * Don't migrate file pages that are mapped in multiple processes
1856 * with execute permissions as they are probably shared libraries.
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001857 */
Mel Gorman1bc115d2013-10-07 11:29:05 +01001858 if (page_mapcount(page) != 1 && page_is_file_cache(page) &&
1859 (vma->vm_flags & VM_EXEC))
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001860 goto out;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001861
Mel Gormana8f60772012-11-14 21:41:46 +00001862 /*
1863 * Rate-limit the amount of data that is being migrated to a node.
1864 * Optimal placement is no good if the memory bus is saturated and
1865 * all the time is being spent migrating!
1866 */
Hugh Dickins340ef392013-02-22 16:34:33 -08001867 if (numamigrate_update_ratelimit(pgdat, 1))
Mel Gormana8f60772012-11-14 21:41:46 +00001868 goto out;
Mel Gormana8f60772012-11-14 21:41:46 +00001869
Mel Gormanb32967f2012-11-19 12:35:47 +00001870 isolated = numamigrate_isolate_page(pgdat, page);
1871 if (!isolated)
1872 goto out;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001873
Mel Gormanb32967f2012-11-19 12:35:47 +00001874 list_add(&page->lru, &migratepages);
Hugh Dickins9c620e22013-02-22 16:35:14 -08001875 nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_page,
David Rientjes68711a72014-06-04 16:08:25 -07001876 NULL, node, MIGRATE_ASYNC,
1877 MR_NUMA_MISPLACED);
Mel Gormanb32967f2012-11-19 12:35:47 +00001878 if (nr_remaining) {
Joonsoo Kim59c82b72014-01-21 15:51:17 -08001879 if (!list_empty(&migratepages)) {
1880 list_del(&page->lru);
Mel Gorman599d0c92016-07-28 15:45:31 -07001881 dec_node_page_state(page, NR_ISOLATED_ANON +
Joonsoo Kim59c82b72014-01-21 15:51:17 -08001882 page_is_file_cache(page));
1883 putback_lru_page(page);
1884 }
Mel Gormanb32967f2012-11-19 12:35:47 +00001885 isolated = 0;
1886 } else
1887 count_vm_numa_event(NUMA_PAGE_MIGRATE);
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001888 BUG_ON(!list_empty(&migratepages));
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001889 return isolated;
Hugh Dickins340ef392013-02-22 16:34:33 -08001890
1891out:
1892 put_page(page);
1893 return 0;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001894}
Mel Gorman220018d2012-12-05 09:32:56 +00001895#endif /* CONFIG_NUMA_BALANCING */
Mel Gormanb32967f2012-11-19 12:35:47 +00001896
Mel Gorman220018d2012-12-05 09:32:56 +00001897#if defined(CONFIG_NUMA_BALANCING) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
Hugh Dickins340ef392013-02-22 16:34:33 -08001898/*
1899 * Migrates a THP to a given target node. page must be locked and is unlocked
1900 * before returning.
1901 */
Mel Gormanb32967f2012-11-19 12:35:47 +00001902int migrate_misplaced_transhuge_page(struct mm_struct *mm,
1903 struct vm_area_struct *vma,
1904 pmd_t *pmd, pmd_t entry,
1905 unsigned long address,
1906 struct page *page, int node)
1907{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001908 spinlock_t *ptl;
Mel Gormanb32967f2012-11-19 12:35:47 +00001909 pg_data_t *pgdat = NODE_DATA(node);
1910 int isolated = 0;
1911 struct page *new_page = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001912 int page_lru = page_is_file_cache(page);
Mel Gormanf714f4f2013-12-18 17:08:33 -08001913 unsigned long mmun_start = address & HPAGE_PMD_MASK;
1914 unsigned long mmun_end = mmun_start + HPAGE_PMD_SIZE;
Mel Gorman2b4847e2013-12-18 17:08:32 -08001915 pmd_t orig_entry;
Mel Gormanb32967f2012-11-19 12:35:47 +00001916
1917 /*
Mel Gormanb32967f2012-11-19 12:35:47 +00001918 * Rate-limit the amount of data that is being migrated to a node.
1919 * Optimal placement is no good if the memory bus is saturated and
1920 * all the time is being spent migrating!
1921 */
Mel Gormand28d43352012-11-29 09:24:36 +00001922 if (numamigrate_update_ratelimit(pgdat, HPAGE_PMD_NR))
Mel Gormanb32967f2012-11-19 12:35:47 +00001923 goto out_dropref;
1924
1925 new_page = alloc_pages_node(node,
Vlastimil Babka25160352016-07-28 15:49:25 -07001926 (GFP_TRANSHUGE_LIGHT | __GFP_THISNODE),
Johannes Weinere97ca8e52014-03-10 15:49:43 -07001927 HPAGE_PMD_ORDER);
Hugh Dickins340ef392013-02-22 16:34:33 -08001928 if (!new_page)
1929 goto out_fail;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08001930 prep_transhuge_page(new_page);
Hugh Dickins340ef392013-02-22 16:34:33 -08001931
Mel Gormanb32967f2012-11-19 12:35:47 +00001932 isolated = numamigrate_isolate_page(pgdat, page);
Hugh Dickins340ef392013-02-22 16:34:33 -08001933 if (!isolated) {
Mel Gormanb32967f2012-11-19 12:35:47 +00001934 put_page(new_page);
Hugh Dickins340ef392013-02-22 16:34:33 -08001935 goto out_fail;
Mel Gormanb32967f2012-11-19 12:35:47 +00001936 }
Aneesh Kumar K.V458aa762016-03-17 14:18:56 -07001937 /*
1938 * We are not sure a pending tlb flush here is for a huge page
1939 * mapping or not. Hence use the tlb range variant
1940 */
Mel Gormanb0943d62013-12-18 17:08:46 -08001941 if (mm_tlb_flush_pending(mm))
1942 flush_tlb_range(vma, mmun_start, mmun_end);
1943
Mel Gormanb32967f2012-11-19 12:35:47 +00001944 /* Prepare a page as a migration target */
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -08001945 __SetPageLocked(new_page);
Hugh Dickinsfa9949d2016-05-19 17:12:41 -07001946 __SetPageSwapBacked(new_page);
Mel Gormanb32967f2012-11-19 12:35:47 +00001947
1948 /* anon mapping, we can simply copy page->mapping to the new page: */
1949 new_page->mapping = page->mapping;
1950 new_page->index = page->index;
1951 migrate_page_copy(new_page, page);
1952 WARN_ON(PageLRU(new_page));
1953
1954 /* Recheck the target PMD */
Mel Gormanf714f4f2013-12-18 17:08:33 -08001955 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001956 ptl = pmd_lock(mm, pmd);
Mel Gorman2b4847e2013-12-18 17:08:32 -08001957 if (unlikely(!pmd_same(*pmd, entry) || page_count(page) != 2)) {
1958fail_putback:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001959 spin_unlock(ptl);
Mel Gormanf714f4f2013-12-18 17:08:33 -08001960 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Mel Gormanb32967f2012-11-19 12:35:47 +00001961
1962 /* Reverse changes made by migrate_page_copy() */
1963 if (TestClearPageActive(new_page))
1964 SetPageActive(page);
1965 if (TestClearPageUnevictable(new_page))
1966 SetPageUnevictable(page);
Mel Gormanb32967f2012-11-19 12:35:47 +00001967
1968 unlock_page(new_page);
1969 put_page(new_page); /* Free it */
1970
Mel Gormana54a4072013-10-07 11:28:46 +01001971 /* Retake the callers reference and putback on LRU */
1972 get_page(page);
Mel Gormanb32967f2012-11-19 12:35:47 +00001973 putback_lru_page(page);
Mel Gorman599d0c92016-07-28 15:45:31 -07001974 mod_node_page_state(page_pgdat(page),
Mel Gormana54a4072013-10-07 11:28:46 +01001975 NR_ISOLATED_ANON + page_lru, -HPAGE_PMD_NR);
Mel Gormaneb4489f62013-12-18 17:08:39 -08001976
1977 goto out_unlock;
Mel Gormanb32967f2012-11-19 12:35:47 +00001978 }
1979
Mel Gorman2b4847e2013-12-18 17:08:32 -08001980 orig_entry = *pmd;
Kirill A. Shutemov10102452016-07-26 15:25:29 -07001981 entry = mk_huge_pmd(new_page, vma->vm_page_prot);
Mel Gorman2b4847e2013-12-18 17:08:32 -08001982 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Mel Gormanb32967f2012-11-19 12:35:47 +00001983
Mel Gorman2b4847e2013-12-18 17:08:32 -08001984 /*
1985 * Clear the old entry under pagetable lock and establish the new PTE.
1986 * Any parallel GUP will either observe the old page blocking on the
1987 * page lock, block on the page table lock or observe the new page.
1988 * The SetPageUptodate on the new page and page_add_new_anon_rmap
1989 * guarantee the copy is visible before the pagetable update.
1990 */
Mel Gormanf714f4f2013-12-18 17:08:33 -08001991 flush_cache_range(vma, mmun_start, mmun_end);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001992 page_add_anon_rmap(new_page, vma, mmun_start, true);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001993 pmdp_huge_clear_flush_notify(vma, mmun_start, pmd);
Mel Gormanf714f4f2013-12-18 17:08:33 -08001994 set_pmd_at(mm, mmun_start, pmd, entry);
Stephen Rothwellce4a9cc2012-12-10 19:50:57 +11001995 update_mmu_cache_pmd(vma, address, &entry);
Mel Gorman2b4847e2013-12-18 17:08:32 -08001996
1997 if (page_count(page) != 2) {
Mel Gormanf714f4f2013-12-18 17:08:33 -08001998 set_pmd_at(mm, mmun_start, pmd, orig_entry);
Aneesh Kumar K.V458aa762016-03-17 14:18:56 -07001999 flush_pmd_tlb_range(vma, mmun_start, mmun_end);
Joerg Roedel34ee6452014-11-13 13:46:09 +11002000 mmu_notifier_invalidate_range(mm, mmun_start, mmun_end);
Mel Gorman2b4847e2013-12-18 17:08:32 -08002001 update_mmu_cache_pmd(vma, address, &entry);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002002 page_remove_rmap(new_page, true);
Mel Gorman2b4847e2013-12-18 17:08:32 -08002003 goto fail_putback;
2004 }
2005
Hugh Dickins51afb122015-11-05 18:49:37 -08002006 mlock_migrate_page(new_page, page);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002007 page_remove_rmap(page, true);
Vlastimil Babka7cd12b42016-03-15 14:56:18 -07002008 set_page_owner_migrate_reason(new_page, MR_NUMA_MISPLACED);
Mel Gorman2b4847e2013-12-18 17:08:32 -08002009
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002010 spin_unlock(ptl);
Mel Gormanf714f4f2013-12-18 17:08:33 -08002011 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Mel Gormanb32967f2012-11-19 12:35:47 +00002012
Mel Gorman11de9922014-06-04 16:07:41 -07002013 /* Take an "isolate" reference and put new page on the LRU. */
2014 get_page(new_page);
2015 putback_lru_page(new_page);
2016
Mel Gormanb32967f2012-11-19 12:35:47 +00002017 unlock_page(new_page);
2018 unlock_page(page);
2019 put_page(page); /* Drop the rmap reference */
2020 put_page(page); /* Drop the LRU isolation reference */
2021
2022 count_vm_events(PGMIGRATE_SUCCESS, HPAGE_PMD_NR);
2023 count_vm_numa_events(NUMA_PAGE_MIGRATE, HPAGE_PMD_NR);
2024
Mel Gorman599d0c92016-07-28 15:45:31 -07002025 mod_node_page_state(page_pgdat(page),
Mel Gormanb32967f2012-11-19 12:35:47 +00002026 NR_ISOLATED_ANON + page_lru,
2027 -HPAGE_PMD_NR);
2028 return isolated;
2029
Hugh Dickins340ef392013-02-22 16:34:33 -08002030out_fail:
2031 count_vm_events(PGMIGRATE_FAIL, HPAGE_PMD_NR);
Mel Gormanb32967f2012-11-19 12:35:47 +00002032out_dropref:
Mel Gorman2b4847e2013-12-18 17:08:32 -08002033 ptl = pmd_lock(mm, pmd);
2034 if (pmd_same(*pmd, entry)) {
Mel Gorman4d942462015-02-12 14:58:28 -08002035 entry = pmd_modify(entry, vma->vm_page_prot);
Mel Gormanf714f4f2013-12-18 17:08:33 -08002036 set_pmd_at(mm, mmun_start, pmd, entry);
Mel Gorman2b4847e2013-12-18 17:08:32 -08002037 update_mmu_cache_pmd(vma, address, &entry);
2038 }
2039 spin_unlock(ptl);
Mel Gormana54a4072013-10-07 11:28:46 +01002040
Mel Gormaneb4489f62013-12-18 17:08:39 -08002041out_unlock:
Hugh Dickins340ef392013-02-22 16:34:33 -08002042 unlock_page(page);
Mel Gormanb32967f2012-11-19 12:35:47 +00002043 put_page(page);
Mel Gormanb32967f2012-11-19 12:35:47 +00002044 return 0;
2045}
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002046#endif /* CONFIG_NUMA_BALANCING */
2047
2048#endif /* CONFIG_NUMA */