blob: dfc8300ecbb273fe3d7f5ef37e21e20a63b58c84 [file] [log] [blame]
Christoph Lameterb20a3502006-03-22 00:09:12 -08001/*
2 * Memory Migration functionality - linux/mm/migration.c
3 *
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>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080033#include <linux/memcontrol.h>
Adrian Bunk4f5ca262008-07-23 21:27:02 -070034#include <linux/syscalls.h>
Naoya Horiguchi290408d2010-09-08 10:19:35 +090035#include <linux/hugetlb.h>
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -070036#include <linux/hugetlb_cgroup.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/gfp.h>
Rafael Aquinibf6bddf2012-12-11 16:02:42 -080038#include <linux/balloon_compaction.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080039
Michal Nazarewicz0d1836c2010-12-21 17:24:26 -080040#include <asm/tlbflush.h>
41
Mel Gorman7b2a2d42012-10-19 14:07:31 +010042#define CREATE_TRACE_POINTS
43#include <trace/events/migrate.h>
44
Christoph Lameterb20a3502006-03-22 00:09:12 -080045#include "internal.h"
46
Christoph Lameterb20a3502006-03-22 00:09:12 -080047/*
Christoph Lameter742755a2006-06-23 02:03:55 -070048 * migrate_prep() needs to be called before we start compiling a list of pages
Mel Gorman748446b2010-05-24 14:32:27 -070049 * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is
50 * undesirable, use migrate_prep_local()
Christoph Lameterb20a3502006-03-22 00:09:12 -080051 */
52int migrate_prep(void)
53{
Christoph Lameterb20a3502006-03-22 00:09:12 -080054 /*
55 * Clear the LRU lists so pages can be isolated.
56 * Note that pages may be moved off the LRU after we have
57 * drained them. Those pages will fail to migrate like other
58 * pages that may be busy.
59 */
60 lru_add_drain_all();
61
62 return 0;
63}
64
Mel Gorman748446b2010-05-24 14:32:27 -070065/* Do the necessary work of migrate_prep but not if it involves other CPUs */
66int migrate_prep_local(void)
67{
68 lru_add_drain();
69
70 return 0;
71}
72
Christoph Lameterb20a3502006-03-22 00:09:12 -080073/*
Lee Schermerhorn894bc312008-10-18 20:26:39 -070074 * Add isolated pages on the list back to the LRU under page lock
75 * to avoid leaking evictable pages back onto unevictable list.
Christoph Lameterb20a3502006-03-22 00:09:12 -080076 */
Minchan Kime13861d2010-05-24 14:31:59 -070077void putback_lru_pages(struct list_head *l)
Christoph Lameterb20a3502006-03-22 00:09:12 -080078{
79 struct page *page;
80 struct page *page2;
Christoph Lameterb20a3502006-03-22 00:09:12 -080081
82 list_for_each_entry_safe(page, page2, l, lru) {
Christoph Lametere24f0b82006-06-23 02:03:51 -070083 list_del(&page->lru);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -070084 dec_zone_page_state(page, NR_ISOLATED_ANON +
Johannes Weiner6c0b1352009-09-21 17:02:59 -070085 page_is_file_cache(page));
Rafael Aquini5733c7d2012-12-11 16:02:47 -080086 putback_lru_page(page);
87 }
88}
89
90/*
91 * Put previously isolated pages back onto the appropriate lists
92 * from where they were once taken off for compaction/migration.
93 *
94 * This function shall be used instead of putback_lru_pages(),
95 * whenever the isolated pageset has been built by isolate_migratepages_range()
96 */
97void putback_movable_pages(struct list_head *l)
98{
99 struct page *page;
100 struct page *page2;
101
102 list_for_each_entry_safe(page, page2, l, lru) {
Naoya Horiguchi31caf662013-09-11 14:21:59 -0700103 if (unlikely(PageHuge(page))) {
104 putback_active_hugepage(page);
105 continue;
106 }
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800107 list_del(&page->lru);
108 dec_zone_page_state(page, NR_ISOLATED_ANON +
109 page_is_file_cache(page));
Rafael Aquini117aad12013-09-30 13:45:16 -0700110 if (unlikely(isolated_balloon_page(page)))
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800111 balloon_page_putback(page);
112 else
113 putback_lru_page(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800114 }
Christoph Lameterb20a3502006-03-22 00:09:12 -0800115}
116
Christoph Lameter06972122006-06-23 02:03:35 -0700117/*
118 * Restore a potential migration pte to a working pte entry
119 */
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800120static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,
121 unsigned long addr, void *old)
Christoph Lameter06972122006-06-23 02:03:35 -0700122{
123 struct mm_struct *mm = vma->vm_mm;
124 swp_entry_t entry;
Christoph Lameter06972122006-06-23 02:03:35 -0700125 pmd_t *pmd;
126 pte_t *ptep, pte;
127 spinlock_t *ptl;
128
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900129 if (unlikely(PageHuge(new))) {
130 ptep = huge_pte_offset(mm, addr);
131 if (!ptep)
132 goto out;
133 ptl = &mm->page_table_lock;
134 } else {
Bob Liu62190492012-12-11 16:00:37 -0800135 pmd = mm_find_pmd(mm, addr);
136 if (!pmd)
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900137 goto out;
Andrea Arcangeli500d65d2011-01-13 15:46:55 -0800138 if (pmd_trans_huge(*pmd))
139 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700140
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900141 ptep = pte_offset_map(pmd, addr);
Christoph Lameter06972122006-06-23 02:03:35 -0700142
Hugh Dickins486cf462011-10-19 12:50:35 -0700143 /*
144 * Peek to check is_swap_pte() before taking ptlock? No, we
145 * can race mremap's move_ptes(), which skips anon_vma lock.
146 */
Christoph Lameter06972122006-06-23 02:03:35 -0700147
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900148 ptl = pte_lockptr(mm, pmd);
149 }
150
Christoph Lameter06972122006-06-23 02:03:35 -0700151 spin_lock(ptl);
152 pte = *ptep;
153 if (!is_swap_pte(pte))
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800154 goto unlock;
Christoph Lameter06972122006-06-23 02:03:35 -0700155
156 entry = pte_to_swp_entry(pte);
157
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800158 if (!is_migration_entry(entry) ||
159 migration_entry_to_page(entry) != old)
160 goto unlock;
Christoph Lameter06972122006-06-23 02:03:35 -0700161
Christoph Lameter06972122006-06-23 02:03:35 -0700162 get_page(new);
163 pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
Cyrill Gorcunovc3d16e12013-10-16 13:46:51 -0700164 if (pte_swp_soft_dirty(*ptep))
165 pte = pte_mksoft_dirty(pte);
Christoph Lameter06972122006-06-23 02:03:35 -0700166 if (is_write_migration_entry(entry))
167 pte = pte_mkwrite(pte);
Andi Kleen3ef8fd72010-10-11 16:03:21 +0200168#ifdef CONFIG_HUGETLB_PAGE
Tony Lube7517d2013-02-04 14:28:46 -0800169 if (PageHuge(new)) {
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900170 pte = pte_mkhuge(pte);
Tony Lube7517d2013-02-04 14:28:46 -0800171 pte = arch_make_huge_pte(pte, vma, new, 0);
172 }
Andi Kleen3ef8fd72010-10-11 16:03:21 +0200173#endif
Leonid Yegoshinc2cc4992013-05-24 15:55:18 -0700174 flush_dcache_page(new);
Christoph Lameter06972122006-06-23 02:03:35 -0700175 set_pte_at(mm, addr, ptep, pte);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700176
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900177 if (PageHuge(new)) {
178 if (PageAnon(new))
179 hugepage_add_anon_rmap(new, vma, addr);
180 else
181 page_dup_rmap(new);
182 } else if (PageAnon(new))
Christoph Lameter04e62a22006-06-23 02:03:38 -0700183 page_add_anon_rmap(new, vma, addr);
184 else
185 page_add_file_rmap(new);
186
187 /* No need to invalidate - it was non-present before */
Russell King4b3073e2009-12-18 16:40:18 +0000188 update_mmu_cache(vma, addr, ptep);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800189unlock:
Christoph Lameter06972122006-06-23 02:03:35 -0700190 pte_unmap_unlock(ptep, ptl);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800191out:
192 return SWAP_AGAIN;
Christoph Lameter06972122006-06-23 02:03:35 -0700193}
194
195/*
Christoph Lameter04e62a22006-06-23 02:03:38 -0700196 * Get rid of all migration entries and replace them by
197 * references to the indicated page.
198 */
199static void remove_migration_ptes(struct page *old, struct page *new)
200{
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800201 rmap_walk(new, remove_migration_pte, old);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700202}
203
204/*
Christoph Lameter06972122006-06-23 02:03:35 -0700205 * Something used the pte of a page under migration. We need to
206 * get to the page and wait until migration is finished.
207 * When we return from this function the fault will be retried.
Christoph Lameter06972122006-06-23 02:03:35 -0700208 */
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700209static void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
210 spinlock_t *ptl)
Christoph Lameter06972122006-06-23 02:03:35 -0700211{
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700212 pte_t pte;
Christoph Lameter06972122006-06-23 02:03:35 -0700213 swp_entry_t entry;
214 struct page *page;
215
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700216 spin_lock(ptl);
Christoph Lameter06972122006-06-23 02:03:35 -0700217 pte = *ptep;
218 if (!is_swap_pte(pte))
219 goto out;
220
221 entry = pte_to_swp_entry(pte);
222 if (!is_migration_entry(entry))
223 goto out;
224
225 page = migration_entry_to_page(entry);
226
Nick Piggine2867812008-07-25 19:45:30 -0700227 /*
228 * Once radix-tree replacement of page migration started, page_count
229 * *must* be zero. And, we don't want to call wait_on_page_locked()
230 * against a page without get_page().
231 * So, we use get_page_unless_zero(), here. Even failed, page fault
232 * will occur again.
233 */
234 if (!get_page_unless_zero(page))
235 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700236 pte_unmap_unlock(ptep, ptl);
237 wait_on_page_locked(page);
238 put_page(page);
239 return;
240out:
241 pte_unmap_unlock(ptep, ptl);
242}
243
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700244void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
245 unsigned long address)
246{
247 spinlock_t *ptl = pte_lockptr(mm, pmd);
248 pte_t *ptep = pte_offset_map(pmd, address);
249 __migration_entry_wait(mm, ptep, ptl);
250}
251
252void migration_entry_wait_huge(struct mm_struct *mm, pte_t *pte)
253{
254 spinlock_t *ptl = &(mm)->page_table_lock;
255 __migration_entry_wait(mm, pte, ptl);
256}
257
Mel Gormanb969c4a2012-01-12 17:19:34 -0800258#ifdef CONFIG_BLOCK
259/* Returns true if all buffers are successfully locked */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800260static bool buffer_migrate_lock_buffers(struct buffer_head *head,
261 enum migrate_mode mode)
Mel Gormanb969c4a2012-01-12 17:19:34 -0800262{
263 struct buffer_head *bh = head;
264
265 /* Simple case, sync compaction */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800266 if (mode != MIGRATE_ASYNC) {
Mel Gormanb969c4a2012-01-12 17:19:34 -0800267 do {
268 get_bh(bh);
269 lock_buffer(bh);
270 bh = bh->b_this_page;
271
272 } while (bh != head);
273
274 return true;
275 }
276
277 /* async case, we cannot block on lock_buffer so use trylock_buffer */
278 do {
279 get_bh(bh);
280 if (!trylock_buffer(bh)) {
281 /*
282 * We failed to lock the buffer and cannot stall in
283 * async migration. Release the taken locks
284 */
285 struct buffer_head *failed_bh = bh;
286 put_bh(failed_bh);
287 bh = head;
288 while (bh != failed_bh) {
289 unlock_buffer(bh);
290 put_bh(bh);
291 bh = bh->b_this_page;
292 }
293 return false;
294 }
295
296 bh = bh->b_this_page;
297 } while (bh != head);
298 return true;
299}
300#else
301static inline bool buffer_migrate_lock_buffers(struct buffer_head *head,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800302 enum migrate_mode mode)
Mel Gormanb969c4a2012-01-12 17:19:34 -0800303{
304 return true;
305}
306#endif /* CONFIG_BLOCK */
307
Christoph Lameterb20a3502006-03-22 00:09:12 -0800308/*
Christoph Lameterc3fcf8a2006-06-23 02:03:32 -0700309 * Replace the page in the mapping.
Christoph Lameter5b5c7122006-06-23 02:03:29 -0700310 *
311 * The number of remaining references must be:
312 * 1 for anonymous pages without a mapping
313 * 2 for pages with a mapping
David Howells266cf652009-04-03 16:42:36 +0100314 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800315 */
Gu Zheng36bc08c2013-07-16 17:56:16 +0800316int migrate_page_move_mapping(struct address_space *mapping,
Mel Gormanb969c4a2012-01-12 17:19:34 -0800317 struct page *newpage, struct page *page,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800318 struct buffer_head *head, enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800319{
Peter Zijlstra7039e1d2012-10-25 14:16:34 +0200320 int expected_count = 0;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800321 void **pslot;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800322
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700323 if (!mapping) {
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700324 /* Anonymous page without mapping */
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700325 if (page_count(page) != 1)
326 return -EAGAIN;
Rafael Aquini78bd5202012-12-11 16:02:31 -0800327 return MIGRATEPAGE_SUCCESS;
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700328 }
329
Nick Piggin19fd6232008-07-25 19:45:32 -0700330 spin_lock_irq(&mapping->tree_lock);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800331
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800332 pslot = radix_tree_lookup_slot(&mapping->page_tree,
333 page_index(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800334
Johannes Weineredcf4742009-09-21 17:02:59 -0700335 expected_count = 2 + page_has_private(page);
Nick Piggine2867812008-07-25 19:45:30 -0700336 if (page_count(page) != expected_count ||
Mel Gorman29c1f672011-01-13 15:47:21 -0800337 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700338 spin_unlock_irq(&mapping->tree_lock);
Christoph Lametere23ca002006-04-10 22:52:57 -0700339 return -EAGAIN;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800340 }
341
Nick Piggine2867812008-07-25 19:45:30 -0700342 if (!page_freeze_refs(page, expected_count)) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700343 spin_unlock_irq(&mapping->tree_lock);
Nick Piggine2867812008-07-25 19:45:30 -0700344 return -EAGAIN;
345 }
346
Christoph Lameterb20a3502006-03-22 00:09:12 -0800347 /*
Mel Gormanb969c4a2012-01-12 17:19:34 -0800348 * In the async migration case of moving a page with buffers, lock the
349 * buffers using trylock before the mapping is moved. If the mapping
350 * was moved, we later failed to lock the buffers and could not move
351 * the mapping back due to an elevated page count, we would have to
352 * block waiting on other references to be dropped.
353 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800354 if (mode == MIGRATE_ASYNC && head &&
355 !buffer_migrate_lock_buffers(head, mode)) {
Mel Gormanb969c4a2012-01-12 17:19:34 -0800356 page_unfreeze_refs(page, expected_count);
357 spin_unlock_irq(&mapping->tree_lock);
358 return -EAGAIN;
359 }
360
361 /*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800362 * Now we know that no one else is looking at the page.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800363 */
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800364 get_page(newpage); /* add cache reference */
Christoph Lameterb20a3502006-03-22 00:09:12 -0800365 if (PageSwapCache(page)) {
366 SetPageSwapCache(newpage);
367 set_page_private(newpage, page_private(page));
368 }
369
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800370 radix_tree_replace_slot(pslot, newpage);
371
372 /*
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800373 * Drop cache reference from old page by unfreezing
374 * to one less reference.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800375 * We know this isn't the last reference.
376 */
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800377 page_unfreeze_refs(page, expected_count - 1);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800378
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700379 /*
380 * If moved to a different zone then also account
381 * the page for that zone. Other VM counters will be
382 * taken care of when we establish references to the
383 * new page and drop references to the old page.
384 *
385 * Note that anonymous pages are accounted for
386 * via NR_FILE_PAGES and NR_ANON_PAGES if they
387 * are mapped to swap space.
388 */
389 __dec_zone_page_state(page, NR_FILE_PAGES);
390 __inc_zone_page_state(newpage, NR_FILE_PAGES);
Andrea Arcangeli99a15e22011-06-16 12:56:19 -0700391 if (!PageSwapCache(page) && PageSwapBacked(page)) {
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700392 __dec_zone_page_state(page, NR_SHMEM);
393 __inc_zone_page_state(newpage, NR_SHMEM);
394 }
Nick Piggin19fd6232008-07-25 19:45:32 -0700395 spin_unlock_irq(&mapping->tree_lock);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800396
Rafael Aquini78bd5202012-12-11 16:02:31 -0800397 return MIGRATEPAGE_SUCCESS;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800398}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800399
400/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900401 * The expected number of remaining references is the same as that
402 * of migrate_page_move_mapping().
403 */
404int migrate_huge_page_move_mapping(struct address_space *mapping,
405 struct page *newpage, struct page *page)
406{
407 int expected_count;
408 void **pslot;
409
410 if (!mapping) {
411 if (page_count(page) != 1)
412 return -EAGAIN;
Rafael Aquini78bd5202012-12-11 16:02:31 -0800413 return MIGRATEPAGE_SUCCESS;
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900414 }
415
416 spin_lock_irq(&mapping->tree_lock);
417
418 pslot = radix_tree_lookup_slot(&mapping->page_tree,
419 page_index(page));
420
421 expected_count = 2 + page_has_private(page);
422 if (page_count(page) != expected_count ||
Mel Gorman29c1f672011-01-13 15:47:21 -0800423 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900424 spin_unlock_irq(&mapping->tree_lock);
425 return -EAGAIN;
426 }
427
428 if (!page_freeze_refs(page, expected_count)) {
429 spin_unlock_irq(&mapping->tree_lock);
430 return -EAGAIN;
431 }
432
433 get_page(newpage);
434
435 radix_tree_replace_slot(pslot, newpage);
436
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800437 page_unfreeze_refs(page, expected_count - 1);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900438
439 spin_unlock_irq(&mapping->tree_lock);
Rafael Aquini78bd5202012-12-11 16:02:31 -0800440 return MIGRATEPAGE_SUCCESS;
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900441}
442
443/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800444 * Copy the page to its new location
445 */
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900446void migrate_page_copy(struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800447{
Rik van Riel7851a452013-10-07 11:29:23 +0100448 int cpupid;
449
Mel Gormanb32967f2012-11-19 12:35:47 +0000450 if (PageHuge(page) || PageTransHuge(page))
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900451 copy_huge_page(newpage, page);
452 else
453 copy_highpage(newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800454
455 if (PageError(page))
456 SetPageError(newpage);
457 if (PageReferenced(page))
458 SetPageReferenced(newpage);
459 if (PageUptodate(page))
460 SetPageUptodate(newpage);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700461 if (TestClearPageActive(page)) {
462 VM_BUG_ON(PageUnevictable(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800463 SetPageActive(newpage);
Lee Schermerhorn418b27e2009-12-14 17:59:54 -0800464 } else if (TestClearPageUnevictable(page))
465 SetPageUnevictable(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800466 if (PageChecked(page))
467 SetPageChecked(newpage);
468 if (PageMappedToDisk(page))
469 SetPageMappedToDisk(newpage);
470
471 if (PageDirty(page)) {
472 clear_page_dirty_for_io(page);
Nick Piggin3a902c52008-04-30 00:55:16 -0700473 /*
474 * Want to mark the page and the radix tree as dirty, and
475 * redo the accounting that clear_page_dirty_for_io undid,
476 * but we can't use set_page_dirty because that function
477 * is actually a signal that all of the page has become dirty.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300478 * Whereas only part of our page may be dirty.
Nick Piggin3a902c52008-04-30 00:55:16 -0700479 */
Hugh Dickins752dc182012-06-02 00:27:47 -0700480 if (PageSwapBacked(page))
481 SetPageDirty(newpage);
482 else
483 __set_page_dirty_nobuffers(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800484 }
485
Rik van Riel7851a452013-10-07 11:29:23 +0100486 /*
487 * Copy NUMA information to the new page, to prevent over-eager
488 * future migrations of this same page.
489 */
490 cpupid = page_cpupid_xchg_last(page, -1);
491 page_cpupid_xchg_last(newpage, cpupid);
492
Nick Pigginb291f002008-10-18 20:26:44 -0700493 mlock_migrate_page(newpage, page);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800494 ksm_migrate_page(newpage, page);
Hugh Dickinsc8d65532013-02-22 16:35:10 -0800495 /*
496 * Please do not reorder this without considering how mm/ksm.c's
497 * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache().
498 */
Christoph Lameterb20a3502006-03-22 00:09:12 -0800499 ClearPageSwapCache(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800500 ClearPagePrivate(page);
501 set_page_private(page, 0);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800502
503 /*
504 * If any waiters have accumulated on the new page then
505 * wake them up.
506 */
507 if (PageWriteback(newpage))
508 end_page_writeback(newpage);
509}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800510
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700511/************************************************************
512 * Migration functions
513 ***********************************************************/
514
515/* Always fail migration. Used for mappings that are not movable */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700516int fail_migrate_page(struct address_space *mapping,
517 struct page *newpage, struct page *page)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700518{
519 return -EIO;
520}
521EXPORT_SYMBOL(fail_migrate_page);
522
Christoph Lameterb20a3502006-03-22 00:09:12 -0800523/*
524 * Common logic to directly migrate a single page suitable for
David Howells266cf652009-04-03 16:42:36 +0100525 * pages that do not use PagePrivate/PagePrivate2.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800526 *
527 * Pages are locked upon entry and exit.
528 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700529int migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800530 struct page *newpage, struct page *page,
531 enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800532{
533 int rc;
534
535 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
536
Mel Gormana6bc32b2012-01-12 17:19:43 -0800537 rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800538
Rafael Aquini78bd5202012-12-11 16:02:31 -0800539 if (rc != MIGRATEPAGE_SUCCESS)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800540 return rc;
541
542 migrate_page_copy(newpage, page);
Rafael Aquini78bd5202012-12-11 16:02:31 -0800543 return MIGRATEPAGE_SUCCESS;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800544}
545EXPORT_SYMBOL(migrate_page);
546
David Howells93614012006-09-30 20:45:40 +0200547#ifdef CONFIG_BLOCK
Christoph Lameterb20a3502006-03-22 00:09:12 -0800548/*
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700549 * Migration function for pages with buffers. This function can only be used
550 * if the underlying filesystem guarantees that no other references to "page"
551 * exist.
552 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700553int buffer_migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800554 struct page *newpage, struct page *page, enum migrate_mode mode)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700555{
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700556 struct buffer_head *bh, *head;
557 int rc;
558
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700559 if (!page_has_buffers(page))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800560 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700561
562 head = page_buffers(page);
563
Mel Gormana6bc32b2012-01-12 17:19:43 -0800564 rc = migrate_page_move_mapping(mapping, newpage, page, head, mode);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700565
Rafael Aquini78bd5202012-12-11 16:02:31 -0800566 if (rc != MIGRATEPAGE_SUCCESS)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700567 return rc;
568
Mel Gormanb969c4a2012-01-12 17:19:34 -0800569 /*
570 * In the async case, migrate_page_move_mapping locked the buffers
571 * with an IRQ-safe spinlock held. In the sync case, the buffers
572 * need to be locked now
573 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800574 if (mode != MIGRATE_ASYNC)
575 BUG_ON(!buffer_migrate_lock_buffers(head, mode));
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700576
577 ClearPagePrivate(page);
578 set_page_private(newpage, page_private(page));
579 set_page_private(page, 0);
580 put_page(page);
581 get_page(newpage);
582
583 bh = head;
584 do {
585 set_bh_page(bh, newpage, bh_offset(bh));
586 bh = bh->b_this_page;
587
588 } while (bh != head);
589
590 SetPagePrivate(newpage);
591
592 migrate_page_copy(newpage, page);
593
594 bh = head;
595 do {
596 unlock_buffer(bh);
597 put_bh(bh);
598 bh = bh->b_this_page;
599
600 } while (bh != head);
601
Rafael Aquini78bd5202012-12-11 16:02:31 -0800602 return MIGRATEPAGE_SUCCESS;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700603}
604EXPORT_SYMBOL(buffer_migrate_page);
David Howells93614012006-09-30 20:45:40 +0200605#endif
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700606
Christoph Lameter04e62a22006-06-23 02:03:38 -0700607/*
608 * Writeback a page to clean the dirty state
609 */
610static int writeout(struct address_space *mapping, struct page *page)
611{
612 struct writeback_control wbc = {
613 .sync_mode = WB_SYNC_NONE,
614 .nr_to_write = 1,
615 .range_start = 0,
616 .range_end = LLONG_MAX,
Christoph Lameter04e62a22006-06-23 02:03:38 -0700617 .for_reclaim = 1
618 };
619 int rc;
620
621 if (!mapping->a_ops->writepage)
622 /* No write method for the address space */
623 return -EINVAL;
624
625 if (!clear_page_dirty_for_io(page))
626 /* Someone else already triggered a write */
627 return -EAGAIN;
628
629 /*
630 * A dirty page may imply that the underlying filesystem has
631 * the page on some queue. So the page must be clean for
632 * migration. Writeout may mean we loose the lock and the
633 * page state is no longer what we checked for earlier.
634 * At this point we know that the migration attempt cannot
635 * be successful.
636 */
637 remove_migration_ptes(page, page);
638
639 rc = mapping->a_ops->writepage(page, &wbc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700640
641 if (rc != AOP_WRITEPAGE_ACTIVATE)
642 /* unlocked. Relock */
643 lock_page(page);
644
Hugh Dickinsbda85502008-11-19 15:36:36 -0800645 return (rc < 0) ? -EIO : -EAGAIN;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700646}
647
648/*
649 * Default handling if a filesystem does not provide a migration function.
650 */
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700651static int fallback_migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800652 struct page *newpage, struct page *page, enum migrate_mode mode)
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700653{
Mel Gormanb969c4a2012-01-12 17:19:34 -0800654 if (PageDirty(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800655 /* Only writeback pages in full synchronous migration */
656 if (mode != MIGRATE_SYNC)
Mel Gormanb969c4a2012-01-12 17:19:34 -0800657 return -EBUSY;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700658 return writeout(mapping, page);
Mel Gormanb969c4a2012-01-12 17:19:34 -0800659 }
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700660
661 /*
662 * Buffers may be managed in a filesystem specific way.
663 * We must have no buffers or drop them.
664 */
David Howells266cf652009-04-03 16:42:36 +0100665 if (page_has_private(page) &&
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700666 !try_to_release_page(page, GFP_KERNEL))
667 return -EAGAIN;
668
Mel Gormana6bc32b2012-01-12 17:19:43 -0800669 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700670}
671
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700672/*
Christoph Lametere24f0b82006-06-23 02:03:51 -0700673 * Move a page to a newly allocated page
674 * The page is locked and all ptes have been successfully removed.
675 *
676 * The new page will have replaced the old page if this function
677 * is successful.
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700678 *
679 * Return value:
680 * < 0 - error code
Rafael Aquini78bd5202012-12-11 16:02:31 -0800681 * MIGRATEPAGE_SUCCESS - success
Christoph Lametere24f0b82006-06-23 02:03:51 -0700682 */
Mel Gorman3fe20112010-05-24 14:32:20 -0700683static int move_to_new_page(struct page *newpage, struct page *page,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800684 int remap_swapcache, enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700685{
686 struct address_space *mapping;
687 int rc;
688
689 /*
690 * Block others from accessing the page when we get around to
691 * establishing additional references. We are the only one
692 * holding a reference to the new page at this point.
693 */
Nick Piggin529ae9a2008-08-02 12:01:03 +0200694 if (!trylock_page(newpage))
Christoph Lametere24f0b82006-06-23 02:03:51 -0700695 BUG();
696
697 /* Prepare mapping for the new page.*/
698 newpage->index = page->index;
699 newpage->mapping = page->mapping;
Rik van Rielb2e18532008-10-18 20:26:30 -0700700 if (PageSwapBacked(page))
701 SetPageSwapBacked(newpage);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700702
703 mapping = page_mapping(page);
704 if (!mapping)
Mel Gormana6bc32b2012-01-12 17:19:43 -0800705 rc = migrate_page(mapping, newpage, page, mode);
Mel Gormanb969c4a2012-01-12 17:19:34 -0800706 else if (mapping->a_ops->migratepage)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700707 /*
Mel Gormanb969c4a2012-01-12 17:19:34 -0800708 * Most pages have a mapping and most filesystems provide a
709 * migratepage callback. Anonymous pages are part of swap
710 * space which also has its own migratepage callback. This
711 * is the most common path for page migration.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700712 */
Mel Gormanb969c4a2012-01-12 17:19:34 -0800713 rc = mapping->a_ops->migratepage(mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800714 newpage, page, mode);
Mel Gormanb969c4a2012-01-12 17:19:34 -0800715 else
Mel Gormana6bc32b2012-01-12 17:19:43 -0800716 rc = fallback_migrate_page(mapping, newpage, page, mode);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700717
Rafael Aquini78bd5202012-12-11 16:02:31 -0800718 if (rc != MIGRATEPAGE_SUCCESS) {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700719 newpage->mapping = NULL;
Mel Gorman3fe20112010-05-24 14:32:20 -0700720 } else {
721 if (remap_swapcache)
722 remove_migration_ptes(page, newpage);
Konstantin Khlebnikov35512ec2012-02-03 15:37:13 -0800723 page->mapping = NULL;
Mel Gorman3fe20112010-05-24 14:32:20 -0700724 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700725
726 unlock_page(newpage);
727
728 return rc;
729}
730
Minchan Kim0dabec92011-10-31 17:06:57 -0700731static int __unmap_and_move(struct page *page, struct page *newpage,
Hugh Dickins9c620e22013-02-22 16:35:14 -0800732 int force, enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700733{
Minchan Kim0dabec92011-10-31 17:06:57 -0700734 int rc = -EAGAIN;
Mel Gorman3fe20112010-05-24 14:32:20 -0700735 int remap_swapcache = 1;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -0700736 struct mem_cgroup *mem;
Mel Gorman3f6c8272010-05-24 14:32:17 -0700737 struct anon_vma *anon_vma = NULL;
Christoph Lameter95a402c2006-06-23 02:03:53 -0700738
Nick Piggin529ae9a2008-08-02 12:01:03 +0200739 if (!trylock_page(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800740 if (!force || mode == MIGRATE_ASYNC)
Minchan Kim0dabec92011-10-31 17:06:57 -0700741 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800742
743 /*
744 * It's not safe for direct compaction to call lock_page.
745 * For example, during page readahead pages are added locked
746 * to the LRU. Later, when the IO completes the pages are
747 * marked uptodate and unlocked. However, the queueing
748 * could be merging multiple pages for one bio (e.g.
749 * mpage_readpages). If an allocation happens for the
750 * second or third page, the process can end up locking
751 * the same page twice and deadlocking. Rather than
752 * trying to be clever about what pages can be locked,
753 * avoid the use of lock_page for direct compaction
754 * altogether.
755 */
756 if (current->flags & PF_MEMALLOC)
Minchan Kim0dabec92011-10-31 17:06:57 -0700757 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800758
Christoph Lametere24f0b82006-06-23 02:03:51 -0700759 lock_page(page);
760 }
761
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800762 /* charge against new page */
Johannes Weiner0030f532012-07-31 16:45:25 -0700763 mem_cgroup_prepare_migration(page, newpage, &mem);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800764
Christoph Lametere24f0b82006-06-23 02:03:51 -0700765 if (PageWriteback(page)) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700766 /*
Jianguo Wufed5b642013-04-29 15:07:58 -0700767 * Only in the case of a full synchronous migration is it
Mel Gormana6bc32b2012-01-12 17:19:43 -0800768 * necessary to wait for PageWriteback. In the async case,
769 * the retry loop is too short and in the sync-light case,
770 * the overhead of stalling is too much
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700771 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800772 if (mode != MIGRATE_SYNC) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700773 rc = -EBUSY;
774 goto uncharge;
775 }
776 if (!force)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800777 goto uncharge;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700778 wait_on_page_writeback(page);
779 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700780 /*
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700781 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
782 * we cannot notice that anon_vma is freed while we migrates a page.
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800783 * This get_anon_vma() delays freeing anon_vma pointer until the end
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700784 * of migration. File cache pages are no problem because of page_lock()
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700785 * File Caches may use write_page() or lock_page() in migration, then,
786 * just care Anon page here.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700787 */
Hugh Dickinsb79bc0a2013-02-22 16:35:13 -0800788 if (PageAnon(page) && !PageKsm(page)) {
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800789 /*
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +0000790 * Only page_lock_anon_vma_read() understands the subtleties of
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800791 * getting a hold on an anon_vma from outside one of its mms.
792 */
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700793 anon_vma = page_get_anon_vma(page);
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800794 if (anon_vma) {
795 /*
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700796 * Anon page
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800797 */
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800798 } else if (PageSwapCache(page)) {
Mel Gorman3fe20112010-05-24 14:32:20 -0700799 /*
800 * We cannot be sure that the anon_vma of an unmapped
801 * swapcache page is safe to use because we don't
802 * know in advance if the VMA that this page belonged
803 * to still exists. If the VMA and others sharing the
804 * data have been freed, then the anon_vma could
805 * already be invalid.
806 *
807 * To avoid this possibility, swapcache pages get
808 * migrated but are not remapped when migration
809 * completes
810 */
811 remap_swapcache = 0;
812 } else {
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800813 goto uncharge;
Mel Gorman3fe20112010-05-24 14:32:20 -0700814 }
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700815 }
Shaohua Li62e1c552008-02-04 22:29:33 -0800816
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800817 if (unlikely(balloon_page_movable(page))) {
818 /*
819 * A ballooned page does not need any special attention from
820 * physical to virtual reverse mapping procedures.
821 * Skip any attempt to unmap PTEs or to remap swap cache,
822 * in order to avoid burning cycles at rmap level, and perform
823 * the page migration right away (proteced by page lock).
824 */
825 rc = balloon_page_migrate(newpage, page, mode);
826 goto uncharge;
827 }
828
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700829 /*
Shaohua Li62e1c552008-02-04 22:29:33 -0800830 * Corner case handling:
831 * 1. When a new swap-cache page is read into, it is added to the LRU
832 * and treated as swapcache but it has no rmap yet.
833 * Calling try_to_unmap() against a page->mapping==NULL page will
834 * trigger a BUG. So handle it here.
835 * 2. An orphaned page (see truncate_complete_page) might have
836 * fs-private metadata. The page can be picked up due to memory
837 * offlining. Everywhere else except page reclaim, the page is
838 * invisible to the vm, so the page can not be migrated. So try to
839 * free the metadata, so the page can be freed.
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700840 */
Shaohua Li62e1c552008-02-04 22:29:33 -0800841 if (!page->mapping) {
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800842 VM_BUG_ON(PageAnon(page));
843 if (page_has_private(page)) {
Shaohua Li62e1c552008-02-04 22:29:33 -0800844 try_to_free_buffers(page);
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800845 goto uncharge;
Shaohua Li62e1c552008-02-04 22:29:33 -0800846 }
Shaohua Liabfc3482009-09-21 17:01:19 -0700847 goto skip_unmap;
Shaohua Li62e1c552008-02-04 22:29:33 -0800848 }
849
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700850 /* Establish migration ptes or remove ptes */
Andi Kleen14fa31b2009-09-16 11:50:10 +0200851 try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700852
Shaohua Liabfc3482009-09-21 17:01:19 -0700853skip_unmap:
Christoph Lametere6a15302006-06-25 05:46:49 -0700854 if (!page_mapped(page))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800855 rc = move_to_new_page(newpage, page, remap_swapcache, mode);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700856
Mel Gorman3fe20112010-05-24 14:32:20 -0700857 if (rc && remap_swapcache)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700858 remove_migration_ptes(page, page);
Mel Gorman3f6c8272010-05-24 14:32:17 -0700859
860 /* Drop an anon_vma reference if we took one */
Rik van Riel76545062010-08-09 17:18:41 -0700861 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -0700862 put_anon_vma(anon_vma);
Mel Gorman3f6c8272010-05-24 14:32:17 -0700863
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800864uncharge:
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800865 mem_cgroup_end_migration(mem, page, newpage,
866 (rc == MIGRATEPAGE_SUCCESS ||
867 rc == MIGRATEPAGE_BALLOON_SUCCESS));
Christoph Lametere24f0b82006-06-23 02:03:51 -0700868 unlock_page(page);
Minchan Kim0dabec92011-10-31 17:06:57 -0700869out:
870 return rc;
871}
Christoph Lameter95a402c2006-06-23 02:03:53 -0700872
Minchan Kim0dabec92011-10-31 17:06:57 -0700873/*
874 * Obtain the lock on page, remove all ptes and migrate the page
875 * to the newly allocated page in newpage.
876 */
877static int unmap_and_move(new_page_t get_new_page, unsigned long private,
Hugh Dickins9c620e22013-02-22 16:35:14 -0800878 struct page *page, int force, enum migrate_mode mode)
Minchan Kim0dabec92011-10-31 17:06:57 -0700879{
880 int rc = 0;
881 int *result = NULL;
882 struct page *newpage = get_new_page(page, private, &result);
883
884 if (!newpage)
885 return -ENOMEM;
886
887 if (page_count(page) == 1) {
888 /* page was freed from under us. So we are done. */
889 goto out;
890 }
891
892 if (unlikely(PageTransHuge(page)))
893 if (unlikely(split_huge_page(page)))
894 goto out;
895
Hugh Dickins9c620e22013-02-22 16:35:14 -0800896 rc = __unmap_and_move(page, newpage, force, mode);
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800897
898 if (unlikely(rc == MIGRATEPAGE_BALLOON_SUCCESS)) {
899 /*
900 * A ballooned page has been migrated already.
901 * Now, it's the time to wrap-up counters,
902 * handle the page back to Buddy and return.
903 */
904 dec_zone_page_state(page, NR_ISOLATED_ANON +
905 page_is_file_cache(page));
906 balloon_page_free(page);
907 return MIGRATEPAGE_SUCCESS;
908 }
Minchan Kim0dabec92011-10-31 17:06:57 -0700909out:
Christoph Lametere24f0b82006-06-23 02:03:51 -0700910 if (rc != -EAGAIN) {
Minchan Kim0dabec92011-10-31 17:06:57 -0700911 /*
912 * A page that has been migrated has all references
913 * removed and will be freed. A page that has not been
914 * migrated will have kepts its references and be
915 * restored.
916 */
917 list_del(&page->lru);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -0700918 dec_zone_page_state(page, NR_ISOLATED_ANON +
Johannes Weiner6c0b1352009-09-21 17:02:59 -0700919 page_is_file_cache(page));
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700920 putback_lru_page(page);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700921 }
Christoph Lameter95a402c2006-06-23 02:03:53 -0700922 /*
923 * Move the new page to the LRU. If migration was not successful
924 * then this will free the page.
925 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700926 putback_lru_page(newpage);
Christoph Lameter742755a2006-06-23 02:03:55 -0700927 if (result) {
928 if (rc)
929 *result = rc;
930 else
931 *result = page_to_nid(newpage);
932 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700933 return rc;
934}
935
936/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900937 * Counterpart of unmap_and_move_page() for hugepage migration.
938 *
939 * This function doesn't wait the completion of hugepage I/O
940 * because there is no race between I/O and migration for hugepage.
941 * Note that currently hugepage I/O occurs only in direct I/O
942 * where no lock is held and PG_writeback is irrelevant,
943 * and writeback status of all subpages are counted in the reference
944 * count of the head page (i.e. if all subpages of a 2MB hugepage are
945 * under direct I/O, the reference of the head page is 512 and a bit more.)
946 * This means that when we try to migrate hugepage whose subpages are
947 * doing direct I/O, some references remain after try_to_unmap() and
948 * hugepage migration fails without data corruption.
949 *
950 * There is also no race when direct I/O is issued on the page under migration,
951 * because then pte is replaced with migration swap entry and direct I/O code
952 * will wait in the page fault for migration to complete.
953 */
954static int unmap_and_move_huge_page(new_page_t get_new_page,
955 unsigned long private, struct page *hpage,
Hugh Dickins9c620e22013-02-22 16:35:14 -0800956 int force, enum migrate_mode mode)
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900957{
958 int rc = 0;
959 int *result = NULL;
960 struct page *new_hpage = get_new_page(hpage, private, &result);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900961 struct anon_vma *anon_vma = NULL;
962
Naoya Horiguchi83467ef2013-09-11 14:22:11 -0700963 /*
964 * Movability of hugepages depends on architectures and hugepage size.
965 * This check is necessary because some callers of hugepage migration
966 * like soft offline and memory hotremove don't walk through page
967 * tables or check whether the hugepage is pmd-based or not before
968 * kicking migration.
969 */
970 if (!hugepage_migration_support(page_hstate(hpage)))
971 return -ENOSYS;
972
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900973 if (!new_hpage)
974 return -ENOMEM;
975
976 rc = -EAGAIN;
977
978 if (!trylock_page(hpage)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800979 if (!force || mode != MIGRATE_SYNC)
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900980 goto out;
981 lock_page(hpage);
982 }
983
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700984 if (PageAnon(hpage))
985 anon_vma = page_get_anon_vma(hpage);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900986
987 try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
988
989 if (!page_mapped(hpage))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800990 rc = move_to_new_page(new_hpage, hpage, 1, mode);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900991
992 if (rc)
993 remove_migration_ptes(hpage, hpage);
994
Hugh Dickinsfd4a4662011-01-13 15:47:31 -0800995 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -0700996 put_anon_vma(anon_vma);
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -0700997
998 if (!rc)
999 hugetlb_cgroup_migrate(hpage, new_hpage);
1000
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001001 unlock_page(hpage);
Hillf Danton09761332011-12-08 14:34:20 -08001002out:
Naoya Horiguchib8ec1ce2013-09-11 14:22:01 -07001003 if (rc != -EAGAIN)
1004 putback_active_hugepage(hpage);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001005 put_page(new_hpage);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001006 if (result) {
1007 if (rc)
1008 *result = rc;
1009 else
1010 *result = page_to_nid(new_hpage);
1011 }
1012 return rc;
1013}
1014
1015/*
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001016 * migrate_pages - migrate the pages specified in a list, to the free pages
1017 * supplied as the target for the page migration
Christoph Lameterb20a3502006-03-22 00:09:12 -08001018 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001019 * @from: The list of pages to be migrated.
1020 * @get_new_page: The function used to allocate free pages to be used
1021 * as the target of the page migration.
1022 * @private: Private data to be passed on to get_new_page()
1023 * @mode: The migration mode that specifies the constraints for
1024 * page migration, if any.
1025 * @reason: The reason for page migration.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001026 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001027 * The function returns after 10 attempts or if no pages are movable any more
1028 * because the list has become empty or no retryable pages exist any more.
1029 * The caller should call putback_lru_pages() to return pages to the LRU
Minchan Kim28bd6572011-01-25 15:07:26 -08001030 * or free list only if ret != 0.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001031 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001032 * Returns the number of pages that were not migrated, or an error code.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001033 */
Hugh Dickins9c620e22013-02-22 16:35:14 -08001034int migrate_pages(struct list_head *from, new_page_t get_new_page,
1035 unsigned long private, enum migrate_mode mode, int reason)
Christoph Lameterb20a3502006-03-22 00:09:12 -08001036{
Christoph Lametere24f0b82006-06-23 02:03:51 -07001037 int retry = 1;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001038 int nr_failed = 0;
Mel Gorman5647bc22012-10-19 10:46:20 +01001039 int nr_succeeded = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001040 int pass = 0;
1041 struct page *page;
1042 struct page *page2;
1043 int swapwrite = current->flags & PF_SWAPWRITE;
1044 int rc;
1045
1046 if (!swapwrite)
1047 current->flags |= PF_SWAPWRITE;
1048
Christoph Lametere24f0b82006-06-23 02:03:51 -07001049 for(pass = 0; pass < 10 && retry; pass++) {
1050 retry = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001051
Christoph Lametere24f0b82006-06-23 02:03:51 -07001052 list_for_each_entry_safe(page, page2, from, lru) {
Christoph Lametere24f0b82006-06-23 02:03:51 -07001053 cond_resched();
Christoph Lameterb20a3502006-03-22 00:09:12 -08001054
Naoya Horiguchi31caf662013-09-11 14:21:59 -07001055 if (PageHuge(page))
1056 rc = unmap_and_move_huge_page(get_new_page,
1057 private, page, pass > 2, mode);
1058 else
1059 rc = unmap_and_move(get_new_page, private,
Hugh Dickins9c620e22013-02-22 16:35:14 -08001060 page, pass > 2, mode);
Christoph Lameterb20a3502006-03-22 00:09:12 -08001061
Christoph Lametere24f0b82006-06-23 02:03:51 -07001062 switch(rc) {
Christoph Lameter95a402c2006-06-23 02:03:53 -07001063 case -ENOMEM:
1064 goto out;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001065 case -EAGAIN:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001066 retry++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001067 break;
Rafael Aquini78bd5202012-12-11 16:02:31 -08001068 case MIGRATEPAGE_SUCCESS:
Mel Gorman5647bc22012-10-19 10:46:20 +01001069 nr_succeeded++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001070 break;
1071 default:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001072 /* Permanent failure */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001073 nr_failed++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001074 break;
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001075 }
Christoph Lameterb20a3502006-03-22 00:09:12 -08001076 }
1077 }
Rafael Aquini78bd5202012-12-11 16:02:31 -08001078 rc = nr_failed + retry;
Christoph Lameter95a402c2006-06-23 02:03:53 -07001079out:
Mel Gorman5647bc22012-10-19 10:46:20 +01001080 if (nr_succeeded)
1081 count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
1082 if (nr_failed)
1083 count_vm_events(PGMIGRATE_FAIL, nr_failed);
Mel Gorman7b2a2d42012-10-19 14:07:31 +01001084 trace_mm_migrate_pages(nr_succeeded, nr_failed, mode, reason);
1085
Christoph Lameterb20a3502006-03-22 00:09:12 -08001086 if (!swapwrite)
1087 current->flags &= ~PF_SWAPWRITE;
1088
Rafael Aquini78bd5202012-12-11 16:02:31 -08001089 return rc;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001090}
1091
Christoph Lameter742755a2006-06-23 02:03:55 -07001092#ifdef CONFIG_NUMA
1093/*
1094 * Move a list of individual pages
1095 */
1096struct page_to_node {
1097 unsigned long addr;
1098 struct page *page;
1099 int node;
1100 int status;
1101};
1102
1103static struct page *new_page_node(struct page *p, unsigned long private,
1104 int **result)
1105{
1106 struct page_to_node *pm = (struct page_to_node *)private;
1107
1108 while (pm->node != MAX_NUMNODES && pm->page != p)
1109 pm++;
1110
1111 if (pm->node == MAX_NUMNODES)
1112 return NULL;
1113
1114 *result = &pm->status;
1115
Naoya Horiguchie632a932013-09-11 14:22:04 -07001116 if (PageHuge(p))
1117 return alloc_huge_page_node(page_hstate(compound_head(p)),
1118 pm->node);
1119 else
1120 return alloc_pages_exact_node(pm->node,
Mel Gorman769848c2007-07-17 04:03:05 -07001121 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
Christoph Lameter742755a2006-06-23 02:03:55 -07001122}
1123
1124/*
1125 * Move a set of pages as indicated in the pm array. The addr
1126 * field must be set to the virtual address of the page to be moved
1127 * and the node number must contain a valid target node.
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001128 * The pm array ends with node = MAX_NUMNODES.
Christoph Lameter742755a2006-06-23 02:03:55 -07001129 */
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001130static int do_move_page_to_node_array(struct mm_struct *mm,
1131 struct page_to_node *pm,
1132 int migrate_all)
Christoph Lameter742755a2006-06-23 02:03:55 -07001133{
1134 int err;
1135 struct page_to_node *pp;
1136 LIST_HEAD(pagelist);
1137
1138 down_read(&mm->mmap_sem);
1139
1140 /*
1141 * Build a list of pages to migrate
1142 */
Christoph Lameter742755a2006-06-23 02:03:55 -07001143 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
1144 struct vm_area_struct *vma;
1145 struct page *page;
1146
Christoph Lameter742755a2006-06-23 02:03:55 -07001147 err = -EFAULT;
1148 vma = find_vma(mm, pp->addr);
Gleb Natapov70384dc2010-10-26 14:22:07 -07001149 if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
Christoph Lameter742755a2006-06-23 02:03:55 -07001150 goto set_status;
1151
Andrea Arcangeli500d65d2011-01-13 15:46:55 -08001152 page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001153
1154 err = PTR_ERR(page);
1155 if (IS_ERR(page))
1156 goto set_status;
1157
Christoph Lameter742755a2006-06-23 02:03:55 -07001158 err = -ENOENT;
1159 if (!page)
1160 goto set_status;
1161
Hugh Dickins62b61f62009-12-14 17:59:33 -08001162 /* Use PageReserved to check for zero page */
Hugh Dickinsb79bc0a2013-02-22 16:35:13 -08001163 if (PageReserved(page))
Christoph Lameter742755a2006-06-23 02:03:55 -07001164 goto put_and_set;
1165
1166 pp->page = page;
1167 err = page_to_nid(page);
1168
1169 if (err == pp->node)
1170 /*
1171 * Node already in the right place
1172 */
1173 goto put_and_set;
1174
1175 err = -EACCES;
1176 if (page_mapcount(page) > 1 &&
1177 !migrate_all)
1178 goto put_and_set;
1179
Naoya Horiguchie632a932013-09-11 14:22:04 -07001180 if (PageHuge(page)) {
1181 isolate_huge_page(page, &pagelist);
1182 goto put_and_set;
1183 }
1184
Nick Piggin62695a82008-10-18 20:26:09 -07001185 err = isolate_lru_page(page);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001186 if (!err) {
Nick Piggin62695a82008-10-18 20:26:09 -07001187 list_add_tail(&page->lru, &pagelist);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001188 inc_zone_page_state(page, NR_ISOLATED_ANON +
1189 page_is_file_cache(page));
1190 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001191put_and_set:
1192 /*
1193 * Either remove the duplicate refcount from
1194 * isolate_lru_page() or drop the page ref if it was
1195 * not isolated.
1196 */
1197 put_page(page);
1198set_status:
1199 pp->status = err;
1200 }
1201
Brice Gogline78bbfa2008-10-18 20:27:15 -07001202 err = 0;
Minchan Kimcf608ac2010-10-26 14:21:29 -07001203 if (!list_empty(&pagelist)) {
Christoph Lameter742755a2006-06-23 02:03:55 -07001204 err = migrate_pages(&pagelist, new_page_node,
Hugh Dickins9c620e22013-02-22 16:35:14 -08001205 (unsigned long)pm, MIGRATE_SYNC, MR_SYSCALL);
Minchan Kimcf608ac2010-10-26 14:21:29 -07001206 if (err)
Naoya Horiguchie632a932013-09-11 14:22:04 -07001207 putback_movable_pages(&pagelist);
Minchan Kimcf608ac2010-10-26 14:21:29 -07001208 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001209
1210 up_read(&mm->mmap_sem);
1211 return err;
1212}
1213
1214/*
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001215 * Migrate an array of page address onto an array of nodes and fill
1216 * the corresponding array of status.
1217 */
Christoph Lameter3268c632012-03-21 16:34:06 -07001218static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001219 unsigned long nr_pages,
1220 const void __user * __user *pages,
1221 const int __user *nodes,
1222 int __user *status, int flags)
1223{
Brice Goglin3140a222009-01-06 14:38:57 -08001224 struct page_to_node *pm;
Brice Goglin3140a222009-01-06 14:38:57 -08001225 unsigned long chunk_nr_pages;
1226 unsigned long chunk_start;
1227 int err;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001228
Brice Goglin3140a222009-01-06 14:38:57 -08001229 err = -ENOMEM;
1230 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
1231 if (!pm)
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001232 goto out;
Brice Goglin35282a22009-06-16 15:32:43 -07001233
1234 migrate_prep();
1235
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001236 /*
Brice Goglin3140a222009-01-06 14:38:57 -08001237 * Store a chunk of page_to_node array in a page,
1238 * but keep the last one as a marker
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001239 */
Brice Goglin3140a222009-01-06 14:38:57 -08001240 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001241
Brice Goglin3140a222009-01-06 14:38:57 -08001242 for (chunk_start = 0;
1243 chunk_start < nr_pages;
1244 chunk_start += chunk_nr_pages) {
1245 int j;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001246
Brice Goglin3140a222009-01-06 14:38:57 -08001247 if (chunk_start + chunk_nr_pages > nr_pages)
1248 chunk_nr_pages = nr_pages - chunk_start;
1249
1250 /* fill the chunk pm with addrs and nodes from user-space */
1251 for (j = 0; j < chunk_nr_pages; j++) {
1252 const void __user *p;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001253 int node;
1254
Brice Goglin3140a222009-01-06 14:38:57 -08001255 err = -EFAULT;
1256 if (get_user(p, pages + j + chunk_start))
1257 goto out_pm;
1258 pm[j].addr = (unsigned long) p;
1259
1260 if (get_user(node, nodes + j + chunk_start))
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001261 goto out_pm;
1262
1263 err = -ENODEV;
Linus Torvalds6f5a55f2010-02-05 16:16:50 -08001264 if (node < 0 || node >= MAX_NUMNODES)
1265 goto out_pm;
1266
Lai Jiangshan389162c2012-12-12 13:51:30 -08001267 if (!node_state(node, N_MEMORY))
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001268 goto out_pm;
1269
1270 err = -EACCES;
1271 if (!node_isset(node, task_nodes))
1272 goto out_pm;
1273
Brice Goglin3140a222009-01-06 14:38:57 -08001274 pm[j].node = node;
1275 }
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001276
Brice Goglin3140a222009-01-06 14:38:57 -08001277 /* End marker for this chunk */
1278 pm[chunk_nr_pages].node = MAX_NUMNODES;
1279
1280 /* Migrate this chunk */
1281 err = do_move_page_to_node_array(mm, pm,
1282 flags & MPOL_MF_MOVE_ALL);
1283 if (err < 0)
1284 goto out_pm;
1285
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001286 /* Return status information */
Brice Goglin3140a222009-01-06 14:38:57 -08001287 for (j = 0; j < chunk_nr_pages; j++)
1288 if (put_user(pm[j].status, status + j + chunk_start)) {
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001289 err = -EFAULT;
Brice Goglin3140a222009-01-06 14:38:57 -08001290 goto out_pm;
1291 }
1292 }
1293 err = 0;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001294
1295out_pm:
Brice Goglin3140a222009-01-06 14:38:57 -08001296 free_page((unsigned long)pm);
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001297out:
1298 return err;
1299}
1300
1301/*
Brice Goglin2f007e72008-10-18 20:27:16 -07001302 * Determine the nodes of an array of pages and store it in an array of status.
Christoph Lameter742755a2006-06-23 02:03:55 -07001303 */
Brice Goglin80bba122008-12-09 13:14:23 -08001304static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1305 const void __user **pages, int *status)
Christoph Lameter742755a2006-06-23 02:03:55 -07001306{
Brice Goglin2f007e72008-10-18 20:27:16 -07001307 unsigned long i;
Brice Goglin2f007e72008-10-18 20:27:16 -07001308
Christoph Lameter742755a2006-06-23 02:03:55 -07001309 down_read(&mm->mmap_sem);
1310
Brice Goglin2f007e72008-10-18 20:27:16 -07001311 for (i = 0; i < nr_pages; i++) {
Brice Goglin80bba122008-12-09 13:14:23 -08001312 unsigned long addr = (unsigned long)(*pages);
Christoph Lameter742755a2006-06-23 02:03:55 -07001313 struct vm_area_struct *vma;
1314 struct page *page;
KOSAKI Motohiroc095adb2008-12-16 16:06:43 +09001315 int err = -EFAULT;
Brice Goglin2f007e72008-10-18 20:27:16 -07001316
1317 vma = find_vma(mm, addr);
Gleb Natapov70384dc2010-10-26 14:22:07 -07001318 if (!vma || addr < vma->vm_start)
Christoph Lameter742755a2006-06-23 02:03:55 -07001319 goto set_status;
1320
Brice Goglin2f007e72008-10-18 20:27:16 -07001321 page = follow_page(vma, addr, 0);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001322
1323 err = PTR_ERR(page);
1324 if (IS_ERR(page))
1325 goto set_status;
1326
Christoph Lameter742755a2006-06-23 02:03:55 -07001327 err = -ENOENT;
1328 /* Use PageReserved to check for zero page */
Hugh Dickinsb79bc0a2013-02-22 16:35:13 -08001329 if (!page || PageReserved(page))
Christoph Lameter742755a2006-06-23 02:03:55 -07001330 goto set_status;
1331
1332 err = page_to_nid(page);
1333set_status:
Brice Goglin80bba122008-12-09 13:14:23 -08001334 *status = err;
1335
1336 pages++;
1337 status++;
1338 }
1339
1340 up_read(&mm->mmap_sem);
1341}
1342
1343/*
1344 * Determine the nodes of a user array of pages and store it in
1345 * a user array of status.
1346 */
1347static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1348 const void __user * __user *pages,
1349 int __user *status)
1350{
1351#define DO_PAGES_STAT_CHUNK_NR 16
1352 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1353 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
Brice Goglin80bba122008-12-09 13:14:23 -08001354
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001355 while (nr_pages) {
1356 unsigned long chunk_nr;
Brice Goglin80bba122008-12-09 13:14:23 -08001357
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001358 chunk_nr = nr_pages;
1359 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1360 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1361
1362 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
1363 break;
Brice Goglin80bba122008-12-09 13:14:23 -08001364
1365 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1366
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001367 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1368 break;
Christoph Lameter742755a2006-06-23 02:03:55 -07001369
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001370 pages += chunk_nr;
1371 status += chunk_nr;
1372 nr_pages -= chunk_nr;
1373 }
1374 return nr_pages ? -EFAULT : 0;
Christoph Lameter742755a2006-06-23 02:03:55 -07001375}
1376
1377/*
1378 * Move a list of pages in the address space of the currently executing
1379 * process.
1380 */
Heiko Carstens938bb9f2009-01-14 14:14:30 +01001381SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1382 const void __user * __user *, pages,
1383 const int __user *, nodes,
1384 int __user *, status, int, flags)
Christoph Lameter742755a2006-06-23 02:03:55 -07001385{
David Howellsc69e8d92008-11-14 10:39:19 +11001386 const struct cred *cred = current_cred(), *tcred;
Christoph Lameter742755a2006-06-23 02:03:55 -07001387 struct task_struct *task;
Christoph Lameter742755a2006-06-23 02:03:55 -07001388 struct mm_struct *mm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001389 int err;
Christoph Lameter3268c632012-03-21 16:34:06 -07001390 nodemask_t task_nodes;
Christoph Lameter742755a2006-06-23 02:03:55 -07001391
1392 /* Check flags */
1393 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1394 return -EINVAL;
1395
1396 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1397 return -EPERM;
1398
1399 /* Find the mm_struct */
Greg Thelena879bf52011-02-25 14:44:13 -08001400 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07001401 task = pid ? find_task_by_vpid(pid) : current;
Christoph Lameter742755a2006-06-23 02:03:55 -07001402 if (!task) {
Greg Thelena879bf52011-02-25 14:44:13 -08001403 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001404 return -ESRCH;
1405 }
Christoph Lameter3268c632012-03-21 16:34:06 -07001406 get_task_struct(task);
Christoph Lameter742755a2006-06-23 02:03:55 -07001407
1408 /*
1409 * Check if this process has the right to modify the specified
1410 * process. The right exists if the process has administrative
1411 * capabilities, superuser privileges or the same
1412 * userid as the target process.
1413 */
David Howellsc69e8d92008-11-14 10:39:19 +11001414 tcred = __task_cred(task);
Eric W. Biedermanb38a86e2012-03-12 15:48:24 -07001415 if (!uid_eq(cred->euid, tcred->suid) && !uid_eq(cred->euid, tcred->uid) &&
1416 !uid_eq(cred->uid, tcred->suid) && !uid_eq(cred->uid, tcred->uid) &&
Christoph Lameter742755a2006-06-23 02:03:55 -07001417 !capable(CAP_SYS_NICE)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001418 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001419 err = -EPERM;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001420 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001421 }
David Howellsc69e8d92008-11-14 10:39:19 +11001422 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001423
David Quigley86c3a762006-06-23 02:04:02 -07001424 err = security_task_movememory(task);
1425 if (err)
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001426 goto out;
David Quigley86c3a762006-06-23 02:04:02 -07001427
Christoph Lameter3268c632012-03-21 16:34:06 -07001428 task_nodes = cpuset_mems_allowed(task);
1429 mm = get_task_mm(task);
1430 put_task_struct(task);
1431
Sasha Levin6e8b09e2012-04-25 16:01:53 -07001432 if (!mm)
1433 return -EINVAL;
1434
1435 if (nodes)
1436 err = do_pages_move(mm, task_nodes, nr_pages, pages,
1437 nodes, status, flags);
1438 else
1439 err = do_pages_stat(mm, nr_pages, pages, status);
Christoph Lameter3268c632012-03-21 16:34:06 -07001440
1441 mmput(mm);
1442 return err;
David Quigley86c3a762006-06-23 02:04:02 -07001443
Christoph Lameter742755a2006-06-23 02:03:55 -07001444out:
Christoph Lameter3268c632012-03-21 16:34:06 -07001445 put_task_struct(task);
Christoph Lameter742755a2006-06-23 02:03:55 -07001446 return err;
1447}
Christoph Lameter742755a2006-06-23 02:03:55 -07001448
Christoph Lameter7b2259b2006-06-25 05:46:48 -07001449/*
1450 * Call migration functions in the vma_ops that may prepare
1451 * memory in a vm for migration. migration functions may perform
1452 * the migration for vmas that do not have an underlying page struct.
1453 */
1454int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1455 const nodemask_t *from, unsigned long flags)
1456{
1457 struct vm_area_struct *vma;
1458 int err = 0;
1459
Daisuke Nishimura1001c9f2009-02-11 13:04:18 -08001460 for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
Christoph Lameter7b2259b2006-06-25 05:46:48 -07001461 if (vma->vm_ops && vma->vm_ops->migrate) {
1462 err = vma->vm_ops->migrate(vma, to, from, flags);
1463 if (err)
1464 break;
1465 }
1466 }
1467 return err;
1468}
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001469
1470#ifdef CONFIG_NUMA_BALANCING
1471/*
1472 * Returns true if this is a safe migration target node for misplaced NUMA
1473 * pages. Currently it only checks the watermarks which crude
1474 */
1475static bool migrate_balanced_pgdat(struct pglist_data *pgdat,
Mel Gorman3abef4e2013-02-22 16:34:27 -08001476 unsigned long nr_migrate_pages)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001477{
1478 int z;
1479 for (z = pgdat->nr_zones - 1; z >= 0; z--) {
1480 struct zone *zone = pgdat->node_zones + z;
1481
1482 if (!populated_zone(zone))
1483 continue;
1484
Lisa Du6e543d52013-09-11 14:22:36 -07001485 if (!zone_reclaimable(zone))
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001486 continue;
1487
1488 /* Avoid waking kswapd by allocating pages_to_migrate pages. */
1489 if (!zone_watermark_ok(zone, 0,
1490 high_wmark_pages(zone) +
1491 nr_migrate_pages,
1492 0, 0))
1493 continue;
1494 return true;
1495 }
1496 return false;
1497}
1498
1499static struct page *alloc_misplaced_dst_page(struct page *page,
1500 unsigned long data,
1501 int **result)
1502{
1503 int nid = (int) data;
1504 struct page *newpage;
1505
1506 newpage = alloc_pages_exact_node(nid,
1507 (GFP_HIGHUSER_MOVABLE | GFP_THISNODE |
1508 __GFP_NOMEMALLOC | __GFP_NORETRY |
1509 __GFP_NOWARN) &
1510 ~GFP_IOFS, 0);
Hillf Dantonbac03822012-11-27 14:46:24 +00001511 if (newpage)
Peter Zijlstra90572892013-10-07 11:29:20 +01001512 page_cpupid_xchg_last(newpage, page_cpupid_last(page));
Hillf Dantonbac03822012-11-27 14:46:24 +00001513
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001514 return newpage;
1515}
1516
1517/*
Mel Gormana8f60772012-11-14 21:41:46 +00001518 * page migration rate limiting control.
1519 * Do not migrate more than @pages_to_migrate in a @migrate_interval_millisecs
1520 * window of time. Default here says do not migrate more than 1280M per second.
Mel Gormane14808b2012-11-19 10:59:15 +00001521 * If a node is rate-limited then PTE NUMA updates are also rate-limited. However
1522 * as it is faults that reset the window, pte updates will happen unconditionally
1523 * if there has not been a fault since @pteupdate_interval_millisecs after the
1524 * throttle window closed.
Mel Gormana8f60772012-11-14 21:41:46 +00001525 */
1526static unsigned int migrate_interval_millisecs __read_mostly = 100;
Mel Gormane14808b2012-11-19 10:59:15 +00001527static unsigned int pteupdate_interval_millisecs __read_mostly = 1000;
Mel Gormana8f60772012-11-14 21:41:46 +00001528static unsigned int ratelimit_pages __read_mostly = 128 << (20 - PAGE_SHIFT);
1529
Mel Gormane14808b2012-11-19 10:59:15 +00001530/* Returns true if NUMA migration is currently rate limited */
1531bool migrate_ratelimited(int node)
1532{
1533 pg_data_t *pgdat = NODE_DATA(node);
1534
1535 if (time_after(jiffies, pgdat->numabalancing_migrate_next_window +
1536 msecs_to_jiffies(pteupdate_interval_millisecs)))
1537 return false;
1538
1539 if (pgdat->numabalancing_migrate_nr_pages < ratelimit_pages)
1540 return false;
1541
1542 return true;
1543}
1544
Mel Gormanb32967f2012-11-19 12:35:47 +00001545/* Returns true if the node is migrate rate-limited after the update */
Mel Gormand28d43352012-11-29 09:24:36 +00001546bool numamigrate_update_ratelimit(pg_data_t *pgdat, unsigned long nr_pages)
Mel Gormanb32967f2012-11-19 12:35:47 +00001547{
1548 bool rate_limited = false;
1549
1550 /*
1551 * Rate-limit the amount of data that is being migrated to a node.
1552 * Optimal placement is no good if the memory bus is saturated and
1553 * all the time is being spent migrating!
1554 */
1555 spin_lock(&pgdat->numabalancing_migrate_lock);
1556 if (time_after(jiffies, pgdat->numabalancing_migrate_next_window)) {
1557 pgdat->numabalancing_migrate_nr_pages = 0;
1558 pgdat->numabalancing_migrate_next_window = jiffies +
1559 msecs_to_jiffies(migrate_interval_millisecs);
1560 }
1561 if (pgdat->numabalancing_migrate_nr_pages > ratelimit_pages)
1562 rate_limited = true;
1563 else
Mel Gormand28d43352012-11-29 09:24:36 +00001564 pgdat->numabalancing_migrate_nr_pages += nr_pages;
Mel Gormanb32967f2012-11-19 12:35:47 +00001565 spin_unlock(&pgdat->numabalancing_migrate_lock);
1566
1567 return rate_limited;
1568}
1569
1570int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
1571{
Hugh Dickins340ef392013-02-22 16:34:33 -08001572 int page_lru;
Mel Gormanb32967f2012-11-19 12:35:47 +00001573
Mel Gorman3abef4e2013-02-22 16:34:27 -08001574 VM_BUG_ON(compound_order(page) && !PageTransHuge(page));
1575
Mel Gormanb32967f2012-11-19 12:35:47 +00001576 /* Avoid migrating to a node that is nearly full */
Hugh Dickins340ef392013-02-22 16:34:33 -08001577 if (!migrate_balanced_pgdat(pgdat, 1UL << compound_order(page)))
1578 return 0;
Mel Gormanb32967f2012-11-19 12:35:47 +00001579
Hugh Dickins340ef392013-02-22 16:34:33 -08001580 if (isolate_lru_page(page))
1581 return 0;
Mel Gormanb32967f2012-11-19 12:35:47 +00001582
1583 /*
Hugh Dickins340ef392013-02-22 16:34:33 -08001584 * migrate_misplaced_transhuge_page() skips page migration's usual
1585 * check on page_count(), so we must do it here, now that the page
1586 * has been isolated: a GUP pin, or any other pin, prevents migration.
1587 * The expected page count is 3: 1 for page's mapcount and 1 for the
1588 * caller's pin and 1 for the reference taken by isolate_lru_page().
1589 */
1590 if (PageTransHuge(page) && page_count(page) != 3) {
1591 putback_lru_page(page);
1592 return 0;
1593 }
1594
1595 page_lru = page_is_file_cache(page);
1596 mod_zone_page_state(page_zone(page), NR_ISOLATED_ANON + page_lru,
1597 hpage_nr_pages(page));
1598
1599 /*
1600 * Isolating the page has taken another reference, so the
1601 * caller's reference can be safely dropped without the page
1602 * disappearing underneath us during migration.
Mel Gormanb32967f2012-11-19 12:35:47 +00001603 */
1604 put_page(page);
Hugh Dickins340ef392013-02-22 16:34:33 -08001605 return 1;
Mel Gormanb32967f2012-11-19 12:35:47 +00001606}
1607
Mel Gormana8f60772012-11-14 21:41:46 +00001608/*
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001609 * Attempt to migrate a misplaced page to the specified destination
1610 * node. Caller is expected to have an elevated reference count on
1611 * the page that will be dropped by this function before returning.
1612 */
Mel Gorman1bc115d2013-10-07 11:29:05 +01001613int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma,
1614 int node)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001615{
Mel Gormana8f60772012-11-14 21:41:46 +00001616 pg_data_t *pgdat = NODE_DATA(node);
Hugh Dickins340ef392013-02-22 16:34:33 -08001617 int isolated;
Mel Gormanb32967f2012-11-19 12:35:47 +00001618 int nr_remaining;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001619 LIST_HEAD(migratepages);
1620
1621 /*
Mel Gorman1bc115d2013-10-07 11:29:05 +01001622 * Don't migrate file pages that are mapped in multiple processes
1623 * with execute permissions as they are probably shared libraries.
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001624 */
Mel Gorman1bc115d2013-10-07 11:29:05 +01001625 if (page_mapcount(page) != 1 && page_is_file_cache(page) &&
1626 (vma->vm_flags & VM_EXEC))
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001627 goto out;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001628
Mel Gormana8f60772012-11-14 21:41:46 +00001629 /*
1630 * Rate-limit the amount of data that is being migrated to a node.
1631 * Optimal placement is no good if the memory bus is saturated and
1632 * all the time is being spent migrating!
1633 */
Hugh Dickins340ef392013-02-22 16:34:33 -08001634 if (numamigrate_update_ratelimit(pgdat, 1))
Mel Gormana8f60772012-11-14 21:41:46 +00001635 goto out;
Mel Gormana8f60772012-11-14 21:41:46 +00001636
Mel Gormanb32967f2012-11-19 12:35:47 +00001637 isolated = numamigrate_isolate_page(pgdat, page);
1638 if (!isolated)
1639 goto out;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001640
Mel Gormanb32967f2012-11-19 12:35:47 +00001641 list_add(&page->lru, &migratepages);
Hugh Dickins9c620e22013-02-22 16:35:14 -08001642 nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_page,
1643 node, MIGRATE_ASYNC, MR_NUMA_MISPLACED);
Mel Gormanb32967f2012-11-19 12:35:47 +00001644 if (nr_remaining) {
1645 putback_lru_pages(&migratepages);
1646 isolated = 0;
1647 } else
1648 count_vm_numa_event(NUMA_PAGE_MIGRATE);
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001649 BUG_ON(!list_empty(&migratepages));
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001650 return isolated;
Hugh Dickins340ef392013-02-22 16:34:33 -08001651
1652out:
1653 put_page(page);
1654 return 0;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001655}
Mel Gorman220018d2012-12-05 09:32:56 +00001656#endif /* CONFIG_NUMA_BALANCING */
Mel Gormanb32967f2012-11-19 12:35:47 +00001657
Mel Gorman220018d2012-12-05 09:32:56 +00001658#if defined(CONFIG_NUMA_BALANCING) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
Hugh Dickins340ef392013-02-22 16:34:33 -08001659/*
1660 * Migrates a THP to a given target node. page must be locked and is unlocked
1661 * before returning.
1662 */
Mel Gormanb32967f2012-11-19 12:35:47 +00001663int migrate_misplaced_transhuge_page(struct mm_struct *mm,
1664 struct vm_area_struct *vma,
1665 pmd_t *pmd, pmd_t entry,
1666 unsigned long address,
1667 struct page *page, int node)
1668{
1669 unsigned long haddr = address & HPAGE_PMD_MASK;
1670 pg_data_t *pgdat = NODE_DATA(node);
1671 int isolated = 0;
1672 struct page *new_page = NULL;
1673 struct mem_cgroup *memcg = NULL;
1674 int page_lru = page_is_file_cache(page);
1675
1676 /*
Mel Gormanb32967f2012-11-19 12:35:47 +00001677 * Rate-limit the amount of data that is being migrated to a node.
1678 * Optimal placement is no good if the memory bus is saturated and
1679 * all the time is being spent migrating!
1680 */
Mel Gormand28d43352012-11-29 09:24:36 +00001681 if (numamigrate_update_ratelimit(pgdat, HPAGE_PMD_NR))
Mel Gormanb32967f2012-11-19 12:35:47 +00001682 goto out_dropref;
1683
1684 new_page = alloc_pages_node(node,
1685 (GFP_TRANSHUGE | GFP_THISNODE) & ~__GFP_WAIT, HPAGE_PMD_ORDER);
Hugh Dickins340ef392013-02-22 16:34:33 -08001686 if (!new_page)
1687 goto out_fail;
1688
Peter Zijlstra90572892013-10-07 11:29:20 +01001689 page_cpupid_xchg_last(new_page, page_cpupid_last(page));
Mel Gormanb32967f2012-11-19 12:35:47 +00001690
1691 isolated = numamigrate_isolate_page(pgdat, page);
Hugh Dickins340ef392013-02-22 16:34:33 -08001692 if (!isolated) {
Mel Gormanb32967f2012-11-19 12:35:47 +00001693 put_page(new_page);
Hugh Dickins340ef392013-02-22 16:34:33 -08001694 goto out_fail;
Mel Gormanb32967f2012-11-19 12:35:47 +00001695 }
1696
1697 /* Prepare a page as a migration target */
1698 __set_page_locked(new_page);
1699 SetPageSwapBacked(new_page);
1700
1701 /* anon mapping, we can simply copy page->mapping to the new page: */
1702 new_page->mapping = page->mapping;
1703 new_page->index = page->index;
1704 migrate_page_copy(new_page, page);
1705 WARN_ON(PageLRU(new_page));
1706
1707 /* Recheck the target PMD */
1708 spin_lock(&mm->page_table_lock);
1709 if (unlikely(!pmd_same(*pmd, entry))) {
1710 spin_unlock(&mm->page_table_lock);
1711
1712 /* Reverse changes made by migrate_page_copy() */
1713 if (TestClearPageActive(new_page))
1714 SetPageActive(page);
1715 if (TestClearPageUnevictable(new_page))
1716 SetPageUnevictable(page);
1717 mlock_migrate_page(page, new_page);
1718
1719 unlock_page(new_page);
1720 put_page(new_page); /* Free it */
1721
Mel Gormana54a4072013-10-07 11:28:46 +01001722 /* Retake the callers reference and putback on LRU */
1723 get_page(page);
Mel Gormanb32967f2012-11-19 12:35:47 +00001724 putback_lru_page(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001725 mod_zone_page_state(page_zone(page),
1726 NR_ISOLATED_ANON + page_lru, -HPAGE_PMD_NR);
1727 goto out_fail;
Mel Gormanb32967f2012-11-19 12:35:47 +00001728 }
1729
1730 /*
1731 * Traditional migration needs to prepare the memcg charge
1732 * transaction early to prevent the old page from being
1733 * uncharged when installing migration entries. Here we can
1734 * save the potential rollback and start the charge transfer
1735 * only when migration is already known to end successfully.
1736 */
1737 mem_cgroup_prepare_migration(page, new_page, &memcg);
1738
1739 entry = mk_pmd(new_page, vma->vm_page_prot);
1740 entry = pmd_mknonnuma(entry);
1741 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1742 entry = pmd_mkhuge(entry);
1743
Mel Gormana54a4072013-10-07 11:28:46 +01001744 pmdp_clear_flush(vma, haddr, pmd);
Mel Gormanb32967f2012-11-19 12:35:47 +00001745 set_pmd_at(mm, haddr, pmd, entry);
Mel Gormana54a4072013-10-07 11:28:46 +01001746 page_add_new_anon_rmap(new_page, vma, haddr);
Stephen Rothwellce4a9cc2012-12-10 19:50:57 +11001747 update_mmu_cache_pmd(vma, address, &entry);
Mel Gormanb32967f2012-11-19 12:35:47 +00001748 page_remove_rmap(page);
1749 /*
1750 * Finish the charge transaction under the page table lock to
1751 * prevent split_huge_page() from dividing up the charge
1752 * before it's fully transferred to the new page.
1753 */
1754 mem_cgroup_end_migration(memcg, page, new_page, true);
1755 spin_unlock(&mm->page_table_lock);
1756
1757 unlock_page(new_page);
1758 unlock_page(page);
1759 put_page(page); /* Drop the rmap reference */
1760 put_page(page); /* Drop the LRU isolation reference */
1761
1762 count_vm_events(PGMIGRATE_SUCCESS, HPAGE_PMD_NR);
1763 count_vm_numa_events(NUMA_PAGE_MIGRATE, HPAGE_PMD_NR);
1764
Mel Gormanb32967f2012-11-19 12:35:47 +00001765 mod_zone_page_state(page_zone(page),
1766 NR_ISOLATED_ANON + page_lru,
1767 -HPAGE_PMD_NR);
1768 return isolated;
1769
Hugh Dickins340ef392013-02-22 16:34:33 -08001770out_fail:
1771 count_vm_events(PGMIGRATE_FAIL, HPAGE_PMD_NR);
Mel Gormanb32967f2012-11-19 12:35:47 +00001772out_dropref:
Mel Gormana54a4072013-10-07 11:28:46 +01001773 entry = pmd_mknonnuma(entry);
1774 set_pmd_at(mm, haddr, pmd, entry);
1775 update_mmu_cache_pmd(vma, address, &entry);
1776
Hugh Dickins340ef392013-02-22 16:34:33 -08001777 unlock_page(page);
Mel Gormanb32967f2012-11-19 12:35:47 +00001778 put_page(page);
Mel Gormanb32967f2012-11-19 12:35:47 +00001779 return 0;
1780}
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001781#endif /* CONFIG_NUMA_BALANCING */
1782
1783#endif /* CONFIG_NUMA */