blob: 98b998ffa3afea4ae3b36f43b7114997b353d362 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/gfp.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080037
Michal Nazarewicz0d1836c2010-12-21 17:24:26 -080038#include <asm/tlbflush.h>
39
Christoph Lameterb20a3502006-03-22 00:09:12 -080040#include "internal.h"
41
Christoph Lameterb20a3502006-03-22 00:09:12 -080042/*
Christoph Lameter742755a2006-06-23 02:03:55 -070043 * migrate_prep() needs to be called before we start compiling a list of pages
Mel Gorman748446b2010-05-24 14:32:27 -070044 * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is
45 * undesirable, use migrate_prep_local()
Christoph Lameterb20a3502006-03-22 00:09:12 -080046 */
47int migrate_prep(void)
48{
Christoph Lameterb20a3502006-03-22 00:09:12 -080049 /*
50 * Clear the LRU lists so pages can be isolated.
51 * Note that pages may be moved off the LRU after we have
52 * drained them. Those pages will fail to migrate like other
53 * pages that may be busy.
54 */
55 lru_add_drain_all();
56
57 return 0;
58}
59
Mel Gorman748446b2010-05-24 14:32:27 -070060/* Do the necessary work of migrate_prep but not if it involves other CPUs */
61int migrate_prep_local(void)
62{
63 lru_add_drain();
64
65 return 0;
66}
67
Christoph Lameterb20a3502006-03-22 00:09:12 -080068/*
Lee Schermerhorn894bc312008-10-18 20:26:39 -070069 * Add isolated pages on the list back to the LRU under page lock
70 * to avoid leaking evictable pages back onto unevictable list.
Christoph Lameterb20a3502006-03-22 00:09:12 -080071 */
Minchan Kime13861d2010-05-24 14:31:59 -070072void putback_lru_pages(struct list_head *l)
Christoph Lameterb20a3502006-03-22 00:09:12 -080073{
74 struct page *page;
75 struct page *page2;
Christoph Lameterb20a3502006-03-22 00:09:12 -080076
77 list_for_each_entry_safe(page, page2, l, lru) {
Christoph Lametere24f0b82006-06-23 02:03:51 -070078 list_del(&page->lru);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -070079 dec_zone_page_state(page, NR_ISOLATED_ANON +
Johannes Weiner6c0b1352009-09-21 17:02:59 -070080 page_is_file_cache(page));
Lee Schermerhorn894bc312008-10-18 20:26:39 -070081 putback_lru_page(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -080082 }
Christoph Lameterb20a3502006-03-22 00:09:12 -080083}
84
Christoph Lameter06972122006-06-23 02:03:35 -070085/*
86 * Restore a potential migration pte to a working pte entry
87 */
Hugh Dickinse9995ef2009-12-14 17:59:31 -080088static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,
89 unsigned long addr, void *old)
Christoph Lameter06972122006-06-23 02:03:35 -070090{
91 struct mm_struct *mm = vma->vm_mm;
92 swp_entry_t entry;
93 pgd_t *pgd;
94 pud_t *pud;
95 pmd_t *pmd;
96 pte_t *ptep, pte;
97 spinlock_t *ptl;
98
Naoya Horiguchi290408d2010-09-08 10:19:35 +090099 if (unlikely(PageHuge(new))) {
100 ptep = huge_pte_offset(mm, addr);
101 if (!ptep)
102 goto out;
103 ptl = &mm->page_table_lock;
104 } else {
105 pgd = pgd_offset(mm, addr);
106 if (!pgd_present(*pgd))
107 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700108
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900109 pud = pud_offset(pgd, addr);
110 if (!pud_present(*pud))
111 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700112
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900113 pmd = pmd_offset(pud, addr);
Andrea Arcangeli500d65d2011-01-13 15:46:55 -0800114 if (pmd_trans_huge(*pmd))
115 goto out;
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900116 if (!pmd_present(*pmd))
117 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700118
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900119 ptep = pte_offset_map(pmd, addr);
Christoph Lameter06972122006-06-23 02:03:35 -0700120
Hugh Dickins486cf462011-10-19 12:50:35 -0700121 /*
122 * Peek to check is_swap_pte() before taking ptlock? No, we
123 * can race mremap's move_ptes(), which skips anon_vma lock.
124 */
Christoph Lameter06972122006-06-23 02:03:35 -0700125
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900126 ptl = pte_lockptr(mm, pmd);
127 }
128
Christoph Lameter06972122006-06-23 02:03:35 -0700129 spin_lock(ptl);
130 pte = *ptep;
131 if (!is_swap_pte(pte))
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800132 goto unlock;
Christoph Lameter06972122006-06-23 02:03:35 -0700133
134 entry = pte_to_swp_entry(pte);
135
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800136 if (!is_migration_entry(entry) ||
137 migration_entry_to_page(entry) != old)
138 goto unlock;
Christoph Lameter06972122006-06-23 02:03:35 -0700139
Christoph Lameter06972122006-06-23 02:03:35 -0700140 get_page(new);
141 pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
Mel Gormanea38cd42014-10-02 19:47:41 +0100142
143 /* Recheck VMA as permissions can change since migration started */
Christoph Lameter06972122006-06-23 02:03:35 -0700144 if (is_write_migration_entry(entry))
Mel Gormanea38cd42014-10-02 19:47:41 +0100145 pte = maybe_mkwrite(pte, vma);
146
Andi Kleen3ef8fd72010-10-11 16:03:21 +0200147#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900148 if (PageHuge(new))
149 pte = pte_mkhuge(pte);
Andi Kleen3ef8fd72010-10-11 16:03:21 +0200150#endif
Leonid Yegoshin11e88c52013-05-24 15:55:18 -0700151 flush_dcache_page(new);
Christoph Lameter06972122006-06-23 02:03:35 -0700152 set_pte_at(mm, addr, ptep, pte);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700153
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900154 if (PageHuge(new)) {
155 if (PageAnon(new))
156 hugepage_add_anon_rmap(new, vma, addr);
157 else
158 page_dup_rmap(new);
159 } else if (PageAnon(new))
Christoph Lameter04e62a22006-06-23 02:03:38 -0700160 page_add_anon_rmap(new, vma, addr);
161 else
162 page_add_file_rmap(new);
163
164 /* No need to invalidate - it was non-present before */
Russell King4b3073e2009-12-18 16:40:18 +0000165 update_mmu_cache(vma, addr, ptep);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800166unlock:
Christoph Lameter06972122006-06-23 02:03:35 -0700167 pte_unmap_unlock(ptep, ptl);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800168out:
169 return SWAP_AGAIN;
Christoph Lameter06972122006-06-23 02:03:35 -0700170}
171
172/*
Christoph Lameter04e62a22006-06-23 02:03:38 -0700173 * Get rid of all migration entries and replace them by
174 * references to the indicated page.
175 */
176static void remove_migration_ptes(struct page *old, struct page *new)
177{
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800178 rmap_walk(new, remove_migration_pte, old);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700179}
180
181/*
Christoph Lameter06972122006-06-23 02:03:35 -0700182 * Something used the pte of a page under migration. We need to
183 * get to the page and wait until migration is finished.
184 * When we return from this function the fault will be retried.
Christoph Lameter06972122006-06-23 02:03:35 -0700185 */
Naoya Horiguchif517dfe2013-06-12 14:05:04 -0700186static void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
187 spinlock_t *ptl)
Christoph Lameter06972122006-06-23 02:03:35 -0700188{
Naoya Horiguchif517dfe2013-06-12 14:05:04 -0700189 pte_t pte;
Christoph Lameter06972122006-06-23 02:03:35 -0700190 swp_entry_t entry;
191 struct page *page;
192
Naoya Horiguchif517dfe2013-06-12 14:05:04 -0700193 spin_lock(ptl);
Christoph Lameter06972122006-06-23 02:03:35 -0700194 pte = *ptep;
195 if (!is_swap_pte(pte))
196 goto out;
197
198 entry = pte_to_swp_entry(pte);
199 if (!is_migration_entry(entry))
200 goto out;
201
202 page = migration_entry_to_page(entry);
203
Nick Piggine2867812008-07-25 19:45:30 -0700204 /*
205 * Once radix-tree replacement of page migration started, page_count
206 * *must* be zero. And, we don't want to call wait_on_page_locked()
207 * against a page without get_page().
208 * So, we use get_page_unless_zero(), here. Even failed, page fault
209 * will occur again.
210 */
211 if (!get_page_unless_zero(page))
212 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700213 pte_unmap_unlock(ptep, ptl);
214 wait_on_page_locked(page);
215 put_page(page);
216 return;
217out:
218 pte_unmap_unlock(ptep, ptl);
219}
220
Naoya Horiguchif517dfe2013-06-12 14:05:04 -0700221void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
222 unsigned long address)
223{
224 spinlock_t *ptl = pte_lockptr(mm, pmd);
225 pte_t *ptep = pte_offset_map(pmd, address);
226 __migration_entry_wait(mm, ptep, ptl);
227}
228
229void migration_entry_wait_huge(struct mm_struct *mm, pte_t *pte)
230{
231 spinlock_t *ptl = &(mm)->page_table_lock;
232 __migration_entry_wait(mm, pte, ptl);
233}
234
Mel Gormanb969c4a2012-01-12 17:19:34 -0800235#ifdef CONFIG_BLOCK
236/* Returns true if all buffers are successfully locked */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800237static bool buffer_migrate_lock_buffers(struct buffer_head *head,
238 enum migrate_mode mode)
Mel Gormanb969c4a2012-01-12 17:19:34 -0800239{
240 struct buffer_head *bh = head;
241
242 /* Simple case, sync compaction */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800243 if (mode != MIGRATE_ASYNC) {
Mel Gormanb969c4a2012-01-12 17:19:34 -0800244 do {
245 get_bh(bh);
246 lock_buffer(bh);
247 bh = bh->b_this_page;
248
249 } while (bh != head);
250
251 return true;
252 }
253
254 /* async case, we cannot block on lock_buffer so use trylock_buffer */
255 do {
256 get_bh(bh);
257 if (!trylock_buffer(bh)) {
258 /*
259 * We failed to lock the buffer and cannot stall in
260 * async migration. Release the taken locks
261 */
262 struct buffer_head *failed_bh = bh;
263 put_bh(failed_bh);
264 bh = head;
265 while (bh != failed_bh) {
266 unlock_buffer(bh);
267 put_bh(bh);
268 bh = bh->b_this_page;
269 }
270 return false;
271 }
272
273 bh = bh->b_this_page;
274 } while (bh != head);
275 return true;
276}
277#else
278static inline bool buffer_migrate_lock_buffers(struct buffer_head *head,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800279 enum migrate_mode mode)
Mel Gormanb969c4a2012-01-12 17:19:34 -0800280{
281 return true;
282}
283#endif /* CONFIG_BLOCK */
284
Christoph Lameterb20a3502006-03-22 00:09:12 -0800285/*
Christoph Lameterc3fcf8a2006-06-23 02:03:32 -0700286 * Replace the page in the mapping.
Christoph Lameter5b5c7122006-06-23 02:03:29 -0700287 *
288 * The number of remaining references must be:
289 * 1 for anonymous pages without a mapping
290 * 2 for pages with a mapping
David Howells266cf652009-04-03 16:42:36 +0100291 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800292 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700293static int migrate_page_move_mapping(struct address_space *mapping,
Mel Gormanb969c4a2012-01-12 17:19:34 -0800294 struct page *newpage, struct page *page,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800295 struct buffer_head *head, enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800296{
Nick Piggine2867812008-07-25 19:45:30 -0700297 int expected_count;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800298 void **pslot;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800299
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700300 if (!mapping) {
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700301 /* Anonymous page without mapping */
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700302 if (page_count(page) != 1)
303 return -EAGAIN;
304 return 0;
305 }
306
Nick Piggin19fd6232008-07-25 19:45:32 -0700307 spin_lock_irq(&mapping->tree_lock);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800308
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800309 pslot = radix_tree_lookup_slot(&mapping->page_tree,
310 page_index(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800311
Johannes Weineredcf4742009-09-21 17:02:59 -0700312 expected_count = 2 + page_has_private(page);
Nick Piggine2867812008-07-25 19:45:30 -0700313 if (page_count(page) != expected_count ||
Mel Gorman29c1f672011-01-13 15:47:21 -0800314 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700315 spin_unlock_irq(&mapping->tree_lock);
Christoph Lametere23ca002006-04-10 22:52:57 -0700316 return -EAGAIN;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800317 }
318
Nick Piggine2867812008-07-25 19:45:30 -0700319 if (!page_freeze_refs(page, expected_count)) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700320 spin_unlock_irq(&mapping->tree_lock);
Nick Piggine2867812008-07-25 19:45:30 -0700321 return -EAGAIN;
322 }
323
Christoph Lameterb20a3502006-03-22 00:09:12 -0800324 /*
Mel Gormanb969c4a2012-01-12 17:19:34 -0800325 * In the async migration case of moving a page with buffers, lock the
326 * buffers using trylock before the mapping is moved. If the mapping
327 * was moved, we later failed to lock the buffers and could not move
328 * the mapping back due to an elevated page count, we would have to
329 * block waiting on other references to be dropped.
330 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800331 if (mode == MIGRATE_ASYNC && head &&
332 !buffer_migrate_lock_buffers(head, mode)) {
Mel Gormanb969c4a2012-01-12 17:19:34 -0800333 page_unfreeze_refs(page, expected_count);
334 spin_unlock_irq(&mapping->tree_lock);
335 return -EAGAIN;
336 }
337
338 /*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800339 * Now we know that no one else is looking at the page.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800340 */
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800341 get_page(newpage); /* add cache reference */
Christoph Lameterb20a3502006-03-22 00:09:12 -0800342 if (PageSwapCache(page)) {
343 SetPageSwapCache(newpage);
344 set_page_private(newpage, page_private(page));
345 }
346
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800347 radix_tree_replace_slot(pslot, newpage);
348
349 /*
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800350 * Drop cache reference from old page by unfreezing
351 * to one less reference.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800352 * We know this isn't the last reference.
353 */
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800354 page_unfreeze_refs(page, expected_count - 1);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800355
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700356 /*
357 * If moved to a different zone then also account
358 * the page for that zone. Other VM counters will be
359 * taken care of when we establish references to the
360 * new page and drop references to the old page.
361 *
362 * Note that anonymous pages are accounted for
363 * via NR_FILE_PAGES and NR_ANON_PAGES if they
364 * are mapped to swap space.
365 */
366 __dec_zone_page_state(page, NR_FILE_PAGES);
367 __inc_zone_page_state(newpage, NR_FILE_PAGES);
Andrea Arcangeli99a15e22011-06-16 12:56:19 -0700368 if (!PageSwapCache(page) && PageSwapBacked(page)) {
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700369 __dec_zone_page_state(page, NR_SHMEM);
370 __inc_zone_page_state(newpage, NR_SHMEM);
371 }
Nick Piggin19fd6232008-07-25 19:45:32 -0700372 spin_unlock_irq(&mapping->tree_lock);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800373
374 return 0;
375}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800376
377/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900378 * The expected number of remaining references is the same as that
379 * of migrate_page_move_mapping().
380 */
381int migrate_huge_page_move_mapping(struct address_space *mapping,
382 struct page *newpage, struct page *page)
383{
384 int expected_count;
385 void **pslot;
386
387 if (!mapping) {
388 if (page_count(page) != 1)
389 return -EAGAIN;
390 return 0;
391 }
392
393 spin_lock_irq(&mapping->tree_lock);
394
395 pslot = radix_tree_lookup_slot(&mapping->page_tree,
396 page_index(page));
397
398 expected_count = 2 + page_has_private(page);
399 if (page_count(page) != expected_count ||
Mel Gorman29c1f672011-01-13 15:47:21 -0800400 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900401 spin_unlock_irq(&mapping->tree_lock);
402 return -EAGAIN;
403 }
404
405 if (!page_freeze_refs(page, expected_count)) {
406 spin_unlock_irq(&mapping->tree_lock);
407 return -EAGAIN;
408 }
409
410 get_page(newpage);
411
412 radix_tree_replace_slot(pslot, newpage);
413
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800414 page_unfreeze_refs(page, expected_count - 1);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900415
416 spin_unlock_irq(&mapping->tree_lock);
417 return 0;
418}
419
420/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800421 * Copy the page to its new location
422 */
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900423void migrate_page_copy(struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800424{
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900425 if (PageHuge(page))
426 copy_huge_page(newpage, page);
427 else
428 copy_highpage(newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800429
430 if (PageError(page))
431 SetPageError(newpage);
432 if (PageReferenced(page))
433 SetPageReferenced(newpage);
434 if (PageUptodate(page))
435 SetPageUptodate(newpage);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700436 if (TestClearPageActive(page)) {
437 VM_BUG_ON(PageUnevictable(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800438 SetPageActive(newpage);
Lee Schermerhorn418b27e2009-12-14 17:59:54 -0800439 } else if (TestClearPageUnevictable(page))
440 SetPageUnevictable(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800441 if (PageChecked(page))
442 SetPageChecked(newpage);
443 if (PageMappedToDisk(page))
444 SetPageMappedToDisk(newpage);
445
446 if (PageDirty(page)) {
447 clear_page_dirty_for_io(page);
Nick Piggin3a902c52008-04-30 00:55:16 -0700448 /*
449 * Want to mark the page and the radix tree as dirty, and
450 * redo the accounting that clear_page_dirty_for_io undid,
451 * but we can't use set_page_dirty because that function
452 * is actually a signal that all of the page has become dirty.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300453 * Whereas only part of our page may be dirty.
Nick Piggin3a902c52008-04-30 00:55:16 -0700454 */
455 __set_page_dirty_nobuffers(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800456 }
457
Nick Pigginb291f002008-10-18 20:26:44 -0700458 mlock_migrate_page(newpage, page);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800459 ksm_migrate_page(newpage, page);
Nick Pigginb291f002008-10-18 20:26:44 -0700460
Christoph Lameterb20a3502006-03-22 00:09:12 -0800461 ClearPageSwapCache(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800462 ClearPagePrivate(page);
463 set_page_private(page, 0);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800464
465 /*
466 * If any waiters have accumulated on the new page then
467 * wake them up.
468 */
469 if (PageWriteback(newpage))
470 end_page_writeback(newpage);
471}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800472
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700473/************************************************************
474 * Migration functions
475 ***********************************************************/
476
477/* Always fail migration. Used for mappings that are not movable */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700478int fail_migrate_page(struct address_space *mapping,
479 struct page *newpage, struct page *page)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700480{
481 return -EIO;
482}
483EXPORT_SYMBOL(fail_migrate_page);
484
Christoph Lameterb20a3502006-03-22 00:09:12 -0800485/*
486 * Common logic to directly migrate a single page suitable for
David Howells266cf652009-04-03 16:42:36 +0100487 * pages that do not use PagePrivate/PagePrivate2.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800488 *
489 * Pages are locked upon entry and exit.
490 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700491int migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800492 struct page *newpage, struct page *page,
493 enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800494{
495 int rc;
496
497 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
498
Mel Gormana6bc32b2012-01-12 17:19:43 -0800499 rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800500
501 if (rc)
502 return rc;
503
504 migrate_page_copy(newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800505 return 0;
506}
507EXPORT_SYMBOL(migrate_page);
508
David Howells93614012006-09-30 20:45:40 +0200509#ifdef CONFIG_BLOCK
Christoph Lameterb20a3502006-03-22 00:09:12 -0800510/*
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700511 * Migration function for pages with buffers. This function can only be used
512 * if the underlying filesystem guarantees that no other references to "page"
513 * exist.
514 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700515int buffer_migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800516 struct page *newpage, struct page *page, enum migrate_mode mode)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700517{
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700518 struct buffer_head *bh, *head;
519 int rc;
520
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700521 if (!page_has_buffers(page))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800522 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700523
524 head = page_buffers(page);
525
Mel Gormana6bc32b2012-01-12 17:19:43 -0800526 rc = migrate_page_move_mapping(mapping, newpage, page, head, mode);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700527
528 if (rc)
529 return rc;
530
Mel Gormanb969c4a2012-01-12 17:19:34 -0800531 /*
532 * In the async case, migrate_page_move_mapping locked the buffers
533 * with an IRQ-safe spinlock held. In the sync case, the buffers
534 * need to be locked now
535 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800536 if (mode != MIGRATE_ASYNC)
537 BUG_ON(!buffer_migrate_lock_buffers(head, mode));
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700538
539 ClearPagePrivate(page);
540 set_page_private(newpage, page_private(page));
541 set_page_private(page, 0);
542 put_page(page);
543 get_page(newpage);
544
545 bh = head;
546 do {
547 set_bh_page(bh, newpage, bh_offset(bh));
548 bh = bh->b_this_page;
549
550 } while (bh != head);
551
552 SetPagePrivate(newpage);
553
554 migrate_page_copy(newpage, page);
555
556 bh = head;
557 do {
558 unlock_buffer(bh);
559 put_bh(bh);
560 bh = bh->b_this_page;
561
562 } while (bh != head);
563
564 return 0;
565}
566EXPORT_SYMBOL(buffer_migrate_page);
David Howells93614012006-09-30 20:45:40 +0200567#endif
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700568
Christoph Lameter04e62a22006-06-23 02:03:38 -0700569/*
570 * Writeback a page to clean the dirty state
571 */
572static int writeout(struct address_space *mapping, struct page *page)
573{
574 struct writeback_control wbc = {
575 .sync_mode = WB_SYNC_NONE,
576 .nr_to_write = 1,
577 .range_start = 0,
578 .range_end = LLONG_MAX,
Christoph Lameter04e62a22006-06-23 02:03:38 -0700579 .for_reclaim = 1
580 };
581 int rc;
582
583 if (!mapping->a_ops->writepage)
584 /* No write method for the address space */
585 return -EINVAL;
586
587 if (!clear_page_dirty_for_io(page))
588 /* Someone else already triggered a write */
589 return -EAGAIN;
590
591 /*
592 * A dirty page may imply that the underlying filesystem has
593 * the page on some queue. So the page must be clean for
594 * migration. Writeout may mean we loose the lock and the
595 * page state is no longer what we checked for earlier.
596 * At this point we know that the migration attempt cannot
597 * be successful.
598 */
599 remove_migration_ptes(page, page);
600
601 rc = mapping->a_ops->writepage(page, &wbc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700602
603 if (rc != AOP_WRITEPAGE_ACTIVATE)
604 /* unlocked. Relock */
605 lock_page(page);
606
Hugh Dickinsbda85502008-11-19 15:36:36 -0800607 return (rc < 0) ? -EIO : -EAGAIN;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700608}
609
610/*
611 * Default handling if a filesystem does not provide a migration function.
612 */
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700613static int fallback_migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800614 struct page *newpage, struct page *page, enum migrate_mode mode)
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700615{
Mel Gormanb969c4a2012-01-12 17:19:34 -0800616 if (PageDirty(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800617 /* Only writeback pages in full synchronous migration */
618 if (mode != MIGRATE_SYNC)
Mel Gormanb969c4a2012-01-12 17:19:34 -0800619 return -EBUSY;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700620 return writeout(mapping, page);
Mel Gormanb969c4a2012-01-12 17:19:34 -0800621 }
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700622
623 /*
624 * Buffers may be managed in a filesystem specific way.
625 * We must have no buffers or drop them.
626 */
David Howells266cf652009-04-03 16:42:36 +0100627 if (page_has_private(page) &&
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700628 !try_to_release_page(page, GFP_KERNEL))
629 return -EAGAIN;
630
Mel Gormana6bc32b2012-01-12 17:19:43 -0800631 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700632}
633
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700634/*
Christoph Lametere24f0b82006-06-23 02:03:51 -0700635 * Move a page to a newly allocated page
636 * The page is locked and all ptes have been successfully removed.
637 *
638 * The new page will have replaced the old page if this function
639 * is successful.
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700640 *
641 * Return value:
642 * < 0 - error code
643 * == 0 - success
Christoph Lametere24f0b82006-06-23 02:03:51 -0700644 */
Mel Gorman3fe20112010-05-24 14:32:20 -0700645static int move_to_new_page(struct page *newpage, struct page *page,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800646 int remap_swapcache, enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700647{
648 struct address_space *mapping;
649 int rc;
650
651 /*
652 * Block others from accessing the page when we get around to
653 * establishing additional references. We are the only one
654 * holding a reference to the new page at this point.
655 */
Nick Piggin529ae9a2008-08-02 12:01:03 +0200656 if (!trylock_page(newpage))
Christoph Lametere24f0b82006-06-23 02:03:51 -0700657 BUG();
658
659 /* Prepare mapping for the new page.*/
660 newpage->index = page->index;
661 newpage->mapping = page->mapping;
Rik van Rielb2e18532008-10-18 20:26:30 -0700662 if (PageSwapBacked(page))
663 SetPageSwapBacked(newpage);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700664
665 mapping = page_mapping(page);
666 if (!mapping)
Mel Gormana6bc32b2012-01-12 17:19:43 -0800667 rc = migrate_page(mapping, newpage, page, mode);
Mel Gormanb969c4a2012-01-12 17:19:34 -0800668 else if (mapping->a_ops->migratepage)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700669 /*
Mel Gormanb969c4a2012-01-12 17:19:34 -0800670 * Most pages have a mapping and most filesystems provide a
671 * migratepage callback. Anonymous pages are part of swap
672 * space which also has its own migratepage callback. This
673 * is the most common path for page migration.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700674 */
Mel Gormanb969c4a2012-01-12 17:19:34 -0800675 rc = mapping->a_ops->migratepage(mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800676 newpage, page, mode);
Mel Gormanb969c4a2012-01-12 17:19:34 -0800677 else
Mel Gormana6bc32b2012-01-12 17:19:43 -0800678 rc = fallback_migrate_page(mapping, newpage, page, mode);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700679
Mel Gorman3fe20112010-05-24 14:32:20 -0700680 if (rc) {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700681 newpage->mapping = NULL;
Mel Gorman3fe20112010-05-24 14:32:20 -0700682 } else {
683 if (remap_swapcache)
684 remove_migration_ptes(page, newpage);
Konstantin Khlebnikov35512ec2012-02-03 15:37:13 -0800685 page->mapping = NULL;
Mel Gorman3fe20112010-05-24 14:32:20 -0700686 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700687
688 unlock_page(newpage);
689
690 return rc;
691}
692
Minchan Kim0dabec92011-10-31 17:06:57 -0700693static int __unmap_and_move(struct page *page, struct page *newpage,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800694 int force, bool offlining, enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700695{
Minchan Kim0dabec92011-10-31 17:06:57 -0700696 int rc = -EAGAIN;
Mel Gorman3fe20112010-05-24 14:32:20 -0700697 int remap_swapcache = 1;
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800698 int charge = 0;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -0700699 struct mem_cgroup *mem;
Mel Gorman3f6c8272010-05-24 14:32:17 -0700700 struct anon_vma *anon_vma = NULL;
Christoph Lameter95a402c2006-06-23 02:03:53 -0700701
Nick Piggin529ae9a2008-08-02 12:01:03 +0200702 if (!trylock_page(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800703 if (!force || mode == MIGRATE_ASYNC)
Minchan Kim0dabec92011-10-31 17:06:57 -0700704 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800705
706 /*
707 * It's not safe for direct compaction to call lock_page.
708 * For example, during page readahead pages are added locked
709 * to the LRU. Later, when the IO completes the pages are
710 * marked uptodate and unlocked. However, the queueing
711 * could be merging multiple pages for one bio (e.g.
712 * mpage_readpages). If an allocation happens for the
713 * second or third page, the process can end up locking
714 * the same page twice and deadlocking. Rather than
715 * trying to be clever about what pages can be locked,
716 * avoid the use of lock_page for direct compaction
717 * altogether.
718 */
719 if (current->flags & PF_MEMALLOC)
Minchan Kim0dabec92011-10-31 17:06:57 -0700720 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800721
Christoph Lametere24f0b82006-06-23 02:03:51 -0700722 lock_page(page);
723 }
724
Hugh Dickins62b61f62009-12-14 17:59:33 -0800725 /*
726 * Only memory hotplug's offline_pages() caller has locked out KSM,
727 * and can safely migrate a KSM page. The other cases have skipped
728 * PageKsm along with PageReserved - but it is only now when we have
729 * the page lock that we can be certain it will not go KSM beneath us
730 * (KSM will not upgrade a page from PageAnon to PageKsm when it sees
731 * its pagecount raised, but only here do we take the page lock which
732 * serializes that).
733 */
734 if (PageKsm(page) && !offlining) {
735 rc = -EBUSY;
736 goto unlock;
737 }
738
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800739 /* charge against new page */
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700740 charge = mem_cgroup_prepare_migration(page, newpage, &mem, GFP_KERNEL);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800741 if (charge == -ENOMEM) {
742 rc = -ENOMEM;
743 goto unlock;
744 }
745 BUG_ON(charge);
746
Christoph Lametere24f0b82006-06-23 02:03:51 -0700747 if (PageWriteback(page)) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700748 /*
Mel Gormana6bc32b2012-01-12 17:19:43 -0800749 * Only in the case of a full syncronous migration is it
750 * necessary to wait for PageWriteback. In the async case,
751 * the retry loop is too short and in the sync-light case,
752 * the overhead of stalling is too much
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700753 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800754 if (mode != MIGRATE_SYNC) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700755 rc = -EBUSY;
756 goto uncharge;
757 }
758 if (!force)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800759 goto uncharge;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700760 wait_on_page_writeback(page);
761 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700762 /*
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700763 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
764 * we cannot notice that anon_vma is freed while we migrates a page.
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800765 * This get_anon_vma() delays freeing anon_vma pointer until the end
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700766 * of migration. File cache pages are no problem because of page_lock()
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700767 * File Caches may use write_page() or lock_page() in migration, then,
768 * just care Anon page here.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700769 */
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700770 if (PageAnon(page)) {
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800771 /*
772 * Only page_lock_anon_vma() understands the subtleties of
773 * getting a hold on an anon_vma from outside one of its mms.
774 */
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700775 anon_vma = page_get_anon_vma(page);
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800776 if (anon_vma) {
777 /*
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700778 * Anon page
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800779 */
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800780 } else if (PageSwapCache(page)) {
Mel Gorman3fe20112010-05-24 14:32:20 -0700781 /*
782 * We cannot be sure that the anon_vma of an unmapped
783 * swapcache page is safe to use because we don't
784 * know in advance if the VMA that this page belonged
785 * to still exists. If the VMA and others sharing the
786 * data have been freed, then the anon_vma could
787 * already be invalid.
788 *
789 * To avoid this possibility, swapcache pages get
790 * migrated but are not remapped when migration
791 * completes
792 */
793 remap_swapcache = 0;
794 } else {
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800795 goto uncharge;
Mel Gorman3fe20112010-05-24 14:32:20 -0700796 }
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700797 }
Shaohua Li62e1c552008-02-04 22:29:33 -0800798
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700799 /*
Shaohua Li62e1c552008-02-04 22:29:33 -0800800 * Corner case handling:
801 * 1. When a new swap-cache page is read into, it is added to the LRU
802 * and treated as swapcache but it has no rmap yet.
803 * Calling try_to_unmap() against a page->mapping==NULL page will
804 * trigger a BUG. So handle it here.
805 * 2. An orphaned page (see truncate_complete_page) might have
806 * fs-private metadata. The page can be picked up due to memory
807 * offlining. Everywhere else except page reclaim, the page is
808 * invisible to the vm, so the page can not be migrated. So try to
809 * free the metadata, so the page can be freed.
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700810 */
Shaohua Li62e1c552008-02-04 22:29:33 -0800811 if (!page->mapping) {
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800812 VM_BUG_ON(PageAnon(page));
813 if (page_has_private(page)) {
Shaohua Li62e1c552008-02-04 22:29:33 -0800814 try_to_free_buffers(page);
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800815 goto uncharge;
Shaohua Li62e1c552008-02-04 22:29:33 -0800816 }
Shaohua Liabfc3482009-09-21 17:01:19 -0700817 goto skip_unmap;
Shaohua Li62e1c552008-02-04 22:29:33 -0800818 }
819
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700820 /* Establish migration ptes or remove ptes */
Andi Kleen14fa31b2009-09-16 11:50:10 +0200821 try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700822
Shaohua Liabfc3482009-09-21 17:01:19 -0700823skip_unmap:
Christoph Lametere6a15302006-06-25 05:46:49 -0700824 if (!page_mapped(page))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800825 rc = move_to_new_page(newpage, page, remap_swapcache, mode);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700826
Mel Gorman3fe20112010-05-24 14:32:20 -0700827 if (rc && remap_swapcache)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700828 remove_migration_ptes(page, page);
Mel Gorman3f6c8272010-05-24 14:32:17 -0700829
830 /* Drop an anon_vma reference if we took one */
Rik van Riel76545062010-08-09 17:18:41 -0700831 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -0700832 put_anon_vma(anon_vma);
Mel Gorman3f6c8272010-05-24 14:32:17 -0700833
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800834uncharge:
835 if (!charge)
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -0800836 mem_cgroup_end_migration(mem, page, newpage, rc == 0);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700837unlock:
838 unlock_page(page);
Minchan Kim0dabec92011-10-31 17:06:57 -0700839out:
840 return rc;
841}
Christoph Lameter95a402c2006-06-23 02:03:53 -0700842
Minchan Kim0dabec92011-10-31 17:06:57 -0700843/*
844 * Obtain the lock on page, remove all ptes and migrate the page
845 * to the newly allocated page in newpage.
846 */
847static int unmap_and_move(new_page_t get_new_page, unsigned long private,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800848 struct page *page, int force, bool offlining,
849 enum migrate_mode mode)
Minchan Kim0dabec92011-10-31 17:06:57 -0700850{
851 int rc = 0;
852 int *result = NULL;
853 struct page *newpage = get_new_page(page, private, &result);
854
855 if (!newpage)
856 return -ENOMEM;
857
858 if (page_count(page) == 1) {
859 /* page was freed from under us. So we are done. */
860 goto out;
861 }
862
863 if (unlikely(PageTransHuge(page)))
864 if (unlikely(split_huge_page(page)))
865 goto out;
866
Mel Gormana6bc32b2012-01-12 17:19:43 -0800867 rc = __unmap_and_move(page, newpage, force, offlining, mode);
Minchan Kim0dabec92011-10-31 17:06:57 -0700868out:
Christoph Lametere24f0b82006-06-23 02:03:51 -0700869 if (rc != -EAGAIN) {
Minchan Kim0dabec92011-10-31 17:06:57 -0700870 /*
871 * A page that has been migrated has all references
872 * removed and will be freed. A page that has not been
873 * migrated will have kepts its references and be
874 * restored.
875 */
876 list_del(&page->lru);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -0700877 dec_zone_page_state(page, NR_ISOLATED_ANON +
Johannes Weiner6c0b1352009-09-21 17:02:59 -0700878 page_is_file_cache(page));
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700879 putback_lru_page(page);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700880 }
Christoph Lameter95a402c2006-06-23 02:03:53 -0700881 /*
882 * Move the new page to the LRU. If migration was not successful
883 * then this will free the page.
884 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700885 putback_lru_page(newpage);
Christoph Lameter742755a2006-06-23 02:03:55 -0700886 if (result) {
887 if (rc)
888 *result = rc;
889 else
890 *result = page_to_nid(newpage);
891 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700892 return rc;
893}
894
895/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900896 * Counterpart of unmap_and_move_page() for hugepage migration.
897 *
898 * This function doesn't wait the completion of hugepage I/O
899 * because there is no race between I/O and migration for hugepage.
900 * Note that currently hugepage I/O occurs only in direct I/O
901 * where no lock is held and PG_writeback is irrelevant,
902 * and writeback status of all subpages are counted in the reference
903 * count of the head page (i.e. if all subpages of a 2MB hugepage are
904 * under direct I/O, the reference of the head page is 512 and a bit more.)
905 * This means that when we try to migrate hugepage whose subpages are
906 * doing direct I/O, some references remain after try_to_unmap() and
907 * hugepage migration fails without data corruption.
908 *
909 * There is also no race when direct I/O is issued on the page under migration,
910 * because then pte is replaced with migration swap entry and direct I/O code
911 * will wait in the page fault for migration to complete.
912 */
913static int unmap_and_move_huge_page(new_page_t get_new_page,
914 unsigned long private, struct page *hpage,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800915 int force, bool offlining,
916 enum migrate_mode mode)
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900917{
918 int rc = 0;
919 int *result = NULL;
920 struct page *new_hpage = get_new_page(hpage, private, &result);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900921 struct anon_vma *anon_vma = NULL;
922
923 if (!new_hpage)
924 return -ENOMEM;
925
926 rc = -EAGAIN;
927
928 if (!trylock_page(hpage)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800929 if (!force || mode != MIGRATE_SYNC)
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900930 goto out;
931 lock_page(hpage);
932 }
933
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700934 if (PageAnon(hpage))
935 anon_vma = page_get_anon_vma(hpage);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900936
937 try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
938
939 if (!page_mapped(hpage))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800940 rc = move_to_new_page(new_hpage, hpage, 1, mode);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900941
942 if (rc)
943 remove_migration_ptes(hpage, hpage);
944
Hugh Dickinsfd4a4662011-01-13 15:47:31 -0800945 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -0700946 put_anon_vma(anon_vma);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900947 unlock_page(hpage);
948
Hillf Danton09761332011-12-08 14:34:20 -0800949out:
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900950 if (rc != -EAGAIN) {
951 list_del(&hpage->lru);
952 put_page(hpage);
953 }
954
955 put_page(new_hpage);
956
957 if (result) {
958 if (rc)
959 *result = rc;
960 else
961 *result = page_to_nid(new_hpage);
962 }
963 return rc;
964}
965
966/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800967 * migrate_pages
968 *
Christoph Lameter95a402c2006-06-23 02:03:53 -0700969 * The function takes one list of pages to migrate and a function
970 * that determines from the page to be migrated and the private data
971 * the target of the move and allocates the page.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800972 *
973 * The function returns after 10 attempts or if no pages
974 * are movable anymore because to has become empty
Minchan Kimcf608ac2010-10-26 14:21:29 -0700975 * or no retryable pages exist anymore.
976 * Caller should call putback_lru_pages to return pages to the LRU
Minchan Kim28bd6572011-01-25 15:07:26 -0800977 * or free list only if ret != 0.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800978 *
Christoph Lameter95a402c2006-06-23 02:03:53 -0700979 * Return: Number of pages not migrated or error code.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800980 */
Christoph Lameter95a402c2006-06-23 02:03:53 -0700981int migrate_pages(struct list_head *from,
Mel Gorman7f0f2492011-01-13 15:45:58 -0800982 new_page_t get_new_page, unsigned long private, bool offlining,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800983 enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800984{
Christoph Lametere24f0b82006-06-23 02:03:51 -0700985 int retry = 1;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800986 int nr_failed = 0;
987 int pass = 0;
988 struct page *page;
989 struct page *page2;
990 int swapwrite = current->flags & PF_SWAPWRITE;
991 int rc;
992
993 if (!swapwrite)
994 current->flags |= PF_SWAPWRITE;
995
Christoph Lametere24f0b82006-06-23 02:03:51 -0700996 for(pass = 0; pass < 10 && retry; pass++) {
997 retry = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800998
Christoph Lametere24f0b82006-06-23 02:03:51 -0700999 list_for_each_entry_safe(page, page2, from, lru) {
Christoph Lametere24f0b82006-06-23 02:03:51 -07001000 cond_resched();
Christoph Lameterb20a3502006-03-22 00:09:12 -08001001
Christoph Lameter95a402c2006-06-23 02:03:53 -07001002 rc = unmap_and_move(get_new_page, private,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001003 page, pass > 2, offlining,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001004 mode);
Christoph Lameterb20a3502006-03-22 00:09:12 -08001005
Christoph Lametere24f0b82006-06-23 02:03:51 -07001006 switch(rc) {
Christoph Lameter95a402c2006-06-23 02:03:53 -07001007 case -ENOMEM:
1008 goto out;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001009 case -EAGAIN:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001010 retry++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001011 break;
1012 case 0:
Christoph Lametere24f0b82006-06-23 02:03:51 -07001013 break;
1014 default:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001015 /* Permanent failure */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001016 nr_failed++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001017 break;
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001018 }
Christoph Lameterb20a3502006-03-22 00:09:12 -08001019 }
1020 }
Christoph Lameter95a402c2006-06-23 02:03:53 -07001021 rc = 0;
1022out:
Christoph Lameterb20a3502006-03-22 00:09:12 -08001023 if (!swapwrite)
1024 current->flags &= ~PF_SWAPWRITE;
1025
Christoph Lameter95a402c2006-06-23 02:03:53 -07001026 if (rc)
1027 return rc;
1028
Christoph Lameterb20a3502006-03-22 00:09:12 -08001029 return nr_failed + retry;
1030}
1031
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001032int migrate_huge_pages(struct list_head *from,
Mel Gorman7f0f2492011-01-13 15:45:58 -08001033 new_page_t get_new_page, unsigned long private, bool offlining,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001034 enum migrate_mode mode)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001035{
1036 int retry = 1;
1037 int nr_failed = 0;
1038 int pass = 0;
1039 struct page *page;
1040 struct page *page2;
1041 int rc;
1042
1043 for (pass = 0; pass < 10 && retry; pass++) {
1044 retry = 0;
1045
1046 list_for_each_entry_safe(page, page2, from, lru) {
1047 cond_resched();
1048
1049 rc = unmap_and_move_huge_page(get_new_page,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001050 private, page, pass > 2, offlining,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001051 mode);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001052
1053 switch(rc) {
1054 case -ENOMEM:
1055 goto out;
1056 case -EAGAIN:
1057 retry++;
1058 break;
1059 case 0:
1060 break;
1061 default:
1062 /* Permanent failure */
1063 nr_failed++;
1064 break;
1065 }
1066 }
1067 }
1068 rc = 0;
1069out:
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001070 if (rc)
1071 return rc;
1072
1073 return nr_failed + retry;
1074}
1075
Christoph Lameter742755a2006-06-23 02:03:55 -07001076#ifdef CONFIG_NUMA
1077/*
1078 * Move a list of individual pages
1079 */
1080struct page_to_node {
1081 unsigned long addr;
1082 struct page *page;
1083 int node;
1084 int status;
1085};
1086
1087static struct page *new_page_node(struct page *p, unsigned long private,
1088 int **result)
1089{
1090 struct page_to_node *pm = (struct page_to_node *)private;
1091
1092 while (pm->node != MAX_NUMNODES && pm->page != p)
1093 pm++;
1094
1095 if (pm->node == MAX_NUMNODES)
1096 return NULL;
1097
1098 *result = &pm->status;
1099
Mel Gorman6484eb32009-06-16 15:31:54 -07001100 return alloc_pages_exact_node(pm->node,
Mel Gorman769848c2007-07-17 04:03:05 -07001101 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
Christoph Lameter742755a2006-06-23 02:03:55 -07001102}
1103
1104/*
1105 * Move a set of pages as indicated in the pm array. The addr
1106 * field must be set to the virtual address of the page to be moved
1107 * and the node number must contain a valid target node.
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001108 * The pm array ends with node = MAX_NUMNODES.
Christoph Lameter742755a2006-06-23 02:03:55 -07001109 */
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001110static int do_move_page_to_node_array(struct mm_struct *mm,
1111 struct page_to_node *pm,
1112 int migrate_all)
Christoph Lameter742755a2006-06-23 02:03:55 -07001113{
1114 int err;
1115 struct page_to_node *pp;
1116 LIST_HEAD(pagelist);
1117
1118 down_read(&mm->mmap_sem);
1119
1120 /*
1121 * Build a list of pages to migrate
1122 */
Christoph Lameter742755a2006-06-23 02:03:55 -07001123 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
1124 struct vm_area_struct *vma;
1125 struct page *page;
1126
Christoph Lameter742755a2006-06-23 02:03:55 -07001127 err = -EFAULT;
1128 vma = find_vma(mm, pp->addr);
Gleb Natapov70384dc2010-10-26 14:22:07 -07001129 if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
Christoph Lameter742755a2006-06-23 02:03:55 -07001130 goto set_status;
1131
Andrea Arcangeli500d65d2011-01-13 15:46:55 -08001132 page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001133
1134 err = PTR_ERR(page);
1135 if (IS_ERR(page))
1136 goto set_status;
1137
Christoph Lameter742755a2006-06-23 02:03:55 -07001138 err = -ENOENT;
1139 if (!page)
1140 goto set_status;
1141
Hugh Dickins62b61f62009-12-14 17:59:33 -08001142 /* Use PageReserved to check for zero page */
1143 if (PageReserved(page) || PageKsm(page))
Christoph Lameter742755a2006-06-23 02:03:55 -07001144 goto put_and_set;
1145
1146 pp->page = page;
1147 err = page_to_nid(page);
1148
1149 if (err == pp->node)
1150 /*
1151 * Node already in the right place
1152 */
1153 goto put_and_set;
1154
1155 err = -EACCES;
1156 if (page_mapcount(page) > 1 &&
1157 !migrate_all)
1158 goto put_and_set;
1159
Nick Piggin62695a82008-10-18 20:26:09 -07001160 err = isolate_lru_page(page);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001161 if (!err) {
Nick Piggin62695a82008-10-18 20:26:09 -07001162 list_add_tail(&page->lru, &pagelist);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001163 inc_zone_page_state(page, NR_ISOLATED_ANON +
1164 page_is_file_cache(page));
1165 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001166put_and_set:
1167 /*
1168 * Either remove the duplicate refcount from
1169 * isolate_lru_page() or drop the page ref if it was
1170 * not isolated.
1171 */
1172 put_page(page);
1173set_status:
1174 pp->status = err;
1175 }
1176
Brice Gogline78bbfa2008-10-18 20:27:15 -07001177 err = 0;
Minchan Kimcf608ac2010-10-26 14:21:29 -07001178 if (!list_empty(&pagelist)) {
Christoph Lameter742755a2006-06-23 02:03:55 -07001179 err = migrate_pages(&pagelist, new_page_node,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001180 (unsigned long)pm, 0, MIGRATE_SYNC);
Minchan Kimcf608ac2010-10-26 14:21:29 -07001181 if (err)
1182 putback_lru_pages(&pagelist);
1183 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001184
1185 up_read(&mm->mmap_sem);
1186 return err;
1187}
1188
1189/*
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001190 * Migrate an array of page address onto an array of nodes and fill
1191 * the corresponding array of status.
1192 */
Christoph Lameter3268c632012-03-21 16:34:06 -07001193static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001194 unsigned long nr_pages,
1195 const void __user * __user *pages,
1196 const int __user *nodes,
1197 int __user *status, int flags)
1198{
Brice Goglin3140a222009-01-06 14:38:57 -08001199 struct page_to_node *pm;
Brice Goglin3140a222009-01-06 14:38:57 -08001200 unsigned long chunk_nr_pages;
1201 unsigned long chunk_start;
1202 int err;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001203
Brice Goglin3140a222009-01-06 14:38:57 -08001204 err = -ENOMEM;
1205 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
1206 if (!pm)
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001207 goto out;
Brice Goglin35282a22009-06-16 15:32:43 -07001208
1209 migrate_prep();
1210
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001211 /*
Brice Goglin3140a222009-01-06 14:38:57 -08001212 * Store a chunk of page_to_node array in a page,
1213 * but keep the last one as a marker
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001214 */
Brice Goglin3140a222009-01-06 14:38:57 -08001215 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001216
Brice Goglin3140a222009-01-06 14:38:57 -08001217 for (chunk_start = 0;
1218 chunk_start < nr_pages;
1219 chunk_start += chunk_nr_pages) {
1220 int j;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001221
Brice Goglin3140a222009-01-06 14:38:57 -08001222 if (chunk_start + chunk_nr_pages > nr_pages)
1223 chunk_nr_pages = nr_pages - chunk_start;
1224
1225 /* fill the chunk pm with addrs and nodes from user-space */
1226 for (j = 0; j < chunk_nr_pages; j++) {
1227 const void __user *p;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001228 int node;
1229
Brice Goglin3140a222009-01-06 14:38:57 -08001230 err = -EFAULT;
1231 if (get_user(p, pages + j + chunk_start))
1232 goto out_pm;
1233 pm[j].addr = (unsigned long) p;
1234
1235 if (get_user(node, nodes + j + chunk_start))
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001236 goto out_pm;
1237
1238 err = -ENODEV;
Linus Torvalds6f5a55f2010-02-05 16:16:50 -08001239 if (node < 0 || node >= MAX_NUMNODES)
1240 goto out_pm;
1241
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001242 if (!node_state(node, N_HIGH_MEMORY))
1243 goto out_pm;
1244
1245 err = -EACCES;
1246 if (!node_isset(node, task_nodes))
1247 goto out_pm;
1248
Brice Goglin3140a222009-01-06 14:38:57 -08001249 pm[j].node = node;
1250 }
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001251
Brice Goglin3140a222009-01-06 14:38:57 -08001252 /* End marker for this chunk */
1253 pm[chunk_nr_pages].node = MAX_NUMNODES;
1254
1255 /* Migrate this chunk */
1256 err = do_move_page_to_node_array(mm, pm,
1257 flags & MPOL_MF_MOVE_ALL);
1258 if (err < 0)
1259 goto out_pm;
1260
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001261 /* Return status information */
Brice Goglin3140a222009-01-06 14:38:57 -08001262 for (j = 0; j < chunk_nr_pages; j++)
1263 if (put_user(pm[j].status, status + j + chunk_start)) {
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001264 err = -EFAULT;
Brice Goglin3140a222009-01-06 14:38:57 -08001265 goto out_pm;
1266 }
1267 }
1268 err = 0;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001269
1270out_pm:
Brice Goglin3140a222009-01-06 14:38:57 -08001271 free_page((unsigned long)pm);
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001272out:
1273 return err;
1274}
1275
1276/*
Brice Goglin2f007e72008-10-18 20:27:16 -07001277 * Determine the nodes of an array of pages and store it in an array of status.
Christoph Lameter742755a2006-06-23 02:03:55 -07001278 */
Brice Goglin80bba122008-12-09 13:14:23 -08001279static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1280 const void __user **pages, int *status)
Christoph Lameter742755a2006-06-23 02:03:55 -07001281{
Brice Goglin2f007e72008-10-18 20:27:16 -07001282 unsigned long i;
Brice Goglin2f007e72008-10-18 20:27:16 -07001283
Christoph Lameter742755a2006-06-23 02:03:55 -07001284 down_read(&mm->mmap_sem);
1285
Brice Goglin2f007e72008-10-18 20:27:16 -07001286 for (i = 0; i < nr_pages; i++) {
Brice Goglin80bba122008-12-09 13:14:23 -08001287 unsigned long addr = (unsigned long)(*pages);
Christoph Lameter742755a2006-06-23 02:03:55 -07001288 struct vm_area_struct *vma;
1289 struct page *page;
KOSAKI Motohiroc095adb2008-12-16 16:06:43 +09001290 int err = -EFAULT;
Brice Goglin2f007e72008-10-18 20:27:16 -07001291
1292 vma = find_vma(mm, addr);
Gleb Natapov70384dc2010-10-26 14:22:07 -07001293 if (!vma || addr < vma->vm_start)
Christoph Lameter742755a2006-06-23 02:03:55 -07001294 goto set_status;
1295
Brice Goglin2f007e72008-10-18 20:27:16 -07001296 page = follow_page(vma, addr, 0);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001297
1298 err = PTR_ERR(page);
1299 if (IS_ERR(page))
1300 goto set_status;
1301
Christoph Lameter742755a2006-06-23 02:03:55 -07001302 err = -ENOENT;
1303 /* Use PageReserved to check for zero page */
Hugh Dickins62b61f62009-12-14 17:59:33 -08001304 if (!page || PageReserved(page) || PageKsm(page))
Christoph Lameter742755a2006-06-23 02:03:55 -07001305 goto set_status;
1306
1307 err = page_to_nid(page);
1308set_status:
Brice Goglin80bba122008-12-09 13:14:23 -08001309 *status = err;
1310
1311 pages++;
1312 status++;
1313 }
1314
1315 up_read(&mm->mmap_sem);
1316}
1317
1318/*
1319 * Determine the nodes of a user array of pages and store it in
1320 * a user array of status.
1321 */
1322static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1323 const void __user * __user *pages,
1324 int __user *status)
1325{
1326#define DO_PAGES_STAT_CHUNK_NR 16
1327 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1328 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
Brice Goglin80bba122008-12-09 13:14:23 -08001329
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001330 while (nr_pages) {
1331 unsigned long chunk_nr;
Brice Goglin80bba122008-12-09 13:14:23 -08001332
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001333 chunk_nr = nr_pages;
1334 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1335 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1336
1337 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
1338 break;
Brice Goglin80bba122008-12-09 13:14:23 -08001339
1340 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1341
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001342 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1343 break;
Christoph Lameter742755a2006-06-23 02:03:55 -07001344
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001345 pages += chunk_nr;
1346 status += chunk_nr;
1347 nr_pages -= chunk_nr;
1348 }
1349 return nr_pages ? -EFAULT : 0;
Christoph Lameter742755a2006-06-23 02:03:55 -07001350}
1351
1352/*
1353 * Move a list of pages in the address space of the currently executing
1354 * process.
1355 */
Heiko Carstens938bb9f2009-01-14 14:14:30 +01001356SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1357 const void __user * __user *, pages,
1358 const int __user *, nodes,
1359 int __user *, status, int, flags)
Christoph Lameter742755a2006-06-23 02:03:55 -07001360{
David Howellsc69e8d92008-11-14 10:39:19 +11001361 const struct cred *cred = current_cred(), *tcred;
Christoph Lameter742755a2006-06-23 02:03:55 -07001362 struct task_struct *task;
Christoph Lameter742755a2006-06-23 02:03:55 -07001363 struct mm_struct *mm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001364 int err;
Christoph Lameter3268c632012-03-21 16:34:06 -07001365 nodemask_t task_nodes;
Christoph Lameter742755a2006-06-23 02:03:55 -07001366
1367 /* Check flags */
1368 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1369 return -EINVAL;
1370
1371 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1372 return -EPERM;
1373
1374 /* Find the mm_struct */
Greg Thelena879bf52011-02-25 14:44:13 -08001375 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07001376 task = pid ? find_task_by_vpid(pid) : current;
Christoph Lameter742755a2006-06-23 02:03:55 -07001377 if (!task) {
Greg Thelena879bf52011-02-25 14:44:13 -08001378 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001379 return -ESRCH;
1380 }
Christoph Lameter3268c632012-03-21 16:34:06 -07001381 get_task_struct(task);
Christoph Lameter742755a2006-06-23 02:03:55 -07001382
1383 /*
1384 * Check if this process has the right to modify the specified
1385 * process. The right exists if the process has administrative
1386 * capabilities, superuser privileges or the same
1387 * userid as the target process.
1388 */
David Howellsc69e8d92008-11-14 10:39:19 +11001389 tcred = __task_cred(task);
David Howellsb6dff3e2008-11-14 10:39:16 +11001390 if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
1391 cred->uid != tcred->suid && cred->uid != tcred->uid &&
Christoph Lameter742755a2006-06-23 02:03:55 -07001392 !capable(CAP_SYS_NICE)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001393 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001394 err = -EPERM;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001395 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001396 }
David Howellsc69e8d92008-11-14 10:39:19 +11001397 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001398
David Quigley86c3a762006-06-23 02:04:02 -07001399 err = security_task_movememory(task);
1400 if (err)
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001401 goto out;
David Quigley86c3a762006-06-23 02:04:02 -07001402
Christoph Lameter3268c632012-03-21 16:34:06 -07001403 task_nodes = cpuset_mems_allowed(task);
1404 mm = get_task_mm(task);
1405 put_task_struct(task);
1406
Sasha Levin6e8b09e2012-04-25 16:01:53 -07001407 if (!mm)
1408 return -EINVAL;
1409
1410 if (nodes)
1411 err = do_pages_move(mm, task_nodes, nr_pages, pages,
1412 nodes, status, flags);
1413 else
1414 err = do_pages_stat(mm, nr_pages, pages, status);
Christoph Lameter3268c632012-03-21 16:34:06 -07001415
1416 mmput(mm);
1417 return err;
David Quigley86c3a762006-06-23 02:04:02 -07001418
Christoph Lameter742755a2006-06-23 02:03:55 -07001419out:
Christoph Lameter3268c632012-03-21 16:34:06 -07001420 put_task_struct(task);
Christoph Lameter742755a2006-06-23 02:03:55 -07001421 return err;
1422}
Christoph Lameter742755a2006-06-23 02:03:55 -07001423
Christoph Lameter7b2259b2006-06-25 05:46:48 -07001424/*
1425 * Call migration functions in the vma_ops that may prepare
1426 * memory in a vm for migration. migration functions may perform
1427 * the migration for vmas that do not have an underlying page struct.
1428 */
1429int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1430 const nodemask_t *from, unsigned long flags)
1431{
1432 struct vm_area_struct *vma;
1433 int err = 0;
1434
Daisuke Nishimura1001c9f2009-02-11 13:04:18 -08001435 for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
Christoph Lameter7b2259b2006-06-25 05:46:48 -07001436 if (vma->vm_ops && vma->vm_ops->migrate) {
1437 err = vma->vm_ops->migrate(vma, to, from, flags);
1438 if (err)
1439 break;
1440 }
1441 }
1442 return err;
1443}
Gerald Schaefer83d16742008-07-23 21:28:22 -07001444#endif