blob: 4e86f3bacb852e1a4c09f8e9ad3fa2a68fb2973a [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));
142 if (is_write_migration_entry(entry))
143 pte = pte_mkwrite(pte);
Andi Kleen3ef8fd72010-10-11 16:03:21 +0200144#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900145 if (PageHuge(new))
146 pte = pte_mkhuge(pte);
Andi Kleen3ef8fd72010-10-11 16:03:21 +0200147#endif
KAMEZAWA Hiroyuki97ee0522007-10-16 01:25:43 -0700148 flush_cache_page(vma, addr, pte_pfn(pte));
Christoph Lameter06972122006-06-23 02:03:35 -0700149 set_pte_at(mm, addr, ptep, pte);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700150
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900151 if (PageHuge(new)) {
152 if (PageAnon(new))
153 hugepage_add_anon_rmap(new, vma, addr);
154 else
155 page_dup_rmap(new);
156 } else if (PageAnon(new))
Christoph Lameter04e62a22006-06-23 02:03:38 -0700157 page_add_anon_rmap(new, vma, addr);
158 else
159 page_add_file_rmap(new);
160
161 /* No need to invalidate - it was non-present before */
Russell King4b3073e2009-12-18 16:40:18 +0000162 update_mmu_cache(vma, addr, ptep);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800163unlock:
Christoph Lameter06972122006-06-23 02:03:35 -0700164 pte_unmap_unlock(ptep, ptl);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800165out:
166 return SWAP_AGAIN;
Christoph Lameter06972122006-06-23 02:03:35 -0700167}
168
169/*
Christoph Lameter04e62a22006-06-23 02:03:38 -0700170 * Get rid of all migration entries and replace them by
171 * references to the indicated page.
172 */
173static void remove_migration_ptes(struct page *old, struct page *new)
174{
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800175 rmap_walk(new, remove_migration_pte, old);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700176}
177
178/*
Christoph Lameter06972122006-06-23 02:03:35 -0700179 * Something used the pte of a page under migration. We need to
180 * get to the page and wait until migration is finished.
181 * When we return from this function the fault will be retried.
Christoph Lameter06972122006-06-23 02:03:35 -0700182 */
183void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
184 unsigned long address)
185{
186 pte_t *ptep, pte;
187 spinlock_t *ptl;
188 swp_entry_t entry;
189 struct page *page;
190
191 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
192 pte = *ptep;
193 if (!is_swap_pte(pte))
194 goto out;
195
196 entry = pte_to_swp_entry(pte);
197 if (!is_migration_entry(entry))
198 goto out;
199
200 page = migration_entry_to_page(entry);
201
Nick Piggine2867812008-07-25 19:45:30 -0700202 /*
203 * Once radix-tree replacement of page migration started, page_count
204 * *must* be zero. And, we don't want to call wait_on_page_locked()
205 * against a page without get_page().
206 * So, we use get_page_unless_zero(), here. Even failed, page fault
207 * will occur again.
208 */
209 if (!get_page_unless_zero(page))
210 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700211 pte_unmap_unlock(ptep, ptl);
212 wait_on_page_locked(page);
213 put_page(page);
214 return;
215out:
216 pte_unmap_unlock(ptep, ptl);
217}
218
Mel Gormanb969c4a2012-01-12 17:19:34 -0800219#ifdef CONFIG_BLOCK
220/* Returns true if all buffers are successfully locked */
221static bool buffer_migrate_lock_buffers(struct buffer_head *head, bool sync)
222{
223 struct buffer_head *bh = head;
224
225 /* Simple case, sync compaction */
226 if (sync) {
227 do {
228 get_bh(bh);
229 lock_buffer(bh);
230 bh = bh->b_this_page;
231
232 } while (bh != head);
233
234 return true;
235 }
236
237 /* async case, we cannot block on lock_buffer so use trylock_buffer */
238 do {
239 get_bh(bh);
240 if (!trylock_buffer(bh)) {
241 /*
242 * We failed to lock the buffer and cannot stall in
243 * async migration. Release the taken locks
244 */
245 struct buffer_head *failed_bh = bh;
246 put_bh(failed_bh);
247 bh = head;
248 while (bh != failed_bh) {
249 unlock_buffer(bh);
250 put_bh(bh);
251 bh = bh->b_this_page;
252 }
253 return false;
254 }
255
256 bh = bh->b_this_page;
257 } while (bh != head);
258 return true;
259}
260#else
261static inline bool buffer_migrate_lock_buffers(struct buffer_head *head,
262 bool sync)
263{
264 return true;
265}
266#endif /* CONFIG_BLOCK */
267
Christoph Lameterb20a3502006-03-22 00:09:12 -0800268/*
Christoph Lameterc3fcf8a2006-06-23 02:03:32 -0700269 * Replace the page in the mapping.
Christoph Lameter5b5c7122006-06-23 02:03:29 -0700270 *
271 * The number of remaining references must be:
272 * 1 for anonymous pages without a mapping
273 * 2 for pages with a mapping
David Howells266cf652009-04-03 16:42:36 +0100274 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800275 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700276static int migrate_page_move_mapping(struct address_space *mapping,
Mel Gormanb969c4a2012-01-12 17:19:34 -0800277 struct page *newpage, struct page *page,
278 struct buffer_head *head, bool sync)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800279{
Nick Piggine2867812008-07-25 19:45:30 -0700280 int expected_count;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800281 void **pslot;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800282
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700283 if (!mapping) {
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700284 /* Anonymous page without mapping */
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700285 if (page_count(page) != 1)
286 return -EAGAIN;
287 return 0;
288 }
289
Nick Piggin19fd6232008-07-25 19:45:32 -0700290 spin_lock_irq(&mapping->tree_lock);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800291
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800292 pslot = radix_tree_lookup_slot(&mapping->page_tree,
293 page_index(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800294
Johannes Weineredcf4742009-09-21 17:02:59 -0700295 expected_count = 2 + page_has_private(page);
Nick Piggine2867812008-07-25 19:45:30 -0700296 if (page_count(page) != expected_count ||
Mel Gorman29c1f672011-01-13 15:47:21 -0800297 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700298 spin_unlock_irq(&mapping->tree_lock);
Christoph Lametere23ca002006-04-10 22:52:57 -0700299 return -EAGAIN;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800300 }
301
Nick Piggine2867812008-07-25 19:45:30 -0700302 if (!page_freeze_refs(page, expected_count)) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700303 spin_unlock_irq(&mapping->tree_lock);
Nick Piggine2867812008-07-25 19:45:30 -0700304 return -EAGAIN;
305 }
306
Christoph Lameterb20a3502006-03-22 00:09:12 -0800307 /*
Mel Gormanb969c4a2012-01-12 17:19:34 -0800308 * In the async migration case of moving a page with buffers, lock the
309 * buffers using trylock before the mapping is moved. If the mapping
310 * was moved, we later failed to lock the buffers and could not move
311 * the mapping back due to an elevated page count, we would have to
312 * block waiting on other references to be dropped.
313 */
314 if (!sync && head && !buffer_migrate_lock_buffers(head, sync)) {
315 page_unfreeze_refs(page, expected_count);
316 spin_unlock_irq(&mapping->tree_lock);
317 return -EAGAIN;
318 }
319
320 /*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800321 * Now we know that no one else is looking at the page.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800322 */
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800323 get_page(newpage); /* add cache reference */
Christoph Lameterb20a3502006-03-22 00:09:12 -0800324 if (PageSwapCache(page)) {
325 SetPageSwapCache(newpage);
326 set_page_private(newpage, page_private(page));
327 }
328
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800329 radix_tree_replace_slot(pslot, newpage);
330
331 /*
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800332 * Drop cache reference from old page by unfreezing
333 * to one less reference.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800334 * We know this isn't the last reference.
335 */
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800336 page_unfreeze_refs(page, expected_count - 1);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800337
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700338 /*
339 * If moved to a different zone then also account
340 * the page for that zone. Other VM counters will be
341 * taken care of when we establish references to the
342 * new page and drop references to the old page.
343 *
344 * Note that anonymous pages are accounted for
345 * via NR_FILE_PAGES and NR_ANON_PAGES if they
346 * are mapped to swap space.
347 */
348 __dec_zone_page_state(page, NR_FILE_PAGES);
349 __inc_zone_page_state(newpage, NR_FILE_PAGES);
Andrea Arcangeli99a15e22011-06-16 12:56:19 -0700350 if (!PageSwapCache(page) && PageSwapBacked(page)) {
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700351 __dec_zone_page_state(page, NR_SHMEM);
352 __inc_zone_page_state(newpage, NR_SHMEM);
353 }
Nick Piggin19fd6232008-07-25 19:45:32 -0700354 spin_unlock_irq(&mapping->tree_lock);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800355
356 return 0;
357}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800358
359/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900360 * The expected number of remaining references is the same as that
361 * of migrate_page_move_mapping().
362 */
363int migrate_huge_page_move_mapping(struct address_space *mapping,
364 struct page *newpage, struct page *page)
365{
366 int expected_count;
367 void **pslot;
368
369 if (!mapping) {
370 if (page_count(page) != 1)
371 return -EAGAIN;
372 return 0;
373 }
374
375 spin_lock_irq(&mapping->tree_lock);
376
377 pslot = radix_tree_lookup_slot(&mapping->page_tree,
378 page_index(page));
379
380 expected_count = 2 + page_has_private(page);
381 if (page_count(page) != expected_count ||
Mel Gorman29c1f672011-01-13 15:47:21 -0800382 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900383 spin_unlock_irq(&mapping->tree_lock);
384 return -EAGAIN;
385 }
386
387 if (!page_freeze_refs(page, expected_count)) {
388 spin_unlock_irq(&mapping->tree_lock);
389 return -EAGAIN;
390 }
391
392 get_page(newpage);
393
394 radix_tree_replace_slot(pslot, newpage);
395
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800396 page_unfreeze_refs(page, expected_count - 1);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900397
398 spin_unlock_irq(&mapping->tree_lock);
399 return 0;
400}
401
402/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800403 * Copy the page to its new location
404 */
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900405void migrate_page_copy(struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800406{
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900407 if (PageHuge(page))
408 copy_huge_page(newpage, page);
409 else
410 copy_highpage(newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800411
412 if (PageError(page))
413 SetPageError(newpage);
414 if (PageReferenced(page))
415 SetPageReferenced(newpage);
416 if (PageUptodate(page))
417 SetPageUptodate(newpage);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700418 if (TestClearPageActive(page)) {
419 VM_BUG_ON(PageUnevictable(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800420 SetPageActive(newpage);
Lee Schermerhorn418b27e2009-12-14 17:59:54 -0800421 } else if (TestClearPageUnevictable(page))
422 SetPageUnevictable(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800423 if (PageChecked(page))
424 SetPageChecked(newpage);
425 if (PageMappedToDisk(page))
426 SetPageMappedToDisk(newpage);
427
428 if (PageDirty(page)) {
429 clear_page_dirty_for_io(page);
Nick Piggin3a902c52008-04-30 00:55:16 -0700430 /*
431 * Want to mark the page and the radix tree as dirty, and
432 * redo the accounting that clear_page_dirty_for_io undid,
433 * but we can't use set_page_dirty because that function
434 * is actually a signal that all of the page has become dirty.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300435 * Whereas only part of our page may be dirty.
Nick Piggin3a902c52008-04-30 00:55:16 -0700436 */
437 __set_page_dirty_nobuffers(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800438 }
439
Nick Pigginb291f002008-10-18 20:26:44 -0700440 mlock_migrate_page(newpage, page);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800441 ksm_migrate_page(newpage, page);
Nick Pigginb291f002008-10-18 20:26:44 -0700442
Christoph Lameterb20a3502006-03-22 00:09:12 -0800443 ClearPageSwapCache(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800444 ClearPagePrivate(page);
445 set_page_private(page, 0);
446 page->mapping = NULL;
447
448 /*
449 * If any waiters have accumulated on the new page then
450 * wake them up.
451 */
452 if (PageWriteback(newpage))
453 end_page_writeback(newpage);
454}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800455
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700456/************************************************************
457 * Migration functions
458 ***********************************************************/
459
460/* Always fail migration. Used for mappings that are not movable */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700461int fail_migrate_page(struct address_space *mapping,
462 struct page *newpage, struct page *page)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700463{
464 return -EIO;
465}
466EXPORT_SYMBOL(fail_migrate_page);
467
Christoph Lameterb20a3502006-03-22 00:09:12 -0800468/*
469 * Common logic to directly migrate a single page suitable for
David Howells266cf652009-04-03 16:42:36 +0100470 * pages that do not use PagePrivate/PagePrivate2.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800471 *
472 * Pages are locked upon entry and exit.
473 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700474int migrate_page(struct address_space *mapping,
Mel Gormanb969c4a2012-01-12 17:19:34 -0800475 struct page *newpage, struct page *page, bool sync)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800476{
477 int rc;
478
479 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
480
Mel Gormanb969c4a2012-01-12 17:19:34 -0800481 rc = migrate_page_move_mapping(mapping, newpage, page, NULL, sync);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800482
483 if (rc)
484 return rc;
485
486 migrate_page_copy(newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800487 return 0;
488}
489EXPORT_SYMBOL(migrate_page);
490
David Howells93614012006-09-30 20:45:40 +0200491#ifdef CONFIG_BLOCK
Christoph Lameterb20a3502006-03-22 00:09:12 -0800492/*
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700493 * Migration function for pages with buffers. This function can only be used
494 * if the underlying filesystem guarantees that no other references to "page"
495 * exist.
496 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700497int buffer_migrate_page(struct address_space *mapping,
Mel Gormanb969c4a2012-01-12 17:19:34 -0800498 struct page *newpage, struct page *page, bool sync)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700499{
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700500 struct buffer_head *bh, *head;
501 int rc;
502
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700503 if (!page_has_buffers(page))
Mel Gormanb969c4a2012-01-12 17:19:34 -0800504 return migrate_page(mapping, newpage, page, sync);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700505
506 head = page_buffers(page);
507
Mel Gormanb969c4a2012-01-12 17:19:34 -0800508 rc = migrate_page_move_mapping(mapping, newpage, page, head, sync);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700509
510 if (rc)
511 return rc;
512
Mel Gormanb969c4a2012-01-12 17:19:34 -0800513 /*
514 * In the async case, migrate_page_move_mapping locked the buffers
515 * with an IRQ-safe spinlock held. In the sync case, the buffers
516 * need to be locked now
517 */
518 if (sync)
519 BUG_ON(!buffer_migrate_lock_buffers(head, sync));
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700520
521 ClearPagePrivate(page);
522 set_page_private(newpage, page_private(page));
523 set_page_private(page, 0);
524 put_page(page);
525 get_page(newpage);
526
527 bh = head;
528 do {
529 set_bh_page(bh, newpage, bh_offset(bh));
530 bh = bh->b_this_page;
531
532 } while (bh != head);
533
534 SetPagePrivate(newpage);
535
536 migrate_page_copy(newpage, page);
537
538 bh = head;
539 do {
540 unlock_buffer(bh);
541 put_bh(bh);
542 bh = bh->b_this_page;
543
544 } while (bh != head);
545
546 return 0;
547}
548EXPORT_SYMBOL(buffer_migrate_page);
David Howells93614012006-09-30 20:45:40 +0200549#endif
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700550
Christoph Lameter04e62a22006-06-23 02:03:38 -0700551/*
552 * Writeback a page to clean the dirty state
553 */
554static int writeout(struct address_space *mapping, struct page *page)
555{
556 struct writeback_control wbc = {
557 .sync_mode = WB_SYNC_NONE,
558 .nr_to_write = 1,
559 .range_start = 0,
560 .range_end = LLONG_MAX,
Christoph Lameter04e62a22006-06-23 02:03:38 -0700561 .for_reclaim = 1
562 };
563 int rc;
564
565 if (!mapping->a_ops->writepage)
566 /* No write method for the address space */
567 return -EINVAL;
568
569 if (!clear_page_dirty_for_io(page))
570 /* Someone else already triggered a write */
571 return -EAGAIN;
572
573 /*
574 * A dirty page may imply that the underlying filesystem has
575 * the page on some queue. So the page must be clean for
576 * migration. Writeout may mean we loose the lock and the
577 * page state is no longer what we checked for earlier.
578 * At this point we know that the migration attempt cannot
579 * be successful.
580 */
581 remove_migration_ptes(page, page);
582
583 rc = mapping->a_ops->writepage(page, &wbc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700584
585 if (rc != AOP_WRITEPAGE_ACTIVATE)
586 /* unlocked. Relock */
587 lock_page(page);
588
Hugh Dickinsbda85502008-11-19 15:36:36 -0800589 return (rc < 0) ? -EIO : -EAGAIN;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700590}
591
592/*
593 * Default handling if a filesystem does not provide a migration function.
594 */
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700595static int fallback_migrate_page(struct address_space *mapping,
Mel Gormanb969c4a2012-01-12 17:19:34 -0800596 struct page *newpage, struct page *page, bool sync)
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700597{
Mel Gormanb969c4a2012-01-12 17:19:34 -0800598 if (PageDirty(page)) {
599 if (!sync)
600 return -EBUSY;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700601 return writeout(mapping, page);
Mel Gormanb969c4a2012-01-12 17:19:34 -0800602 }
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700603
604 /*
605 * Buffers may be managed in a filesystem specific way.
606 * We must have no buffers or drop them.
607 */
David Howells266cf652009-04-03 16:42:36 +0100608 if (page_has_private(page) &&
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700609 !try_to_release_page(page, GFP_KERNEL))
610 return -EAGAIN;
611
Mel Gormanb969c4a2012-01-12 17:19:34 -0800612 return migrate_page(mapping, newpage, page, sync);
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700613}
614
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700615/*
Christoph Lametere24f0b82006-06-23 02:03:51 -0700616 * Move a page to a newly allocated page
617 * The page is locked and all ptes have been successfully removed.
618 *
619 * The new page will have replaced the old page if this function
620 * is successful.
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700621 *
622 * Return value:
623 * < 0 - error code
624 * == 0 - success
Christoph Lametere24f0b82006-06-23 02:03:51 -0700625 */
Mel Gorman3fe20112010-05-24 14:32:20 -0700626static int move_to_new_page(struct page *newpage, struct page *page,
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700627 int remap_swapcache, bool sync)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700628{
629 struct address_space *mapping;
630 int rc;
631
632 /*
633 * Block others from accessing the page when we get around to
634 * establishing additional references. We are the only one
635 * holding a reference to the new page at this point.
636 */
Nick Piggin529ae9a2008-08-02 12:01:03 +0200637 if (!trylock_page(newpage))
Christoph Lametere24f0b82006-06-23 02:03:51 -0700638 BUG();
639
640 /* Prepare mapping for the new page.*/
641 newpage->index = page->index;
642 newpage->mapping = page->mapping;
Rik van Rielb2e18532008-10-18 20:26:30 -0700643 if (PageSwapBacked(page))
644 SetPageSwapBacked(newpage);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700645
646 mapping = page_mapping(page);
647 if (!mapping)
Mel Gormanb969c4a2012-01-12 17:19:34 -0800648 rc = migrate_page(mapping, newpage, page, sync);
649 else if (mapping->a_ops->migratepage)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700650 /*
Mel Gormanb969c4a2012-01-12 17:19:34 -0800651 * Most pages have a mapping and most filesystems provide a
652 * migratepage callback. Anonymous pages are part of swap
653 * space which also has its own migratepage callback. This
654 * is the most common path for page migration.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700655 */
Mel Gormanb969c4a2012-01-12 17:19:34 -0800656 rc = mapping->a_ops->migratepage(mapping,
657 newpage, page, sync);
658 else
659 rc = fallback_migrate_page(mapping, newpage, page, sync);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700660
Mel Gorman3fe20112010-05-24 14:32:20 -0700661 if (rc) {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700662 newpage->mapping = NULL;
Mel Gorman3fe20112010-05-24 14:32:20 -0700663 } else {
664 if (remap_swapcache)
665 remove_migration_ptes(page, newpage);
666 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700667
668 unlock_page(newpage);
669
670 return rc;
671}
672
Minchan Kim0dabec92011-10-31 17:06:57 -0700673static int __unmap_and_move(struct page *page, struct page *newpage,
674 int force, bool offlining, bool sync)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700675{
Minchan Kim0dabec92011-10-31 17:06:57 -0700676 int rc = -EAGAIN;
Mel Gorman3fe20112010-05-24 14:32:20 -0700677 int remap_swapcache = 1;
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800678 int charge = 0;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -0700679 struct mem_cgroup *mem;
Mel Gorman3f6c8272010-05-24 14:32:17 -0700680 struct anon_vma *anon_vma = NULL;
Christoph Lameter95a402c2006-06-23 02:03:53 -0700681
Nick Piggin529ae9a2008-08-02 12:01:03 +0200682 if (!trylock_page(page)) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700683 if (!force || !sync)
Minchan Kim0dabec92011-10-31 17:06:57 -0700684 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800685
686 /*
687 * It's not safe for direct compaction to call lock_page.
688 * For example, during page readahead pages are added locked
689 * to the LRU. Later, when the IO completes the pages are
690 * marked uptodate and unlocked. However, the queueing
691 * could be merging multiple pages for one bio (e.g.
692 * mpage_readpages). If an allocation happens for the
693 * second or third page, the process can end up locking
694 * the same page twice and deadlocking. Rather than
695 * trying to be clever about what pages can be locked,
696 * avoid the use of lock_page for direct compaction
697 * altogether.
698 */
699 if (current->flags & PF_MEMALLOC)
Minchan Kim0dabec92011-10-31 17:06:57 -0700700 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800701
Christoph Lametere24f0b82006-06-23 02:03:51 -0700702 lock_page(page);
703 }
704
Hugh Dickins62b61f62009-12-14 17:59:33 -0800705 /*
706 * Only memory hotplug's offline_pages() caller has locked out KSM,
707 * and can safely migrate a KSM page. The other cases have skipped
708 * PageKsm along with PageReserved - but it is only now when we have
709 * the page lock that we can be certain it will not go KSM beneath us
710 * (KSM will not upgrade a page from PageAnon to PageKsm when it sees
711 * its pagecount raised, but only here do we take the page lock which
712 * serializes that).
713 */
714 if (PageKsm(page) && !offlining) {
715 rc = -EBUSY;
716 goto unlock;
717 }
718
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800719 /* charge against new page */
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700720 charge = mem_cgroup_prepare_migration(page, newpage, &mem, GFP_KERNEL);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800721 if (charge == -ENOMEM) {
722 rc = -ENOMEM;
723 goto unlock;
724 }
725 BUG_ON(charge);
726
Christoph Lametere24f0b82006-06-23 02:03:51 -0700727 if (PageWriteback(page)) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700728 /*
729 * For !sync, there is no point retrying as the retry loop
730 * is expected to be too short for PageWriteback to be cleared
731 */
732 if (!sync) {
733 rc = -EBUSY;
734 goto uncharge;
735 }
736 if (!force)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800737 goto uncharge;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700738 wait_on_page_writeback(page);
739 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700740 /*
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700741 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
742 * we cannot notice that anon_vma is freed while we migrates a page.
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800743 * This get_anon_vma() delays freeing anon_vma pointer until the end
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700744 * of migration. File cache pages are no problem because of page_lock()
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700745 * File Caches may use write_page() or lock_page() in migration, then,
746 * just care Anon page here.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700747 */
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700748 if (PageAnon(page)) {
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800749 /*
750 * Only page_lock_anon_vma() understands the subtleties of
751 * getting a hold on an anon_vma from outside one of its mms.
752 */
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700753 anon_vma = page_get_anon_vma(page);
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800754 if (anon_vma) {
755 /*
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700756 * Anon page
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800757 */
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800758 } else if (PageSwapCache(page)) {
Mel Gorman3fe20112010-05-24 14:32:20 -0700759 /*
760 * We cannot be sure that the anon_vma of an unmapped
761 * swapcache page is safe to use because we don't
762 * know in advance if the VMA that this page belonged
763 * to still exists. If the VMA and others sharing the
764 * data have been freed, then the anon_vma could
765 * already be invalid.
766 *
767 * To avoid this possibility, swapcache pages get
768 * migrated but are not remapped when migration
769 * completes
770 */
771 remap_swapcache = 0;
772 } else {
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800773 goto uncharge;
Mel Gorman3fe20112010-05-24 14:32:20 -0700774 }
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700775 }
Shaohua Li62e1c552008-02-04 22:29:33 -0800776
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700777 /*
Shaohua Li62e1c552008-02-04 22:29:33 -0800778 * Corner case handling:
779 * 1. When a new swap-cache page is read into, it is added to the LRU
780 * and treated as swapcache but it has no rmap yet.
781 * Calling try_to_unmap() against a page->mapping==NULL page will
782 * trigger a BUG. So handle it here.
783 * 2. An orphaned page (see truncate_complete_page) might have
784 * fs-private metadata. The page can be picked up due to memory
785 * offlining. Everywhere else except page reclaim, the page is
786 * invisible to the vm, so the page can not be migrated. So try to
787 * free the metadata, so the page can be freed.
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700788 */
Shaohua Li62e1c552008-02-04 22:29:33 -0800789 if (!page->mapping) {
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800790 VM_BUG_ON(PageAnon(page));
791 if (page_has_private(page)) {
Shaohua Li62e1c552008-02-04 22:29:33 -0800792 try_to_free_buffers(page);
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800793 goto uncharge;
Shaohua Li62e1c552008-02-04 22:29:33 -0800794 }
Shaohua Liabfc3482009-09-21 17:01:19 -0700795 goto skip_unmap;
Shaohua Li62e1c552008-02-04 22:29:33 -0800796 }
797
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700798 /* Establish migration ptes or remove ptes */
Andi Kleen14fa31b2009-09-16 11:50:10 +0200799 try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700800
Shaohua Liabfc3482009-09-21 17:01:19 -0700801skip_unmap:
Christoph Lametere6a15302006-06-25 05:46:49 -0700802 if (!page_mapped(page))
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700803 rc = move_to_new_page(newpage, page, remap_swapcache, sync);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700804
Mel Gorman3fe20112010-05-24 14:32:20 -0700805 if (rc && remap_swapcache)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700806 remove_migration_ptes(page, page);
Mel Gorman3f6c8272010-05-24 14:32:17 -0700807
808 /* Drop an anon_vma reference if we took one */
Rik van Riel76545062010-08-09 17:18:41 -0700809 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -0700810 put_anon_vma(anon_vma);
Mel Gorman3f6c8272010-05-24 14:32:17 -0700811
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800812uncharge:
813 if (!charge)
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -0800814 mem_cgroup_end_migration(mem, page, newpage, rc == 0);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700815unlock:
816 unlock_page(page);
Minchan Kim0dabec92011-10-31 17:06:57 -0700817out:
818 return rc;
819}
Christoph Lameter95a402c2006-06-23 02:03:53 -0700820
Minchan Kim0dabec92011-10-31 17:06:57 -0700821/*
822 * Obtain the lock on page, remove all ptes and migrate the page
823 * to the newly allocated page in newpage.
824 */
825static int unmap_and_move(new_page_t get_new_page, unsigned long private,
826 struct page *page, int force, bool offlining, bool sync)
827{
828 int rc = 0;
829 int *result = NULL;
830 struct page *newpage = get_new_page(page, private, &result);
831
832 if (!newpage)
833 return -ENOMEM;
834
KAMEZAWA Hiroyuki4e5f01c2012-01-12 17:18:58 -0800835 mem_cgroup_reset_owner(newpage);
836
Minchan Kim0dabec92011-10-31 17:06:57 -0700837 if (page_count(page) == 1) {
838 /* page was freed from under us. So we are done. */
839 goto out;
840 }
841
842 if (unlikely(PageTransHuge(page)))
843 if (unlikely(split_huge_page(page)))
844 goto out;
845
846 rc = __unmap_and_move(page, newpage, force, offlining, sync);
847out:
Christoph Lametere24f0b82006-06-23 02:03:51 -0700848 if (rc != -EAGAIN) {
Minchan Kim0dabec92011-10-31 17:06:57 -0700849 /*
850 * A page that has been migrated has all references
851 * removed and will be freed. A page that has not been
852 * migrated will have kepts its references and be
853 * restored.
854 */
855 list_del(&page->lru);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -0700856 dec_zone_page_state(page, NR_ISOLATED_ANON +
Johannes Weiner6c0b1352009-09-21 17:02:59 -0700857 page_is_file_cache(page));
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700858 putback_lru_page(page);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700859 }
Christoph Lameter95a402c2006-06-23 02:03:53 -0700860 /*
861 * Move the new page to the LRU. If migration was not successful
862 * then this will free the page.
863 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700864 putback_lru_page(newpage);
Christoph Lameter742755a2006-06-23 02:03:55 -0700865 if (result) {
866 if (rc)
867 *result = rc;
868 else
869 *result = page_to_nid(newpage);
870 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700871 return rc;
872}
873
874/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900875 * Counterpart of unmap_and_move_page() for hugepage migration.
876 *
877 * This function doesn't wait the completion of hugepage I/O
878 * because there is no race between I/O and migration for hugepage.
879 * Note that currently hugepage I/O occurs only in direct I/O
880 * where no lock is held and PG_writeback is irrelevant,
881 * and writeback status of all subpages are counted in the reference
882 * count of the head page (i.e. if all subpages of a 2MB hugepage are
883 * under direct I/O, the reference of the head page is 512 and a bit more.)
884 * This means that when we try to migrate hugepage whose subpages are
885 * doing direct I/O, some references remain after try_to_unmap() and
886 * hugepage migration fails without data corruption.
887 *
888 * There is also no race when direct I/O is issued on the page under migration,
889 * because then pte is replaced with migration swap entry and direct I/O code
890 * will wait in the page fault for migration to complete.
891 */
892static int unmap_and_move_huge_page(new_page_t get_new_page,
893 unsigned long private, struct page *hpage,
Mel Gorman7f0f2492011-01-13 15:45:58 -0800894 int force, bool offlining, bool sync)
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900895{
896 int rc = 0;
897 int *result = NULL;
898 struct page *new_hpage = get_new_page(hpage, private, &result);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900899 struct anon_vma *anon_vma = NULL;
900
901 if (!new_hpage)
902 return -ENOMEM;
903
904 rc = -EAGAIN;
905
906 if (!trylock_page(hpage)) {
Mel Gorman77f1fe62011-01-13 15:45:57 -0800907 if (!force || !sync)
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900908 goto out;
909 lock_page(hpage);
910 }
911
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700912 if (PageAnon(hpage))
913 anon_vma = page_get_anon_vma(hpage);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900914
915 try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
916
917 if (!page_mapped(hpage))
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700918 rc = move_to_new_page(new_hpage, hpage, 1, sync);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900919
920 if (rc)
921 remove_migration_ptes(hpage, hpage);
922
Hugh Dickinsfd4a4662011-01-13 15:47:31 -0800923 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -0700924 put_anon_vma(anon_vma);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900925 unlock_page(hpage);
926
Hillf Danton09761332011-12-08 14:34:20 -0800927out:
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900928 if (rc != -EAGAIN) {
929 list_del(&hpage->lru);
930 put_page(hpage);
931 }
932
933 put_page(new_hpage);
934
935 if (result) {
936 if (rc)
937 *result = rc;
938 else
939 *result = page_to_nid(new_hpage);
940 }
941 return rc;
942}
943
944/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800945 * migrate_pages
946 *
Christoph Lameter95a402c2006-06-23 02:03:53 -0700947 * The function takes one list of pages to migrate and a function
948 * that determines from the page to be migrated and the private data
949 * the target of the move and allocates the page.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800950 *
951 * The function returns after 10 attempts or if no pages
952 * are movable anymore because to has become empty
Minchan Kimcf608ac2010-10-26 14:21:29 -0700953 * or no retryable pages exist anymore.
954 * Caller should call putback_lru_pages to return pages to the LRU
Minchan Kim28bd6572011-01-25 15:07:26 -0800955 * or free list only if ret != 0.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800956 *
Christoph Lameter95a402c2006-06-23 02:03:53 -0700957 * Return: Number of pages not migrated or error code.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800958 */
Christoph Lameter95a402c2006-06-23 02:03:53 -0700959int migrate_pages(struct list_head *from,
Mel Gorman7f0f2492011-01-13 15:45:58 -0800960 new_page_t get_new_page, unsigned long private, bool offlining,
Mel Gorman77f1fe62011-01-13 15:45:57 -0800961 bool sync)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800962{
Christoph Lametere24f0b82006-06-23 02:03:51 -0700963 int retry = 1;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800964 int nr_failed = 0;
965 int pass = 0;
966 struct page *page;
967 struct page *page2;
968 int swapwrite = current->flags & PF_SWAPWRITE;
969 int rc;
970
971 if (!swapwrite)
972 current->flags |= PF_SWAPWRITE;
973
Christoph Lametere24f0b82006-06-23 02:03:51 -0700974 for(pass = 0; pass < 10 && retry; pass++) {
975 retry = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800976
Christoph Lametere24f0b82006-06-23 02:03:51 -0700977 list_for_each_entry_safe(page, page2, from, lru) {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700978 cond_resched();
Christoph Lameterb20a3502006-03-22 00:09:12 -0800979
Christoph Lameter95a402c2006-06-23 02:03:53 -0700980 rc = unmap_and_move(get_new_page, private,
Mel Gorman77f1fe62011-01-13 15:45:57 -0800981 page, pass > 2, offlining,
982 sync);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800983
Christoph Lametere24f0b82006-06-23 02:03:51 -0700984 switch(rc) {
Christoph Lameter95a402c2006-06-23 02:03:53 -0700985 case -ENOMEM:
986 goto out;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700987 case -EAGAIN:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700988 retry++;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700989 break;
990 case 0:
Christoph Lametere24f0b82006-06-23 02:03:51 -0700991 break;
992 default:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700993 /* Permanent failure */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700994 nr_failed++;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700995 break;
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700996 }
Christoph Lameterb20a3502006-03-22 00:09:12 -0800997 }
998 }
Christoph Lameter95a402c2006-06-23 02:03:53 -0700999 rc = 0;
1000out:
Christoph Lameterb20a3502006-03-22 00:09:12 -08001001 if (!swapwrite)
1002 current->flags &= ~PF_SWAPWRITE;
1003
Christoph Lameter95a402c2006-06-23 02:03:53 -07001004 if (rc)
1005 return rc;
1006
Christoph Lameterb20a3502006-03-22 00:09:12 -08001007 return nr_failed + retry;
1008}
1009
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001010int migrate_huge_pages(struct list_head *from,
Mel Gorman7f0f2492011-01-13 15:45:58 -08001011 new_page_t get_new_page, unsigned long private, bool offlining,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001012 bool sync)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001013{
1014 int retry = 1;
1015 int nr_failed = 0;
1016 int pass = 0;
1017 struct page *page;
1018 struct page *page2;
1019 int rc;
1020
1021 for (pass = 0; pass < 10 && retry; pass++) {
1022 retry = 0;
1023
1024 list_for_each_entry_safe(page, page2, from, lru) {
1025 cond_resched();
1026
1027 rc = unmap_and_move_huge_page(get_new_page,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001028 private, page, pass > 2, offlining,
1029 sync);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001030
1031 switch(rc) {
1032 case -ENOMEM:
1033 goto out;
1034 case -EAGAIN:
1035 retry++;
1036 break;
1037 case 0:
1038 break;
1039 default:
1040 /* Permanent failure */
1041 nr_failed++;
1042 break;
1043 }
1044 }
1045 }
1046 rc = 0;
1047out:
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001048 if (rc)
1049 return rc;
1050
1051 return nr_failed + retry;
1052}
1053
Christoph Lameter742755a2006-06-23 02:03:55 -07001054#ifdef CONFIG_NUMA
1055/*
1056 * Move a list of individual pages
1057 */
1058struct page_to_node {
1059 unsigned long addr;
1060 struct page *page;
1061 int node;
1062 int status;
1063};
1064
1065static struct page *new_page_node(struct page *p, unsigned long private,
1066 int **result)
1067{
1068 struct page_to_node *pm = (struct page_to_node *)private;
1069
1070 while (pm->node != MAX_NUMNODES && pm->page != p)
1071 pm++;
1072
1073 if (pm->node == MAX_NUMNODES)
1074 return NULL;
1075
1076 *result = &pm->status;
1077
Mel Gorman6484eb32009-06-16 15:31:54 -07001078 return alloc_pages_exact_node(pm->node,
Mel Gorman769848c2007-07-17 04:03:05 -07001079 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
Christoph Lameter742755a2006-06-23 02:03:55 -07001080}
1081
1082/*
1083 * Move a set of pages as indicated in the pm array. The addr
1084 * field must be set to the virtual address of the page to be moved
1085 * and the node number must contain a valid target node.
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001086 * The pm array ends with node = MAX_NUMNODES.
Christoph Lameter742755a2006-06-23 02:03:55 -07001087 */
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001088static int do_move_page_to_node_array(struct mm_struct *mm,
1089 struct page_to_node *pm,
1090 int migrate_all)
Christoph Lameter742755a2006-06-23 02:03:55 -07001091{
1092 int err;
1093 struct page_to_node *pp;
1094 LIST_HEAD(pagelist);
1095
1096 down_read(&mm->mmap_sem);
1097
1098 /*
1099 * Build a list of pages to migrate
1100 */
Christoph Lameter742755a2006-06-23 02:03:55 -07001101 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
1102 struct vm_area_struct *vma;
1103 struct page *page;
1104
Christoph Lameter742755a2006-06-23 02:03:55 -07001105 err = -EFAULT;
1106 vma = find_vma(mm, pp->addr);
Gleb Natapov70384dc2010-10-26 14:22:07 -07001107 if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
Christoph Lameter742755a2006-06-23 02:03:55 -07001108 goto set_status;
1109
Andrea Arcangeli500d65d2011-01-13 15:46:55 -08001110 page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001111
1112 err = PTR_ERR(page);
1113 if (IS_ERR(page))
1114 goto set_status;
1115
Christoph Lameter742755a2006-06-23 02:03:55 -07001116 err = -ENOENT;
1117 if (!page)
1118 goto set_status;
1119
Hugh Dickins62b61f62009-12-14 17:59:33 -08001120 /* Use PageReserved to check for zero page */
1121 if (PageReserved(page) || PageKsm(page))
Christoph Lameter742755a2006-06-23 02:03:55 -07001122 goto put_and_set;
1123
1124 pp->page = page;
1125 err = page_to_nid(page);
1126
1127 if (err == pp->node)
1128 /*
1129 * Node already in the right place
1130 */
1131 goto put_and_set;
1132
1133 err = -EACCES;
1134 if (page_mapcount(page) > 1 &&
1135 !migrate_all)
1136 goto put_and_set;
1137
Nick Piggin62695a82008-10-18 20:26:09 -07001138 err = isolate_lru_page(page);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001139 if (!err) {
Nick Piggin62695a82008-10-18 20:26:09 -07001140 list_add_tail(&page->lru, &pagelist);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001141 inc_zone_page_state(page, NR_ISOLATED_ANON +
1142 page_is_file_cache(page));
1143 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001144put_and_set:
1145 /*
1146 * Either remove the duplicate refcount from
1147 * isolate_lru_page() or drop the page ref if it was
1148 * not isolated.
1149 */
1150 put_page(page);
1151set_status:
1152 pp->status = err;
1153 }
1154
Brice Gogline78bbfa2008-10-18 20:27:15 -07001155 err = 0;
Minchan Kimcf608ac2010-10-26 14:21:29 -07001156 if (!list_empty(&pagelist)) {
Christoph Lameter742755a2006-06-23 02:03:55 -07001157 err = migrate_pages(&pagelist, new_page_node,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001158 (unsigned long)pm, 0, true);
Minchan Kimcf608ac2010-10-26 14:21:29 -07001159 if (err)
1160 putback_lru_pages(&pagelist);
1161 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001162
1163 up_read(&mm->mmap_sem);
1164 return err;
1165}
1166
1167/*
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001168 * Migrate an array of page address onto an array of nodes and fill
1169 * the corresponding array of status.
1170 */
1171static int do_pages_move(struct mm_struct *mm, struct task_struct *task,
1172 unsigned long nr_pages,
1173 const void __user * __user *pages,
1174 const int __user *nodes,
1175 int __user *status, int flags)
1176{
Brice Goglin3140a222009-01-06 14:38:57 -08001177 struct page_to_node *pm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001178 nodemask_t task_nodes;
Brice Goglin3140a222009-01-06 14:38:57 -08001179 unsigned long chunk_nr_pages;
1180 unsigned long chunk_start;
1181 int err;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001182
1183 task_nodes = cpuset_mems_allowed(task);
1184
Brice Goglin3140a222009-01-06 14:38:57 -08001185 err = -ENOMEM;
1186 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
1187 if (!pm)
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001188 goto out;
Brice Goglin35282a22009-06-16 15:32:43 -07001189
1190 migrate_prep();
1191
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001192 /*
Brice Goglin3140a222009-01-06 14:38:57 -08001193 * Store a chunk of page_to_node array in a page,
1194 * but keep the last one as a marker
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001195 */
Brice Goglin3140a222009-01-06 14:38:57 -08001196 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001197
Brice Goglin3140a222009-01-06 14:38:57 -08001198 for (chunk_start = 0;
1199 chunk_start < nr_pages;
1200 chunk_start += chunk_nr_pages) {
1201 int j;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001202
Brice Goglin3140a222009-01-06 14:38:57 -08001203 if (chunk_start + chunk_nr_pages > nr_pages)
1204 chunk_nr_pages = nr_pages - chunk_start;
1205
1206 /* fill the chunk pm with addrs and nodes from user-space */
1207 for (j = 0; j < chunk_nr_pages; j++) {
1208 const void __user *p;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001209 int node;
1210
Brice Goglin3140a222009-01-06 14:38:57 -08001211 err = -EFAULT;
1212 if (get_user(p, pages + j + chunk_start))
1213 goto out_pm;
1214 pm[j].addr = (unsigned long) p;
1215
1216 if (get_user(node, nodes + j + chunk_start))
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001217 goto out_pm;
1218
1219 err = -ENODEV;
Linus Torvalds6f5a55f2010-02-05 16:16:50 -08001220 if (node < 0 || node >= MAX_NUMNODES)
1221 goto out_pm;
1222
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001223 if (!node_state(node, N_HIGH_MEMORY))
1224 goto out_pm;
1225
1226 err = -EACCES;
1227 if (!node_isset(node, task_nodes))
1228 goto out_pm;
1229
Brice Goglin3140a222009-01-06 14:38:57 -08001230 pm[j].node = node;
1231 }
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001232
Brice Goglin3140a222009-01-06 14:38:57 -08001233 /* End marker for this chunk */
1234 pm[chunk_nr_pages].node = MAX_NUMNODES;
1235
1236 /* Migrate this chunk */
1237 err = do_move_page_to_node_array(mm, pm,
1238 flags & MPOL_MF_MOVE_ALL);
1239 if (err < 0)
1240 goto out_pm;
1241
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001242 /* Return status information */
Brice Goglin3140a222009-01-06 14:38:57 -08001243 for (j = 0; j < chunk_nr_pages; j++)
1244 if (put_user(pm[j].status, status + j + chunk_start)) {
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001245 err = -EFAULT;
Brice Goglin3140a222009-01-06 14:38:57 -08001246 goto out_pm;
1247 }
1248 }
1249 err = 0;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001250
1251out_pm:
Brice Goglin3140a222009-01-06 14:38:57 -08001252 free_page((unsigned long)pm);
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001253out:
1254 return err;
1255}
1256
1257/*
Brice Goglin2f007e72008-10-18 20:27:16 -07001258 * Determine the nodes of an array of pages and store it in an array of status.
Christoph Lameter742755a2006-06-23 02:03:55 -07001259 */
Brice Goglin80bba122008-12-09 13:14:23 -08001260static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1261 const void __user **pages, int *status)
Christoph Lameter742755a2006-06-23 02:03:55 -07001262{
Brice Goglin2f007e72008-10-18 20:27:16 -07001263 unsigned long i;
Brice Goglin2f007e72008-10-18 20:27:16 -07001264
Christoph Lameter742755a2006-06-23 02:03:55 -07001265 down_read(&mm->mmap_sem);
1266
Brice Goglin2f007e72008-10-18 20:27:16 -07001267 for (i = 0; i < nr_pages; i++) {
Brice Goglin80bba122008-12-09 13:14:23 -08001268 unsigned long addr = (unsigned long)(*pages);
Christoph Lameter742755a2006-06-23 02:03:55 -07001269 struct vm_area_struct *vma;
1270 struct page *page;
KOSAKI Motohiroc095adb2008-12-16 16:06:43 +09001271 int err = -EFAULT;
Brice Goglin2f007e72008-10-18 20:27:16 -07001272
1273 vma = find_vma(mm, addr);
Gleb Natapov70384dc2010-10-26 14:22:07 -07001274 if (!vma || addr < vma->vm_start)
Christoph Lameter742755a2006-06-23 02:03:55 -07001275 goto set_status;
1276
Brice Goglin2f007e72008-10-18 20:27:16 -07001277 page = follow_page(vma, addr, 0);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001278
1279 err = PTR_ERR(page);
1280 if (IS_ERR(page))
1281 goto set_status;
1282
Christoph Lameter742755a2006-06-23 02:03:55 -07001283 err = -ENOENT;
1284 /* Use PageReserved to check for zero page */
Hugh Dickins62b61f62009-12-14 17:59:33 -08001285 if (!page || PageReserved(page) || PageKsm(page))
Christoph Lameter742755a2006-06-23 02:03:55 -07001286 goto set_status;
1287
1288 err = page_to_nid(page);
1289set_status:
Brice Goglin80bba122008-12-09 13:14:23 -08001290 *status = err;
1291
1292 pages++;
1293 status++;
1294 }
1295
1296 up_read(&mm->mmap_sem);
1297}
1298
1299/*
1300 * Determine the nodes of a user array of pages and store it in
1301 * a user array of status.
1302 */
1303static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1304 const void __user * __user *pages,
1305 int __user *status)
1306{
1307#define DO_PAGES_STAT_CHUNK_NR 16
1308 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1309 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
Brice Goglin80bba122008-12-09 13:14:23 -08001310
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001311 while (nr_pages) {
1312 unsigned long chunk_nr;
Brice Goglin80bba122008-12-09 13:14:23 -08001313
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001314 chunk_nr = nr_pages;
1315 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1316 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1317
1318 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
1319 break;
Brice Goglin80bba122008-12-09 13:14:23 -08001320
1321 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1322
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001323 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1324 break;
Christoph Lameter742755a2006-06-23 02:03:55 -07001325
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001326 pages += chunk_nr;
1327 status += chunk_nr;
1328 nr_pages -= chunk_nr;
1329 }
1330 return nr_pages ? -EFAULT : 0;
Christoph Lameter742755a2006-06-23 02:03:55 -07001331}
1332
1333/*
1334 * Move a list of pages in the address space of the currently executing
1335 * process.
1336 */
Heiko Carstens938bb9f2009-01-14 14:14:30 +01001337SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1338 const void __user * __user *, pages,
1339 const int __user *, nodes,
1340 int __user *, status, int, flags)
Christoph Lameter742755a2006-06-23 02:03:55 -07001341{
David Howellsc69e8d92008-11-14 10:39:19 +11001342 const struct cred *cred = current_cred(), *tcred;
Christoph Lameter742755a2006-06-23 02:03:55 -07001343 struct task_struct *task;
Christoph Lameter742755a2006-06-23 02:03:55 -07001344 struct mm_struct *mm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001345 int err;
Christoph Lameter742755a2006-06-23 02:03:55 -07001346
1347 /* Check flags */
1348 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1349 return -EINVAL;
1350
1351 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1352 return -EPERM;
1353
1354 /* Find the mm_struct */
Greg Thelena879bf52011-02-25 14:44:13 -08001355 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07001356 task = pid ? find_task_by_vpid(pid) : current;
Christoph Lameter742755a2006-06-23 02:03:55 -07001357 if (!task) {
Greg Thelena879bf52011-02-25 14:44:13 -08001358 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001359 return -ESRCH;
1360 }
1361 mm = get_task_mm(task);
Greg Thelena879bf52011-02-25 14:44:13 -08001362 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001363
1364 if (!mm)
1365 return -EINVAL;
1366
1367 /*
1368 * Check if this process has the right to modify the specified
1369 * process. The right exists if the process has administrative
1370 * capabilities, superuser privileges or the same
1371 * userid as the target process.
1372 */
David Howellsc69e8d92008-11-14 10:39:19 +11001373 rcu_read_lock();
1374 tcred = __task_cred(task);
David Howellsb6dff3e2008-11-14 10:39:16 +11001375 if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
1376 cred->uid != tcred->suid && cred->uid != tcred->uid &&
Christoph Lameter742755a2006-06-23 02:03:55 -07001377 !capable(CAP_SYS_NICE)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001378 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001379 err = -EPERM;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001380 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001381 }
David Howellsc69e8d92008-11-14 10:39:19 +11001382 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001383
David Quigley86c3a762006-06-23 02:04:02 -07001384 err = security_task_movememory(task);
1385 if (err)
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001386 goto out;
David Quigley86c3a762006-06-23 02:04:02 -07001387
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001388 if (nodes) {
1389 err = do_pages_move(mm, task, nr_pages, pages, nodes, status,
1390 flags);
1391 } else {
Brice Goglin2f007e72008-10-18 20:27:16 -07001392 err = do_pages_stat(mm, nr_pages, pages, status);
Brice Goglin2f007e72008-10-18 20:27:16 -07001393 }
David Quigley86c3a762006-06-23 02:04:02 -07001394
Christoph Lameter742755a2006-06-23 02:03:55 -07001395out:
Christoph Lameter742755a2006-06-23 02:03:55 -07001396 mmput(mm);
1397 return err;
1398}
Christoph Lameter742755a2006-06-23 02:03:55 -07001399
Christoph Lameter7b2259b2006-06-25 05:46:48 -07001400/*
1401 * Call migration functions in the vma_ops that may prepare
1402 * memory in a vm for migration. migration functions may perform
1403 * the migration for vmas that do not have an underlying page struct.
1404 */
1405int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1406 const nodemask_t *from, unsigned long flags)
1407{
1408 struct vm_area_struct *vma;
1409 int err = 0;
1410
Daisuke Nishimura1001c9f2009-02-11 13:04:18 -08001411 for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
Christoph Lameter7b2259b2006-06-25 05:46:48 -07001412 if (vma->vm_ops && vma->vm_ops->migrate) {
1413 err = vma->vm_ops->migrate(vma, to, from, flags);
1414 if (err)
1415 break;
1416 }
1417 }
1418 return err;
1419}
Gerald Schaefer83d16742008-07-23 21:28:22 -07001420#endif