blob: 99ff32a2e5390bc07e3ae3096543d3a6a0822c95 [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>
Liam Mark069398a2013-01-16 10:14:40 -080037#include <trace/events/kmem.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080038
Michal Nazarewicz0d1836c2010-12-21 17:24:26 -080039#include <asm/tlbflush.h>
40
Christoph Lameterb20a3502006-03-22 00:09:12 -080041#include "internal.h"
42
Christoph Lameterb20a3502006-03-22 00:09:12 -080043/*
Christoph Lameter742755a2006-06-23 02:03:55 -070044 * migrate_prep() needs to be called before we start compiling a list of pages
Mel Gorman748446b2010-05-24 14:32:27 -070045 * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is
46 * undesirable, use migrate_prep_local()
Christoph Lameterb20a3502006-03-22 00:09:12 -080047 */
48int migrate_prep(void)
49{
Christoph Lameterb20a3502006-03-22 00:09:12 -080050 /*
51 * Clear the LRU lists so pages can be isolated.
52 * Note that pages may be moved off the LRU after we have
53 * drained them. Those pages will fail to migrate like other
54 * pages that may be busy.
55 */
56 lru_add_drain_all();
57
58 return 0;
59}
60
Mel Gorman748446b2010-05-24 14:32:27 -070061/* Do the necessary work of migrate_prep but not if it involves other CPUs */
62int migrate_prep_local(void)
63{
64 lru_add_drain();
65
66 return 0;
67}
68
Christoph Lameterb20a3502006-03-22 00:09:12 -080069/*
Lee Schermerhorn894bc312008-10-18 20:26:39 -070070 * Add isolated pages on the list back to the LRU under page lock
71 * to avoid leaking evictable pages back onto unevictable list.
Christoph Lameterb20a3502006-03-22 00:09:12 -080072 */
Minchan Kime13861d2010-05-24 14:31:59 -070073void putback_lru_pages(struct list_head *l)
Christoph Lameterb20a3502006-03-22 00:09:12 -080074{
75 struct page *page;
76 struct page *page2;
Christoph Lameterb20a3502006-03-22 00:09:12 -080077
78 list_for_each_entry_safe(page, page2, l, lru) {
Christoph Lametere24f0b82006-06-23 02:03:51 -070079 list_del(&page->lru);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -070080 dec_zone_page_state(page, NR_ISOLATED_ANON +
Johannes Weiner6c0b1352009-09-21 17:02:59 -070081 page_is_file_cache(page));
Lee Schermerhorn894bc312008-10-18 20:26:39 -070082 putback_lru_page(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -080083 }
Christoph Lameterb20a3502006-03-22 00:09:12 -080084}
85
Christoph Lameter06972122006-06-23 02:03:35 -070086/*
87 * Restore a potential migration pte to a working pte entry
88 */
Hugh Dickinse9995ef2009-12-14 17:59:31 -080089static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,
90 unsigned long addr, void *old)
Christoph Lameter06972122006-06-23 02:03:35 -070091{
92 struct mm_struct *mm = vma->vm_mm;
93 swp_entry_t entry;
94 pgd_t *pgd;
95 pud_t *pud;
96 pmd_t *pmd;
97 pte_t *ptep, pte;
98 spinlock_t *ptl;
99
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900100 if (unlikely(PageHuge(new))) {
101 ptep = huge_pte_offset(mm, addr);
102 if (!ptep)
103 goto out;
104 ptl = &mm->page_table_lock;
105 } else {
106 pgd = pgd_offset(mm, addr);
107 if (!pgd_present(*pgd))
108 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700109
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900110 pud = pud_offset(pgd, addr);
111 if (!pud_present(*pud))
112 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700113
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900114 pmd = pmd_offset(pud, addr);
Andrea Arcangeli500d65d2011-01-13 15:46:55 -0800115 if (pmd_trans_huge(*pmd))
116 goto out;
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900117 if (!pmd_present(*pmd))
118 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700119
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900120 ptep = pte_offset_map(pmd, addr);
Christoph Lameter06972122006-06-23 02:03:35 -0700121
Hugh Dickins486cf462011-10-19 12:50:35 -0700122 /*
123 * Peek to check is_swap_pte() before taking ptlock? No, we
124 * can race mremap's move_ptes(), which skips anon_vma lock.
125 */
Christoph Lameter06972122006-06-23 02:03:35 -0700126
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900127 ptl = pte_lockptr(mm, pmd);
128 }
129
Christoph Lameter06972122006-06-23 02:03:35 -0700130 spin_lock(ptl);
131 pte = *ptep;
132 if (!is_swap_pte(pte))
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800133 goto unlock;
Christoph Lameter06972122006-06-23 02:03:35 -0700134
135 entry = pte_to_swp_entry(pte);
136
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800137 if (!is_migration_entry(entry) ||
138 migration_entry_to_page(entry) != old)
139 goto unlock;
Christoph Lameter06972122006-06-23 02:03:35 -0700140
Christoph Lameter06972122006-06-23 02:03:35 -0700141 get_page(new);
142 pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
143 if (is_write_migration_entry(entry))
144 pte = pte_mkwrite(pte);
Andi Kleen3ef8fd72010-10-11 16:03:21 +0200145#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900146 if (PageHuge(new))
147 pte = pte_mkhuge(pte);
Andi Kleen3ef8fd72010-10-11 16:03:21 +0200148#endif
Leonid Yegoshin11e88c52013-05-24 15:55:18 -0700149 flush_dcache_page(new);
Christoph Lameter06972122006-06-23 02:03:35 -0700150 set_pte_at(mm, addr, ptep, pte);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700151
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900152 if (PageHuge(new)) {
153 if (PageAnon(new))
154 hugepage_add_anon_rmap(new, vma, addr);
155 else
156 page_dup_rmap(new);
157 } else if (PageAnon(new))
Christoph Lameter04e62a22006-06-23 02:03:38 -0700158 page_add_anon_rmap(new, vma, addr);
159 else
160 page_add_file_rmap(new);
161
162 /* No need to invalidate - it was non-present before */
Russell King4b3073e2009-12-18 16:40:18 +0000163 update_mmu_cache(vma, addr, ptep);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800164unlock:
Christoph Lameter06972122006-06-23 02:03:35 -0700165 pte_unmap_unlock(ptep, ptl);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800166out:
167 return SWAP_AGAIN;
Christoph Lameter06972122006-06-23 02:03:35 -0700168}
169
170/*
Christoph Lameter04e62a22006-06-23 02:03:38 -0700171 * Get rid of all migration entries and replace them by
172 * references to the indicated page.
173 */
174static void remove_migration_ptes(struct page *old, struct page *new)
175{
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800176 rmap_walk(new, remove_migration_pte, old);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700177}
178
179/*
Christoph Lameter06972122006-06-23 02:03:35 -0700180 * Something used the pte of a page under migration. We need to
181 * get to the page and wait until migration is finished.
182 * When we return from this function the fault will be retried.
Christoph Lameter06972122006-06-23 02:03:35 -0700183 */
Naoya Horiguchif517dfe2013-06-12 14:05:04 -0700184static void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
185 spinlock_t *ptl)
Christoph Lameter06972122006-06-23 02:03:35 -0700186{
Naoya Horiguchif517dfe2013-06-12 14:05:04 -0700187 pte_t pte;
Christoph Lameter06972122006-06-23 02:03:35 -0700188 swp_entry_t entry;
189 struct page *page;
190
Naoya Horiguchif517dfe2013-06-12 14:05:04 -0700191 spin_lock(ptl);
Christoph Lameter06972122006-06-23 02:03:35 -0700192 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
Naoya Horiguchif517dfe2013-06-12 14:05:04 -0700219void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
220 unsigned long address)
221{
222 spinlock_t *ptl = pte_lockptr(mm, pmd);
223 pte_t *ptep = pte_offset_map(pmd, address);
224 __migration_entry_wait(mm, ptep, ptl);
225}
226
227void migration_entry_wait_huge(struct mm_struct *mm, pte_t *pte)
228{
229 spinlock_t *ptl = &(mm)->page_table_lock;
230 __migration_entry_wait(mm, pte, ptl);
231}
232
Mel Gormanb969c4a2012-01-12 17:19:34 -0800233#ifdef CONFIG_BLOCK
234/* Returns true if all buffers are successfully locked */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800235static bool buffer_migrate_lock_buffers(struct buffer_head *head,
236 enum migrate_mode mode)
Mel Gormanb969c4a2012-01-12 17:19:34 -0800237{
238 struct buffer_head *bh = head;
239
240 /* Simple case, sync compaction */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800241 if (mode != MIGRATE_ASYNC) {
Mel Gormanb969c4a2012-01-12 17:19:34 -0800242 do {
243 get_bh(bh);
244 lock_buffer(bh);
245 bh = bh->b_this_page;
246
247 } while (bh != head);
248
249 return true;
250 }
251
252 /* async case, we cannot block on lock_buffer so use trylock_buffer */
253 do {
254 get_bh(bh);
255 if (!trylock_buffer(bh)) {
256 /*
257 * We failed to lock the buffer and cannot stall in
258 * async migration. Release the taken locks
259 */
260 struct buffer_head *failed_bh = bh;
261 put_bh(failed_bh);
262 bh = head;
263 while (bh != failed_bh) {
264 unlock_buffer(bh);
265 put_bh(bh);
266 bh = bh->b_this_page;
267 }
268 return false;
269 }
270
271 bh = bh->b_this_page;
272 } while (bh != head);
273 return true;
274}
275#else
276static inline bool buffer_migrate_lock_buffers(struct buffer_head *head,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800277 enum migrate_mode mode)
Mel Gormanb969c4a2012-01-12 17:19:34 -0800278{
279 return true;
280}
281#endif /* CONFIG_BLOCK */
282
Christoph Lameterb20a3502006-03-22 00:09:12 -0800283/*
Christoph Lameterc3fcf8a2006-06-23 02:03:32 -0700284 * Replace the page in the mapping.
Christoph Lameter5b5c7122006-06-23 02:03:29 -0700285 *
286 * The number of remaining references must be:
287 * 1 for anonymous pages without a mapping
288 * 2 for pages with a mapping
David Howells266cf652009-04-03 16:42:36 +0100289 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800290 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700291static int migrate_page_move_mapping(struct address_space *mapping,
Mel Gormanb969c4a2012-01-12 17:19:34 -0800292 struct page *newpage, struct page *page,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800293 struct buffer_head *head, enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800294{
Nick Piggine2867812008-07-25 19:45:30 -0700295 int expected_count;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800296 void **pslot;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800297
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700298 if (!mapping) {
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700299 /* Anonymous page without mapping */
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700300 if (page_count(page) != 1)
301 return -EAGAIN;
302 return 0;
303 }
304
Nick Piggin19fd6232008-07-25 19:45:32 -0700305 spin_lock_irq(&mapping->tree_lock);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800306
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800307 pslot = radix_tree_lookup_slot(&mapping->page_tree,
308 page_index(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800309
Johannes Weineredcf4742009-09-21 17:02:59 -0700310 expected_count = 2 + page_has_private(page);
Nick Piggine2867812008-07-25 19:45:30 -0700311 if (page_count(page) != expected_count ||
Mel Gorman29c1f672011-01-13 15:47:21 -0800312 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700313 spin_unlock_irq(&mapping->tree_lock);
Christoph Lametere23ca002006-04-10 22:52:57 -0700314 return -EAGAIN;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800315 }
316
Nick Piggine2867812008-07-25 19:45:30 -0700317 if (!page_freeze_refs(page, expected_count)) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700318 spin_unlock_irq(&mapping->tree_lock);
Nick Piggine2867812008-07-25 19:45:30 -0700319 return -EAGAIN;
320 }
321
Christoph Lameterb20a3502006-03-22 00:09:12 -0800322 /*
Mel Gormanb969c4a2012-01-12 17:19:34 -0800323 * In the async migration case of moving a page with buffers, lock the
324 * buffers using trylock before the mapping is moved. If the mapping
325 * was moved, we later failed to lock the buffers and could not move
326 * the mapping back due to an elevated page count, we would have to
327 * block waiting on other references to be dropped.
328 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800329 if (mode == MIGRATE_ASYNC && head &&
330 !buffer_migrate_lock_buffers(head, mode)) {
Mel Gormanb969c4a2012-01-12 17:19:34 -0800331 page_unfreeze_refs(page, expected_count);
332 spin_unlock_irq(&mapping->tree_lock);
333 return -EAGAIN;
334 }
335
336 /*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800337 * Now we know that no one else is looking at the page.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800338 */
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800339 get_page(newpage); /* add cache reference */
Christoph Lameterb20a3502006-03-22 00:09:12 -0800340 if (PageSwapCache(page)) {
341 SetPageSwapCache(newpage);
342 set_page_private(newpage, page_private(page));
343 }
344
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800345 radix_tree_replace_slot(pslot, newpage);
346
347 /*
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800348 * Drop cache reference from old page by unfreezing
349 * to one less reference.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800350 * We know this isn't the last reference.
351 */
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800352 page_unfreeze_refs(page, expected_count - 1);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800353
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700354 /*
355 * If moved to a different zone then also account
356 * the page for that zone. Other VM counters will be
357 * taken care of when we establish references to the
358 * new page and drop references to the old page.
359 *
360 * Note that anonymous pages are accounted for
361 * via NR_FILE_PAGES and NR_ANON_PAGES if they
362 * are mapped to swap space.
363 */
364 __dec_zone_page_state(page, NR_FILE_PAGES);
365 __inc_zone_page_state(newpage, NR_FILE_PAGES);
Andrea Arcangeli99a15e22011-06-16 12:56:19 -0700366 if (!PageSwapCache(page) && PageSwapBacked(page)) {
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700367 __dec_zone_page_state(page, NR_SHMEM);
368 __inc_zone_page_state(newpage, NR_SHMEM);
369 }
Nick Piggin19fd6232008-07-25 19:45:32 -0700370 spin_unlock_irq(&mapping->tree_lock);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800371
372 return 0;
373}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800374
375/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900376 * The expected number of remaining references is the same as that
377 * of migrate_page_move_mapping().
378 */
379int migrate_huge_page_move_mapping(struct address_space *mapping,
380 struct page *newpage, struct page *page)
381{
382 int expected_count;
383 void **pslot;
384
385 if (!mapping) {
386 if (page_count(page) != 1)
387 return -EAGAIN;
388 return 0;
389 }
390
391 spin_lock_irq(&mapping->tree_lock);
392
393 pslot = radix_tree_lookup_slot(&mapping->page_tree,
394 page_index(page));
395
396 expected_count = 2 + page_has_private(page);
397 if (page_count(page) != expected_count ||
Mel Gorman29c1f672011-01-13 15:47:21 -0800398 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900399 spin_unlock_irq(&mapping->tree_lock);
400 return -EAGAIN;
401 }
402
403 if (!page_freeze_refs(page, expected_count)) {
404 spin_unlock_irq(&mapping->tree_lock);
405 return -EAGAIN;
406 }
407
408 get_page(newpage);
409
410 radix_tree_replace_slot(pslot, newpage);
411
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800412 page_unfreeze_refs(page, expected_count - 1);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900413
414 spin_unlock_irq(&mapping->tree_lock);
415 return 0;
416}
417
418/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800419 * Copy the page to its new location
420 */
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900421void migrate_page_copy(struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800422{
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900423 if (PageHuge(page))
424 copy_huge_page(newpage, page);
425 else
426 copy_highpage(newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800427
428 if (PageError(page))
429 SetPageError(newpage);
430 if (PageReferenced(page))
431 SetPageReferenced(newpage);
432 if (PageUptodate(page))
433 SetPageUptodate(newpage);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700434 if (TestClearPageActive(page)) {
435 VM_BUG_ON(PageUnevictable(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800436 SetPageActive(newpage);
Lee Schermerhorn418b27e2009-12-14 17:59:54 -0800437 } else if (TestClearPageUnevictable(page))
438 SetPageUnevictable(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800439 if (PageChecked(page))
440 SetPageChecked(newpage);
441 if (PageMappedToDisk(page))
442 SetPageMappedToDisk(newpage);
443
444 if (PageDirty(page)) {
445 clear_page_dirty_for_io(page);
Nick Piggin3a902c52008-04-30 00:55:16 -0700446 /*
447 * Want to mark the page and the radix tree as dirty, and
448 * redo the accounting that clear_page_dirty_for_io undid,
449 * but we can't use set_page_dirty because that function
450 * is actually a signal that all of the page has become dirty.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300451 * Whereas only part of our page may be dirty.
Nick Piggin3a902c52008-04-30 00:55:16 -0700452 */
453 __set_page_dirty_nobuffers(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800454 }
455
Nick Pigginb291f002008-10-18 20:26:44 -0700456 mlock_migrate_page(newpage, page);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800457 ksm_migrate_page(newpage, page);
Nick Pigginb291f002008-10-18 20:26:44 -0700458
Christoph Lameterb20a3502006-03-22 00:09:12 -0800459 ClearPageSwapCache(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800460 ClearPagePrivate(page);
461 set_page_private(page, 0);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800462
463 /*
464 * If any waiters have accumulated on the new page then
465 * wake them up.
466 */
467 if (PageWriteback(newpage))
468 end_page_writeback(newpage);
469}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800470
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700471/************************************************************
472 * Migration functions
473 ***********************************************************/
474
475/* Always fail migration. Used for mappings that are not movable */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700476int fail_migrate_page(struct address_space *mapping,
477 struct page *newpage, struct page *page)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700478{
479 return -EIO;
480}
481EXPORT_SYMBOL(fail_migrate_page);
482
Christoph Lameterb20a3502006-03-22 00:09:12 -0800483/*
484 * Common logic to directly migrate a single page suitable for
David Howells266cf652009-04-03 16:42:36 +0100485 * pages that do not use PagePrivate/PagePrivate2.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800486 *
487 * Pages are locked upon entry and exit.
488 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700489int migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800490 struct page *newpage, struct page *page,
491 enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800492{
493 int rc;
494
495 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
496
Mel Gormana6bc32b2012-01-12 17:19:43 -0800497 rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800498
499 if (rc)
500 return rc;
501
502 migrate_page_copy(newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800503 return 0;
504}
505EXPORT_SYMBOL(migrate_page);
506
David Howells93614012006-09-30 20:45:40 +0200507#ifdef CONFIG_BLOCK
Christoph Lameterb20a3502006-03-22 00:09:12 -0800508/*
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700509 * Migration function for pages with buffers. This function can only be used
510 * if the underlying filesystem guarantees that no other references to "page"
511 * exist.
512 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700513int buffer_migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800514 struct page *newpage, struct page *page, enum migrate_mode mode)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700515{
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700516 struct buffer_head *bh, *head;
517 int rc;
518
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700519 if (!page_has_buffers(page))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800520 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700521
522 head = page_buffers(page);
523
Mel Gormana6bc32b2012-01-12 17:19:43 -0800524 rc = migrate_page_move_mapping(mapping, newpage, page, head, mode);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700525
526 if (rc)
527 return rc;
528
Mel Gormanb969c4a2012-01-12 17:19:34 -0800529 /*
530 * In the async case, migrate_page_move_mapping locked the buffers
531 * with an IRQ-safe spinlock held. In the sync case, the buffers
532 * need to be locked now
533 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800534 if (mode != MIGRATE_ASYNC)
535 BUG_ON(!buffer_migrate_lock_buffers(head, mode));
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700536
537 ClearPagePrivate(page);
538 set_page_private(newpage, page_private(page));
539 set_page_private(page, 0);
540 put_page(page);
541 get_page(newpage);
542
543 bh = head;
544 do {
545 set_bh_page(bh, newpage, bh_offset(bh));
546 bh = bh->b_this_page;
547
548 } while (bh != head);
549
550 SetPagePrivate(newpage);
551
552 migrate_page_copy(newpage, page);
553
554 bh = head;
555 do {
556 unlock_buffer(bh);
557 put_bh(bh);
558 bh = bh->b_this_page;
559
560 } while (bh != head);
561
562 return 0;
563}
564EXPORT_SYMBOL(buffer_migrate_page);
David Howells93614012006-09-30 20:45:40 +0200565#endif
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700566
Christoph Lameter04e62a22006-06-23 02:03:38 -0700567/*
568 * Writeback a page to clean the dirty state
569 */
570static int writeout(struct address_space *mapping, struct page *page)
571{
572 struct writeback_control wbc = {
573 .sync_mode = WB_SYNC_NONE,
574 .nr_to_write = 1,
575 .range_start = 0,
576 .range_end = LLONG_MAX,
Christoph Lameter04e62a22006-06-23 02:03:38 -0700577 .for_reclaim = 1
578 };
579 int rc;
580
581 if (!mapping->a_ops->writepage)
582 /* No write method for the address space */
583 return -EINVAL;
584
585 if (!clear_page_dirty_for_io(page))
586 /* Someone else already triggered a write */
587 return -EAGAIN;
588
589 /*
590 * A dirty page may imply that the underlying filesystem has
591 * the page on some queue. So the page must be clean for
592 * migration. Writeout may mean we loose the lock and the
593 * page state is no longer what we checked for earlier.
594 * At this point we know that the migration attempt cannot
595 * be successful.
596 */
597 remove_migration_ptes(page, page);
598
599 rc = mapping->a_ops->writepage(page, &wbc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700600
601 if (rc != AOP_WRITEPAGE_ACTIVATE)
602 /* unlocked. Relock */
603 lock_page(page);
604
Hugh Dickinsbda85502008-11-19 15:36:36 -0800605 return (rc < 0) ? -EIO : -EAGAIN;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700606}
607
608/*
609 * Default handling if a filesystem does not provide a migration function.
610 */
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700611static int fallback_migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800612 struct page *newpage, struct page *page, enum migrate_mode mode)
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700613{
Mel Gormanb969c4a2012-01-12 17:19:34 -0800614 if (PageDirty(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800615 /* Only writeback pages in full synchronous migration */
616 if (mode != MIGRATE_SYNC)
Mel Gormanb969c4a2012-01-12 17:19:34 -0800617 return -EBUSY;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700618 return writeout(mapping, page);
Mel Gormanb969c4a2012-01-12 17:19:34 -0800619 }
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700620
621 /*
622 * Buffers may be managed in a filesystem specific way.
623 * We must have no buffers or drop them.
624 */
David Howells266cf652009-04-03 16:42:36 +0100625 if (page_has_private(page) &&
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700626 !try_to_release_page(page, GFP_KERNEL))
627 return -EAGAIN;
628
Mel Gormana6bc32b2012-01-12 17:19:43 -0800629 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700630}
631
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700632/*
Christoph Lametere24f0b82006-06-23 02:03:51 -0700633 * Move a page to a newly allocated page
634 * The page is locked and all ptes have been successfully removed.
635 *
636 * The new page will have replaced the old page if this function
637 * is successful.
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700638 *
639 * Return value:
640 * < 0 - error code
641 * == 0 - success
Christoph Lametere24f0b82006-06-23 02:03:51 -0700642 */
Mel Gorman3fe20112010-05-24 14:32:20 -0700643static int move_to_new_page(struct page *newpage, struct page *page,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800644 int remap_swapcache, enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700645{
646 struct address_space *mapping;
647 int rc;
648
649 /*
650 * Block others from accessing the page when we get around to
651 * establishing additional references. We are the only one
652 * holding a reference to the new page at this point.
653 */
Nick Piggin529ae9a2008-08-02 12:01:03 +0200654 if (!trylock_page(newpage))
Christoph Lametere24f0b82006-06-23 02:03:51 -0700655 BUG();
656
657 /* Prepare mapping for the new page.*/
658 newpage->index = page->index;
659 newpage->mapping = page->mapping;
Rik van Rielb2e18532008-10-18 20:26:30 -0700660 if (PageSwapBacked(page))
661 SetPageSwapBacked(newpage);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700662
663 mapping = page_mapping(page);
664 if (!mapping)
Mel Gormana6bc32b2012-01-12 17:19:43 -0800665 rc = migrate_page(mapping, newpage, page, mode);
Mel Gormanb969c4a2012-01-12 17:19:34 -0800666 else if (mapping->a_ops->migratepage)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700667 /*
Mel Gormanb969c4a2012-01-12 17:19:34 -0800668 * Most pages have a mapping and most filesystems provide a
669 * migratepage callback. Anonymous pages are part of swap
670 * space which also has its own migratepage callback. This
671 * is the most common path for page migration.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700672 */
Mel Gormanb969c4a2012-01-12 17:19:34 -0800673 rc = mapping->a_ops->migratepage(mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800674 newpage, page, mode);
Mel Gormanb969c4a2012-01-12 17:19:34 -0800675 else
Mel Gormana6bc32b2012-01-12 17:19:43 -0800676 rc = fallback_migrate_page(mapping, newpage, page, mode);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700677
Mel Gorman3fe20112010-05-24 14:32:20 -0700678 if (rc) {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700679 newpage->mapping = NULL;
Mel Gorman3fe20112010-05-24 14:32:20 -0700680 } else {
681 if (remap_swapcache)
682 remove_migration_ptes(page, newpage);
Konstantin Khlebnikov35512ec2012-02-03 15:37:13 -0800683 page->mapping = NULL;
Mel Gorman3fe20112010-05-24 14:32:20 -0700684 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700685
686 unlock_page(newpage);
687
688 return rc;
689}
690
Minchan Kim0dabec92011-10-31 17:06:57 -0700691static int __unmap_and_move(struct page *page, struct page *newpage,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800692 int force, bool offlining, enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700693{
Minchan Kim0dabec92011-10-31 17:06:57 -0700694 int rc = -EAGAIN;
Mel Gorman3fe20112010-05-24 14:32:20 -0700695 int remap_swapcache = 1;
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800696 int charge = 0;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -0700697 struct mem_cgroup *mem;
Mel Gorman3f6c8272010-05-24 14:32:17 -0700698 struct anon_vma *anon_vma = NULL;
Christoph Lameter95a402c2006-06-23 02:03:53 -0700699
Nick Piggin529ae9a2008-08-02 12:01:03 +0200700 if (!trylock_page(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800701 if (!force || mode == MIGRATE_ASYNC)
Minchan Kim0dabec92011-10-31 17:06:57 -0700702 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800703
704 /*
705 * It's not safe for direct compaction to call lock_page.
706 * For example, during page readahead pages are added locked
707 * to the LRU. Later, when the IO completes the pages are
708 * marked uptodate and unlocked. However, the queueing
709 * could be merging multiple pages for one bio (e.g.
710 * mpage_readpages). If an allocation happens for the
711 * second or third page, the process can end up locking
712 * the same page twice and deadlocking. Rather than
713 * trying to be clever about what pages can be locked,
714 * avoid the use of lock_page for direct compaction
715 * altogether.
716 */
717 if (current->flags & PF_MEMALLOC)
Minchan Kim0dabec92011-10-31 17:06:57 -0700718 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800719
Christoph Lametere24f0b82006-06-23 02:03:51 -0700720 lock_page(page);
721 }
722
Hugh Dickins62b61f62009-12-14 17:59:33 -0800723 /*
724 * Only memory hotplug's offline_pages() caller has locked out KSM,
725 * and can safely migrate a KSM page. The other cases have skipped
726 * PageKsm along with PageReserved - but it is only now when we have
727 * the page lock that we can be certain it will not go KSM beneath us
728 * (KSM will not upgrade a page from PageAnon to PageKsm when it sees
729 * its pagecount raised, but only here do we take the page lock which
730 * serializes that).
731 */
732 if (PageKsm(page) && !offlining) {
733 rc = -EBUSY;
734 goto unlock;
735 }
736
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800737 /* charge against new page */
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700738 charge = mem_cgroup_prepare_migration(page, newpage, &mem, GFP_KERNEL);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800739 if (charge == -ENOMEM) {
740 rc = -ENOMEM;
741 goto unlock;
742 }
743 BUG_ON(charge);
744
Christoph Lametere24f0b82006-06-23 02:03:51 -0700745 if (PageWriteback(page)) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700746 /*
Mel Gormana6bc32b2012-01-12 17:19:43 -0800747 * Only in the case of a full syncronous migration is it
748 * necessary to wait for PageWriteback. In the async case,
749 * the retry loop is too short and in the sync-light case,
750 * the overhead of stalling is too much
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700751 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800752 if (mode != MIGRATE_SYNC) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700753 rc = -EBUSY;
754 goto uncharge;
755 }
756 if (!force)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800757 goto uncharge;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700758 wait_on_page_writeback(page);
759 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700760 /*
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700761 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
762 * we cannot notice that anon_vma is freed while we migrates a page.
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800763 * This get_anon_vma() delays freeing anon_vma pointer until the end
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700764 * of migration. File cache pages are no problem because of page_lock()
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700765 * File Caches may use write_page() or lock_page() in migration, then,
766 * just care Anon page here.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700767 */
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700768 if (PageAnon(page)) {
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800769 /*
770 * Only page_lock_anon_vma() understands the subtleties of
771 * getting a hold on an anon_vma from outside one of its mms.
772 */
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700773 anon_vma = page_get_anon_vma(page);
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800774 if (anon_vma) {
775 /*
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700776 * Anon page
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800777 */
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800778 } else if (PageSwapCache(page)) {
Mel Gorman3fe20112010-05-24 14:32:20 -0700779 /*
780 * We cannot be sure that the anon_vma of an unmapped
781 * swapcache page is safe to use because we don't
782 * know in advance if the VMA that this page belonged
783 * to still exists. If the VMA and others sharing the
784 * data have been freed, then the anon_vma could
785 * already be invalid.
786 *
787 * To avoid this possibility, swapcache pages get
788 * migrated but are not remapped when migration
789 * completes
790 */
791 remap_swapcache = 0;
792 } else {
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800793 goto uncharge;
Mel Gorman3fe20112010-05-24 14:32:20 -0700794 }
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700795 }
Shaohua Li62e1c552008-02-04 22:29:33 -0800796
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700797 /*
Shaohua Li62e1c552008-02-04 22:29:33 -0800798 * Corner case handling:
799 * 1. When a new swap-cache page is read into, it is added to the LRU
800 * and treated as swapcache but it has no rmap yet.
801 * Calling try_to_unmap() against a page->mapping==NULL page will
802 * trigger a BUG. So handle it here.
803 * 2. An orphaned page (see truncate_complete_page) might have
804 * fs-private metadata. The page can be picked up due to memory
805 * offlining. Everywhere else except page reclaim, the page is
806 * invisible to the vm, so the page can not be migrated. So try to
807 * free the metadata, so the page can be freed.
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700808 */
Shaohua Li62e1c552008-02-04 22:29:33 -0800809 if (!page->mapping) {
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800810 VM_BUG_ON(PageAnon(page));
811 if (page_has_private(page)) {
Shaohua Li62e1c552008-02-04 22:29:33 -0800812 try_to_free_buffers(page);
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800813 goto uncharge;
Shaohua Li62e1c552008-02-04 22:29:33 -0800814 }
Shaohua Liabfc3482009-09-21 17:01:19 -0700815 goto skip_unmap;
Shaohua Li62e1c552008-02-04 22:29:33 -0800816 }
817
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700818 /* Establish migration ptes or remove ptes */
Andi Kleen14fa31b2009-09-16 11:50:10 +0200819 try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700820
Shaohua Liabfc3482009-09-21 17:01:19 -0700821skip_unmap:
Christoph Lametere6a15302006-06-25 05:46:49 -0700822 if (!page_mapped(page))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800823 rc = move_to_new_page(newpage, page, remap_swapcache, mode);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700824
Mel Gorman3fe20112010-05-24 14:32:20 -0700825 if (rc && remap_swapcache)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700826 remove_migration_ptes(page, page);
Mel Gorman3f6c8272010-05-24 14:32:17 -0700827
828 /* Drop an anon_vma reference if we took one */
Rik van Riel76545062010-08-09 17:18:41 -0700829 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -0700830 put_anon_vma(anon_vma);
Mel Gorman3f6c8272010-05-24 14:32:17 -0700831
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800832uncharge:
833 if (!charge)
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -0800834 mem_cgroup_end_migration(mem, page, newpage, rc == 0);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700835unlock:
836 unlock_page(page);
Minchan Kim0dabec92011-10-31 17:06:57 -0700837out:
838 return rc;
839}
Christoph Lameter95a402c2006-06-23 02:03:53 -0700840
Minchan Kim0dabec92011-10-31 17:06:57 -0700841/*
842 * Obtain the lock on page, remove all ptes and migrate the page
843 * to the newly allocated page in newpage.
844 */
845static int unmap_and_move(new_page_t get_new_page, unsigned long private,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800846 struct page *page, int force, bool offlining,
847 enum migrate_mode mode)
Minchan Kim0dabec92011-10-31 17:06:57 -0700848{
849 int rc = 0;
850 int *result = NULL;
851 struct page *newpage = get_new_page(page, private, &result);
852
853 if (!newpage)
854 return -ENOMEM;
855
856 if (page_count(page) == 1) {
857 /* page was freed from under us. So we are done. */
858 goto out;
859 }
860
861 if (unlikely(PageTransHuge(page)))
862 if (unlikely(split_huge_page(page)))
863 goto out;
864
Mel Gormana6bc32b2012-01-12 17:19:43 -0800865 rc = __unmap_and_move(page, newpage, force, offlining, mode);
Minchan Kim0dabec92011-10-31 17:06:57 -0700866out:
Christoph Lametere24f0b82006-06-23 02:03:51 -0700867 if (rc != -EAGAIN) {
Minchan Kim0dabec92011-10-31 17:06:57 -0700868 /*
869 * A page that has been migrated has all references
870 * removed and will be freed. A page that has not been
871 * migrated will have kepts its references and be
872 * restored.
873 */
874 list_del(&page->lru);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -0700875 dec_zone_page_state(page, NR_ISOLATED_ANON +
Johannes Weiner6c0b1352009-09-21 17:02:59 -0700876 page_is_file_cache(page));
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700877 putback_lru_page(page);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700878 }
Christoph Lameter95a402c2006-06-23 02:03:53 -0700879 /*
880 * Move the new page to the LRU. If migration was not successful
881 * then this will free the page.
882 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700883 putback_lru_page(newpage);
Christoph Lameter742755a2006-06-23 02:03:55 -0700884 if (result) {
885 if (rc)
886 *result = rc;
887 else
888 *result = page_to_nid(newpage);
889 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700890 return rc;
891}
892
893/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900894 * Counterpart of unmap_and_move_page() for hugepage migration.
895 *
896 * This function doesn't wait the completion of hugepage I/O
897 * because there is no race between I/O and migration for hugepage.
898 * Note that currently hugepage I/O occurs only in direct I/O
899 * where no lock is held and PG_writeback is irrelevant,
900 * and writeback status of all subpages are counted in the reference
901 * count of the head page (i.e. if all subpages of a 2MB hugepage are
902 * under direct I/O, the reference of the head page is 512 and a bit more.)
903 * This means that when we try to migrate hugepage whose subpages are
904 * doing direct I/O, some references remain after try_to_unmap() and
905 * hugepage migration fails without data corruption.
906 *
907 * There is also no race when direct I/O is issued on the page under migration,
908 * because then pte is replaced with migration swap entry and direct I/O code
909 * will wait in the page fault for migration to complete.
910 */
911static int unmap_and_move_huge_page(new_page_t get_new_page,
912 unsigned long private, struct page *hpage,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800913 int force, bool offlining,
914 enum migrate_mode mode)
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900915{
916 int rc = 0;
917 int *result = NULL;
918 struct page *new_hpage = get_new_page(hpage, private, &result);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900919 struct anon_vma *anon_vma = NULL;
920
921 if (!new_hpage)
922 return -ENOMEM;
923
924 rc = -EAGAIN;
925
926 if (!trylock_page(hpage)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800927 if (!force || mode != MIGRATE_SYNC)
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900928 goto out;
929 lock_page(hpage);
930 }
931
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700932 if (PageAnon(hpage))
933 anon_vma = page_get_anon_vma(hpage);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900934
935 try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
936
937 if (!page_mapped(hpage))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800938 rc = move_to_new_page(new_hpage, hpage, 1, mode);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900939
940 if (rc)
941 remove_migration_ptes(hpage, hpage);
942
Hugh Dickinsfd4a4662011-01-13 15:47:31 -0800943 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -0700944 put_anon_vma(anon_vma);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900945 unlock_page(hpage);
946
Hillf Danton09761332011-12-08 14:34:20 -0800947out:
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900948 if (rc != -EAGAIN) {
949 list_del(&hpage->lru);
950 put_page(hpage);
951 }
952
953 put_page(new_hpage);
954
955 if (result) {
956 if (rc)
957 *result = rc;
958 else
959 *result = page_to_nid(new_hpage);
960 }
961 return rc;
962}
963
964/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800965 * migrate_pages
966 *
Christoph Lameter95a402c2006-06-23 02:03:53 -0700967 * The function takes one list of pages to migrate and a function
968 * that determines from the page to be migrated and the private data
969 * the target of the move and allocates the page.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800970 *
971 * The function returns after 10 attempts or if no pages
972 * are movable anymore because to has become empty
Minchan Kimcf608ac2010-10-26 14:21:29 -0700973 * or no retryable pages exist anymore.
974 * Caller should call putback_lru_pages to return pages to the LRU
Minchan Kim28bd6572011-01-25 15:07:26 -0800975 * or free list only if ret != 0.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800976 *
Christoph Lameter95a402c2006-06-23 02:03:53 -0700977 * Return: Number of pages not migrated or error code.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800978 */
Christoph Lameter95a402c2006-06-23 02:03:53 -0700979int migrate_pages(struct list_head *from,
Mel Gorman7f0f2492011-01-13 15:45:58 -0800980 new_page_t get_new_page, unsigned long private, bool offlining,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800981 enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800982{
Christoph Lametere24f0b82006-06-23 02:03:51 -0700983 int retry = 1;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800984 int nr_failed = 0;
Mel Gorman7bfd6e42012-10-19 10:46:20 +0100985 int nr_succeeded = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800986 int pass = 0;
987 struct page *page;
988 struct page *page2;
989 int swapwrite = current->flags & PF_SWAPWRITE;
990 int rc;
991
Liam Mark069398a2013-01-16 10:14:40 -0800992 trace_migrate_pages_start(mode);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800993 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++;
Liam Mark069398a2013-01-16 10:14:40 -08001011 trace_migrate_retry(retry);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001012 break;
1013 case 0:
Mel Gorman7bfd6e42012-10-19 10:46:20 +01001014 nr_succeeded++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001015 break;
1016 default:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001017 /* Permanent failure */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001018 nr_failed++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001019 break;
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001020 }
Christoph Lameterb20a3502006-03-22 00:09:12 -08001021 }
1022 }
Christoph Lameter95a402c2006-06-23 02:03:53 -07001023 rc = 0;
1024out:
Mel Gorman7bfd6e42012-10-19 10:46:20 +01001025 if (nr_succeeded)
1026 count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
1027 if (nr_failed)
1028 count_vm_events(PGMIGRATE_FAIL, nr_failed);
Christoph Lameterb20a3502006-03-22 00:09:12 -08001029 if (!swapwrite)
1030 current->flags &= ~PF_SWAPWRITE;
1031
Liam Mark069398a2013-01-16 10:14:40 -08001032 trace_migrate_pages_end(mode);
Christoph Lameter95a402c2006-06-23 02:03:53 -07001033 if (rc)
1034 return rc;
1035
Christoph Lameterb20a3502006-03-22 00:09:12 -08001036 return nr_failed + retry;
1037}
1038
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001039int migrate_huge_pages(struct list_head *from,
Mel Gorman7f0f2492011-01-13 15:45:58 -08001040 new_page_t get_new_page, unsigned long private, bool offlining,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001041 enum migrate_mode mode)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001042{
1043 int retry = 1;
1044 int nr_failed = 0;
1045 int pass = 0;
1046 struct page *page;
1047 struct page *page2;
1048 int rc;
1049
1050 for (pass = 0; pass < 10 && retry; pass++) {
1051 retry = 0;
1052
1053 list_for_each_entry_safe(page, page2, from, lru) {
1054 cond_resched();
1055
1056 rc = unmap_and_move_huge_page(get_new_page,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001057 private, page, pass > 2, offlining,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001058 mode);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001059
1060 switch(rc) {
1061 case -ENOMEM:
1062 goto out;
1063 case -EAGAIN:
1064 retry++;
1065 break;
1066 case 0:
1067 break;
1068 default:
1069 /* Permanent failure */
1070 nr_failed++;
1071 break;
1072 }
1073 }
1074 }
1075 rc = 0;
1076out:
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001077 if (rc)
1078 return rc;
1079
1080 return nr_failed + retry;
1081}
1082
Christoph Lameter742755a2006-06-23 02:03:55 -07001083#ifdef CONFIG_NUMA
1084/*
1085 * Move a list of individual pages
1086 */
1087struct page_to_node {
1088 unsigned long addr;
1089 struct page *page;
1090 int node;
1091 int status;
1092};
1093
1094static struct page *new_page_node(struct page *p, unsigned long private,
1095 int **result)
1096{
1097 struct page_to_node *pm = (struct page_to_node *)private;
1098
1099 while (pm->node != MAX_NUMNODES && pm->page != p)
1100 pm++;
1101
1102 if (pm->node == MAX_NUMNODES)
1103 return NULL;
1104
1105 *result = &pm->status;
1106
Mel Gorman6484eb32009-06-16 15:31:54 -07001107 return alloc_pages_exact_node(pm->node,
Mel Gorman769848c2007-07-17 04:03:05 -07001108 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
Christoph Lameter742755a2006-06-23 02:03:55 -07001109}
1110
1111/*
1112 * Move a set of pages as indicated in the pm array. The addr
1113 * field must be set to the virtual address of the page to be moved
1114 * and the node number must contain a valid target node.
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001115 * The pm array ends with node = MAX_NUMNODES.
Christoph Lameter742755a2006-06-23 02:03:55 -07001116 */
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001117static int do_move_page_to_node_array(struct mm_struct *mm,
1118 struct page_to_node *pm,
1119 int migrate_all)
Christoph Lameter742755a2006-06-23 02:03:55 -07001120{
1121 int err;
1122 struct page_to_node *pp;
1123 LIST_HEAD(pagelist);
1124
1125 down_read(&mm->mmap_sem);
1126
1127 /*
1128 * Build a list of pages to migrate
1129 */
Christoph Lameter742755a2006-06-23 02:03:55 -07001130 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
1131 struct vm_area_struct *vma;
1132 struct page *page;
1133
Christoph Lameter742755a2006-06-23 02:03:55 -07001134 err = -EFAULT;
1135 vma = find_vma(mm, pp->addr);
Gleb Natapov70384dc2010-10-26 14:22:07 -07001136 if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
Christoph Lameter742755a2006-06-23 02:03:55 -07001137 goto set_status;
1138
Andrea Arcangeli500d65d2011-01-13 15:46:55 -08001139 page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001140
1141 err = PTR_ERR(page);
1142 if (IS_ERR(page))
1143 goto set_status;
1144
Christoph Lameter742755a2006-06-23 02:03:55 -07001145 err = -ENOENT;
1146 if (!page)
1147 goto set_status;
1148
Hugh Dickins62b61f62009-12-14 17:59:33 -08001149 /* Use PageReserved to check for zero page */
1150 if (PageReserved(page) || PageKsm(page))
Christoph Lameter742755a2006-06-23 02:03:55 -07001151 goto put_and_set;
1152
1153 pp->page = page;
1154 err = page_to_nid(page);
1155
1156 if (err == pp->node)
1157 /*
1158 * Node already in the right place
1159 */
1160 goto put_and_set;
1161
1162 err = -EACCES;
1163 if (page_mapcount(page) > 1 &&
1164 !migrate_all)
1165 goto put_and_set;
1166
Nick Piggin62695a82008-10-18 20:26:09 -07001167 err = isolate_lru_page(page);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001168 if (!err) {
Nick Piggin62695a82008-10-18 20:26:09 -07001169 list_add_tail(&page->lru, &pagelist);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001170 inc_zone_page_state(page, NR_ISOLATED_ANON +
1171 page_is_file_cache(page));
1172 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001173put_and_set:
1174 /*
1175 * Either remove the duplicate refcount from
1176 * isolate_lru_page() or drop the page ref if it was
1177 * not isolated.
1178 */
1179 put_page(page);
1180set_status:
1181 pp->status = err;
1182 }
1183
Brice Gogline78bbfa2008-10-18 20:27:15 -07001184 err = 0;
Minchan Kimcf608ac2010-10-26 14:21:29 -07001185 if (!list_empty(&pagelist)) {
Christoph Lameter742755a2006-06-23 02:03:55 -07001186 err = migrate_pages(&pagelist, new_page_node,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001187 (unsigned long)pm, 0, MIGRATE_SYNC);
Minchan Kimcf608ac2010-10-26 14:21:29 -07001188 if (err)
1189 putback_lru_pages(&pagelist);
1190 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001191
1192 up_read(&mm->mmap_sem);
1193 return err;
1194}
1195
1196/*
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001197 * Migrate an array of page address onto an array of nodes and fill
1198 * the corresponding array of status.
1199 */
Christoph Lameter3268c632012-03-21 16:34:06 -07001200static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001201 unsigned long nr_pages,
1202 const void __user * __user *pages,
1203 const int __user *nodes,
1204 int __user *status, int flags)
1205{
Brice Goglin3140a222009-01-06 14:38:57 -08001206 struct page_to_node *pm;
Brice Goglin3140a222009-01-06 14:38:57 -08001207 unsigned long chunk_nr_pages;
1208 unsigned long chunk_start;
1209 int err;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001210
Brice Goglin3140a222009-01-06 14:38:57 -08001211 err = -ENOMEM;
1212 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
1213 if (!pm)
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001214 goto out;
Brice Goglin35282a22009-06-16 15:32:43 -07001215
1216 migrate_prep();
1217
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001218 /*
Brice Goglin3140a222009-01-06 14:38:57 -08001219 * Store a chunk of page_to_node array in a page,
1220 * but keep the last one as a marker
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001221 */
Brice Goglin3140a222009-01-06 14:38:57 -08001222 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001223
Brice Goglin3140a222009-01-06 14:38:57 -08001224 for (chunk_start = 0;
1225 chunk_start < nr_pages;
1226 chunk_start += chunk_nr_pages) {
1227 int j;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001228
Brice Goglin3140a222009-01-06 14:38:57 -08001229 if (chunk_start + chunk_nr_pages > nr_pages)
1230 chunk_nr_pages = nr_pages - chunk_start;
1231
1232 /* fill the chunk pm with addrs and nodes from user-space */
1233 for (j = 0; j < chunk_nr_pages; j++) {
1234 const void __user *p;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001235 int node;
1236
Brice Goglin3140a222009-01-06 14:38:57 -08001237 err = -EFAULT;
1238 if (get_user(p, pages + j + chunk_start))
1239 goto out_pm;
1240 pm[j].addr = (unsigned long) p;
1241
1242 if (get_user(node, nodes + j + chunk_start))
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001243 goto out_pm;
1244
1245 err = -ENODEV;
Linus Torvalds6f5a55f2010-02-05 16:16:50 -08001246 if (node < 0 || node >= MAX_NUMNODES)
1247 goto out_pm;
1248
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001249 if (!node_state(node, N_HIGH_MEMORY))
1250 goto out_pm;
1251
1252 err = -EACCES;
1253 if (!node_isset(node, task_nodes))
1254 goto out_pm;
1255
Brice Goglin3140a222009-01-06 14:38:57 -08001256 pm[j].node = node;
1257 }
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001258
Brice Goglin3140a222009-01-06 14:38:57 -08001259 /* End marker for this chunk */
1260 pm[chunk_nr_pages].node = MAX_NUMNODES;
1261
1262 /* Migrate this chunk */
1263 err = do_move_page_to_node_array(mm, pm,
1264 flags & MPOL_MF_MOVE_ALL);
1265 if (err < 0)
1266 goto out_pm;
1267
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001268 /* Return status information */
Brice Goglin3140a222009-01-06 14:38:57 -08001269 for (j = 0; j < chunk_nr_pages; j++)
1270 if (put_user(pm[j].status, status + j + chunk_start)) {
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001271 err = -EFAULT;
Brice Goglin3140a222009-01-06 14:38:57 -08001272 goto out_pm;
1273 }
1274 }
1275 err = 0;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001276
1277out_pm:
Brice Goglin3140a222009-01-06 14:38:57 -08001278 free_page((unsigned long)pm);
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001279out:
1280 return err;
1281}
1282
1283/*
Brice Goglin2f007e72008-10-18 20:27:16 -07001284 * Determine the nodes of an array of pages and store it in an array of status.
Christoph Lameter742755a2006-06-23 02:03:55 -07001285 */
Brice Goglin80bba122008-12-09 13:14:23 -08001286static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1287 const void __user **pages, int *status)
Christoph Lameter742755a2006-06-23 02:03:55 -07001288{
Brice Goglin2f007e72008-10-18 20:27:16 -07001289 unsigned long i;
Brice Goglin2f007e72008-10-18 20:27:16 -07001290
Christoph Lameter742755a2006-06-23 02:03:55 -07001291 down_read(&mm->mmap_sem);
1292
Brice Goglin2f007e72008-10-18 20:27:16 -07001293 for (i = 0; i < nr_pages; i++) {
Brice Goglin80bba122008-12-09 13:14:23 -08001294 unsigned long addr = (unsigned long)(*pages);
Christoph Lameter742755a2006-06-23 02:03:55 -07001295 struct vm_area_struct *vma;
1296 struct page *page;
KOSAKI Motohiroc095adb2008-12-16 16:06:43 +09001297 int err = -EFAULT;
Brice Goglin2f007e72008-10-18 20:27:16 -07001298
1299 vma = find_vma(mm, addr);
Gleb Natapov70384dc2010-10-26 14:22:07 -07001300 if (!vma || addr < vma->vm_start)
Christoph Lameter742755a2006-06-23 02:03:55 -07001301 goto set_status;
1302
Brice Goglin2f007e72008-10-18 20:27:16 -07001303 page = follow_page(vma, addr, 0);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001304
1305 err = PTR_ERR(page);
1306 if (IS_ERR(page))
1307 goto set_status;
1308
Christoph Lameter742755a2006-06-23 02:03:55 -07001309 err = -ENOENT;
1310 /* Use PageReserved to check for zero page */
Hugh Dickins62b61f62009-12-14 17:59:33 -08001311 if (!page || PageReserved(page) || PageKsm(page))
Christoph Lameter742755a2006-06-23 02:03:55 -07001312 goto set_status;
1313
1314 err = page_to_nid(page);
1315set_status:
Brice Goglin80bba122008-12-09 13:14:23 -08001316 *status = err;
1317
1318 pages++;
1319 status++;
1320 }
1321
1322 up_read(&mm->mmap_sem);
1323}
1324
1325/*
1326 * Determine the nodes of a user array of pages and store it in
1327 * a user array of status.
1328 */
1329static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1330 const void __user * __user *pages,
1331 int __user *status)
1332{
1333#define DO_PAGES_STAT_CHUNK_NR 16
1334 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1335 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
Brice Goglin80bba122008-12-09 13:14:23 -08001336
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001337 while (nr_pages) {
1338 unsigned long chunk_nr;
Brice Goglin80bba122008-12-09 13:14:23 -08001339
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001340 chunk_nr = nr_pages;
1341 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1342 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1343
1344 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
1345 break;
Brice Goglin80bba122008-12-09 13:14:23 -08001346
1347 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1348
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001349 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1350 break;
Christoph Lameter742755a2006-06-23 02:03:55 -07001351
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001352 pages += chunk_nr;
1353 status += chunk_nr;
1354 nr_pages -= chunk_nr;
1355 }
1356 return nr_pages ? -EFAULT : 0;
Christoph Lameter742755a2006-06-23 02:03:55 -07001357}
1358
1359/*
1360 * Move a list of pages in the address space of the currently executing
1361 * process.
1362 */
Heiko Carstens938bb9f2009-01-14 14:14:30 +01001363SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1364 const void __user * __user *, pages,
1365 const int __user *, nodes,
1366 int __user *, status, int, flags)
Christoph Lameter742755a2006-06-23 02:03:55 -07001367{
David Howellsc69e8d92008-11-14 10:39:19 +11001368 const struct cred *cred = current_cred(), *tcred;
Christoph Lameter742755a2006-06-23 02:03:55 -07001369 struct task_struct *task;
Christoph Lameter742755a2006-06-23 02:03:55 -07001370 struct mm_struct *mm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001371 int err;
Christoph Lameter3268c632012-03-21 16:34:06 -07001372 nodemask_t task_nodes;
Christoph Lameter742755a2006-06-23 02:03:55 -07001373
1374 /* Check flags */
1375 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1376 return -EINVAL;
1377
1378 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1379 return -EPERM;
1380
1381 /* Find the mm_struct */
Greg Thelena879bf52011-02-25 14:44:13 -08001382 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07001383 task = pid ? find_task_by_vpid(pid) : current;
Christoph Lameter742755a2006-06-23 02:03:55 -07001384 if (!task) {
Greg Thelena879bf52011-02-25 14:44:13 -08001385 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001386 return -ESRCH;
1387 }
Christoph Lameter3268c632012-03-21 16:34:06 -07001388 get_task_struct(task);
Christoph Lameter742755a2006-06-23 02:03:55 -07001389
1390 /*
1391 * Check if this process has the right to modify the specified
1392 * process. The right exists if the process has administrative
1393 * capabilities, superuser privileges or the same
1394 * userid as the target process.
1395 */
David Howellsc69e8d92008-11-14 10:39:19 +11001396 tcred = __task_cred(task);
David Howellsb6dff3e2008-11-14 10:39:16 +11001397 if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
1398 cred->uid != tcred->suid && cred->uid != tcred->uid &&
Christoph Lameter742755a2006-06-23 02:03:55 -07001399 !capable(CAP_SYS_NICE)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001400 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001401 err = -EPERM;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001402 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001403 }
David Howellsc69e8d92008-11-14 10:39:19 +11001404 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001405
David Quigley86c3a762006-06-23 02:04:02 -07001406 err = security_task_movememory(task);
1407 if (err)
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001408 goto out;
David Quigley86c3a762006-06-23 02:04:02 -07001409
Christoph Lameter3268c632012-03-21 16:34:06 -07001410 task_nodes = cpuset_mems_allowed(task);
1411 mm = get_task_mm(task);
1412 put_task_struct(task);
1413
Sasha Levin6e8b09e2012-04-25 16:01:53 -07001414 if (!mm)
1415 return -EINVAL;
1416
1417 if (nodes)
1418 err = do_pages_move(mm, task_nodes, nr_pages, pages,
1419 nodes, status, flags);
1420 else
1421 err = do_pages_stat(mm, nr_pages, pages, status);
Christoph Lameter3268c632012-03-21 16:34:06 -07001422
1423 mmput(mm);
1424 return err;
David Quigley86c3a762006-06-23 02:04:02 -07001425
Christoph Lameter742755a2006-06-23 02:03:55 -07001426out:
Christoph Lameter3268c632012-03-21 16:34:06 -07001427 put_task_struct(task);
Christoph Lameter742755a2006-06-23 02:03:55 -07001428 return err;
1429}
Christoph Lameter742755a2006-06-23 02:03:55 -07001430
Christoph Lameter7b2259b2006-06-25 05:46:48 -07001431/*
1432 * Call migration functions in the vma_ops that may prepare
1433 * memory in a vm for migration. migration functions may perform
1434 * the migration for vmas that do not have an underlying page struct.
1435 */
1436int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1437 const nodemask_t *from, unsigned long flags)
1438{
1439 struct vm_area_struct *vma;
1440 int err = 0;
1441
Daisuke Nishimura1001c9f2009-02-11 13:04:18 -08001442 for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
Christoph Lameter7b2259b2006-06-25 05:46:48 -07001443 if (vma->vm_ops && vma->vm_ops->migrate) {
1444 err = vma->vm_ops->migrate(vma, to, from, flags);
1445 if (err)
1446 break;
1447 }
1448 }
1449 return err;
1450}
Gerald Schaefer83d16742008-07-23 21:28:22 -07001451#endif