blob: c18e80413e1a1d9f67c83dc2787a67962911830c [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));
Mel Gormanea38cd42014-10-02 19:47:41 +0100143
144 /* Recheck VMA as permissions can change since migration started */
Christoph Lameter06972122006-06-23 02:03:35 -0700145 if (is_write_migration_entry(entry))
Mel Gormanea38cd42014-10-02 19:47:41 +0100146 pte = maybe_mkwrite(pte, vma);
147
Andi Kleen3ef8fd72010-10-11 16:03:21 +0200148#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900149 if (PageHuge(new))
150 pte = pte_mkhuge(pte);
Andi Kleen3ef8fd72010-10-11 16:03:21 +0200151#endif
Leonid Yegoshin11e88c52013-05-24 15:55:18 -0700152 flush_dcache_page(new);
Christoph Lameter06972122006-06-23 02:03:35 -0700153 set_pte_at(mm, addr, ptep, pte);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700154
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900155 if (PageHuge(new)) {
156 if (PageAnon(new))
157 hugepage_add_anon_rmap(new, vma, addr);
158 else
159 page_dup_rmap(new);
160 } else if (PageAnon(new))
Christoph Lameter04e62a22006-06-23 02:03:38 -0700161 page_add_anon_rmap(new, vma, addr);
162 else
163 page_add_file_rmap(new);
164
165 /* No need to invalidate - it was non-present before */
Russell King4b3073e2009-12-18 16:40:18 +0000166 update_mmu_cache(vma, addr, ptep);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800167unlock:
Christoph Lameter06972122006-06-23 02:03:35 -0700168 pte_unmap_unlock(ptep, ptl);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800169out:
170 return SWAP_AGAIN;
Christoph Lameter06972122006-06-23 02:03:35 -0700171}
172
173/*
Christoph Lameter04e62a22006-06-23 02:03:38 -0700174 * Get rid of all migration entries and replace them by
175 * references to the indicated page.
176 */
177static void remove_migration_ptes(struct page *old, struct page *new)
178{
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800179 rmap_walk(new, remove_migration_pte, old);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700180}
181
182/*
Christoph Lameter06972122006-06-23 02:03:35 -0700183 * Something used the pte of a page under migration. We need to
184 * get to the page and wait until migration is finished.
185 * When we return from this function the fault will be retried.
Christoph Lameter06972122006-06-23 02:03:35 -0700186 */
Naoya Horiguchif517dfe2013-06-12 14:05:04 -0700187static void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
188 spinlock_t *ptl)
Christoph Lameter06972122006-06-23 02:03:35 -0700189{
Naoya Horiguchif517dfe2013-06-12 14:05:04 -0700190 pte_t pte;
Christoph Lameter06972122006-06-23 02:03:35 -0700191 swp_entry_t entry;
192 struct page *page;
193
Naoya Horiguchif517dfe2013-06-12 14:05:04 -0700194 spin_lock(ptl);
Christoph Lameter06972122006-06-23 02:03:35 -0700195 pte = *ptep;
196 if (!is_swap_pte(pte))
197 goto out;
198
199 entry = pte_to_swp_entry(pte);
200 if (!is_migration_entry(entry))
201 goto out;
202
203 page = migration_entry_to_page(entry);
204
Nick Piggine2867812008-07-25 19:45:30 -0700205 /*
206 * Once radix-tree replacement of page migration started, page_count
207 * *must* be zero. And, we don't want to call wait_on_page_locked()
208 * against a page without get_page().
209 * So, we use get_page_unless_zero(), here. Even failed, page fault
210 * will occur again.
211 */
212 if (!get_page_unless_zero(page))
213 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700214 pte_unmap_unlock(ptep, ptl);
215 wait_on_page_locked(page);
216 put_page(page);
217 return;
218out:
219 pte_unmap_unlock(ptep, ptl);
220}
221
Naoya Horiguchif517dfe2013-06-12 14:05:04 -0700222void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
223 unsigned long address)
224{
225 spinlock_t *ptl = pte_lockptr(mm, pmd);
226 pte_t *ptep = pte_offset_map(pmd, address);
227 __migration_entry_wait(mm, ptep, ptl);
228}
229
230void migration_entry_wait_huge(struct mm_struct *mm, pte_t *pte)
231{
232 spinlock_t *ptl = &(mm)->page_table_lock;
233 __migration_entry_wait(mm, pte, ptl);
234}
235
Mel Gormanb969c4a2012-01-12 17:19:34 -0800236#ifdef CONFIG_BLOCK
237/* Returns true if all buffers are successfully locked */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800238static bool buffer_migrate_lock_buffers(struct buffer_head *head,
239 enum migrate_mode mode)
Mel Gormanb969c4a2012-01-12 17:19:34 -0800240{
241 struct buffer_head *bh = head;
242
243 /* Simple case, sync compaction */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800244 if (mode != MIGRATE_ASYNC) {
Mel Gormanb969c4a2012-01-12 17:19:34 -0800245 do {
246 get_bh(bh);
247 lock_buffer(bh);
248 bh = bh->b_this_page;
249
250 } while (bh != head);
251
252 return true;
253 }
254
255 /* async case, we cannot block on lock_buffer so use trylock_buffer */
256 do {
257 get_bh(bh);
258 if (!trylock_buffer(bh)) {
259 /*
260 * We failed to lock the buffer and cannot stall in
261 * async migration. Release the taken locks
262 */
263 struct buffer_head *failed_bh = bh;
264 put_bh(failed_bh);
265 bh = head;
266 while (bh != failed_bh) {
267 unlock_buffer(bh);
268 put_bh(bh);
269 bh = bh->b_this_page;
270 }
271 return false;
272 }
273
274 bh = bh->b_this_page;
275 } while (bh != head);
276 return true;
277}
278#else
279static inline bool buffer_migrate_lock_buffers(struct buffer_head *head,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800280 enum migrate_mode mode)
Mel Gormanb969c4a2012-01-12 17:19:34 -0800281{
282 return true;
283}
284#endif /* CONFIG_BLOCK */
285
Christoph Lameterb20a3502006-03-22 00:09:12 -0800286/*
Christoph Lameterc3fcf8a2006-06-23 02:03:32 -0700287 * Replace the page in the mapping.
Christoph Lameter5b5c7122006-06-23 02:03:29 -0700288 *
289 * The number of remaining references must be:
290 * 1 for anonymous pages without a mapping
291 * 2 for pages with a mapping
David Howells266cf652009-04-03 16:42:36 +0100292 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800293 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700294static int migrate_page_move_mapping(struct address_space *mapping,
Mel Gormanb969c4a2012-01-12 17:19:34 -0800295 struct page *newpage, struct page *page,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800296 struct buffer_head *head, enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800297{
Nick Piggine2867812008-07-25 19:45:30 -0700298 int expected_count;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800299 void **pslot;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800300
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700301 if (!mapping) {
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700302 /* Anonymous page without mapping */
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700303 if (page_count(page) != 1)
304 return -EAGAIN;
305 return 0;
306 }
307
Nick Piggin19fd6232008-07-25 19:45:32 -0700308 spin_lock_irq(&mapping->tree_lock);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800309
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800310 pslot = radix_tree_lookup_slot(&mapping->page_tree,
311 page_index(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800312
Johannes Weineredcf4742009-09-21 17:02:59 -0700313 expected_count = 2 + page_has_private(page);
Nick Piggine2867812008-07-25 19:45:30 -0700314 if (page_count(page) != expected_count ||
Mel Gorman29c1f672011-01-13 15:47:21 -0800315 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700316 spin_unlock_irq(&mapping->tree_lock);
Christoph Lametere23ca002006-04-10 22:52:57 -0700317 return -EAGAIN;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800318 }
319
Nick Piggine2867812008-07-25 19:45:30 -0700320 if (!page_freeze_refs(page, expected_count)) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700321 spin_unlock_irq(&mapping->tree_lock);
Nick Piggine2867812008-07-25 19:45:30 -0700322 return -EAGAIN;
323 }
324
Christoph Lameterb20a3502006-03-22 00:09:12 -0800325 /*
Mel Gormanb969c4a2012-01-12 17:19:34 -0800326 * In the async migration case of moving a page with buffers, lock the
327 * buffers using trylock before the mapping is moved. If the mapping
328 * was moved, we later failed to lock the buffers and could not move
329 * the mapping back due to an elevated page count, we would have to
330 * block waiting on other references to be dropped.
331 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800332 if (mode == MIGRATE_ASYNC && head &&
333 !buffer_migrate_lock_buffers(head, mode)) {
Mel Gormanb969c4a2012-01-12 17:19:34 -0800334 page_unfreeze_refs(page, expected_count);
335 spin_unlock_irq(&mapping->tree_lock);
336 return -EAGAIN;
337 }
338
339 /*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800340 * Now we know that no one else is looking at the page.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800341 */
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800342 get_page(newpage); /* add cache reference */
Christoph Lameterb20a3502006-03-22 00:09:12 -0800343 if (PageSwapCache(page)) {
344 SetPageSwapCache(newpage);
345 set_page_private(newpage, page_private(page));
346 }
347
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800348 radix_tree_replace_slot(pslot, newpage);
349
350 /*
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800351 * Drop cache reference from old page by unfreezing
352 * to one less reference.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800353 * We know this isn't the last reference.
354 */
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800355 page_unfreeze_refs(page, expected_count - 1);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800356
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700357 /*
358 * If moved to a different zone then also account
359 * the page for that zone. Other VM counters will be
360 * taken care of when we establish references to the
361 * new page and drop references to the old page.
362 *
363 * Note that anonymous pages are accounted for
364 * via NR_FILE_PAGES and NR_ANON_PAGES if they
365 * are mapped to swap space.
366 */
367 __dec_zone_page_state(page, NR_FILE_PAGES);
368 __inc_zone_page_state(newpage, NR_FILE_PAGES);
Andrea Arcangeli99a15e22011-06-16 12:56:19 -0700369 if (!PageSwapCache(page) && PageSwapBacked(page)) {
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700370 __dec_zone_page_state(page, NR_SHMEM);
371 __inc_zone_page_state(newpage, NR_SHMEM);
372 }
Nick Piggin19fd6232008-07-25 19:45:32 -0700373 spin_unlock_irq(&mapping->tree_lock);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800374
375 return 0;
376}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800377
378/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900379 * The expected number of remaining references is the same as that
380 * of migrate_page_move_mapping().
381 */
382int migrate_huge_page_move_mapping(struct address_space *mapping,
383 struct page *newpage, struct page *page)
384{
385 int expected_count;
386 void **pslot;
387
388 if (!mapping) {
389 if (page_count(page) != 1)
390 return -EAGAIN;
391 return 0;
392 }
393
394 spin_lock_irq(&mapping->tree_lock);
395
396 pslot = radix_tree_lookup_slot(&mapping->page_tree,
397 page_index(page));
398
399 expected_count = 2 + page_has_private(page);
400 if (page_count(page) != expected_count ||
Mel Gorman29c1f672011-01-13 15:47:21 -0800401 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900402 spin_unlock_irq(&mapping->tree_lock);
403 return -EAGAIN;
404 }
405
406 if (!page_freeze_refs(page, expected_count)) {
407 spin_unlock_irq(&mapping->tree_lock);
408 return -EAGAIN;
409 }
410
411 get_page(newpage);
412
413 radix_tree_replace_slot(pslot, newpage);
414
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800415 page_unfreeze_refs(page, expected_count - 1);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900416
417 spin_unlock_irq(&mapping->tree_lock);
418 return 0;
419}
420
421/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800422 * Copy the page to its new location
423 */
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900424void migrate_page_copy(struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800425{
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900426 if (PageHuge(page))
427 copy_huge_page(newpage, page);
428 else
429 copy_highpage(newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800430
431 if (PageError(page))
432 SetPageError(newpage);
433 if (PageReferenced(page))
434 SetPageReferenced(newpage);
435 if (PageUptodate(page))
436 SetPageUptodate(newpage);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700437 if (TestClearPageActive(page)) {
438 VM_BUG_ON(PageUnevictable(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800439 SetPageActive(newpage);
Lee Schermerhorn418b27e2009-12-14 17:59:54 -0800440 } else if (TestClearPageUnevictable(page))
441 SetPageUnevictable(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800442 if (PageChecked(page))
443 SetPageChecked(newpage);
444 if (PageMappedToDisk(page))
445 SetPageMappedToDisk(newpage);
446
447 if (PageDirty(page)) {
448 clear_page_dirty_for_io(page);
Nick Piggin3a902c52008-04-30 00:55:16 -0700449 /*
450 * Want to mark the page and the radix tree as dirty, and
451 * redo the accounting that clear_page_dirty_for_io undid,
452 * but we can't use set_page_dirty because that function
453 * is actually a signal that all of the page has become dirty.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300454 * Whereas only part of our page may be dirty.
Nick Piggin3a902c52008-04-30 00:55:16 -0700455 */
456 __set_page_dirty_nobuffers(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800457 }
458
Nick Pigginb291f002008-10-18 20:26:44 -0700459 mlock_migrate_page(newpage, page);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800460 ksm_migrate_page(newpage, page);
Nick Pigginb291f002008-10-18 20:26:44 -0700461
Christoph Lameterb20a3502006-03-22 00:09:12 -0800462 ClearPageSwapCache(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800463 ClearPagePrivate(page);
464 set_page_private(page, 0);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800465
466 /*
467 * If any waiters have accumulated on the new page then
468 * wake them up.
469 */
470 if (PageWriteback(newpage))
471 end_page_writeback(newpage);
472}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800473
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700474/************************************************************
475 * Migration functions
476 ***********************************************************/
477
478/* Always fail migration. Used for mappings that are not movable */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700479int fail_migrate_page(struct address_space *mapping,
480 struct page *newpage, struct page *page)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700481{
482 return -EIO;
483}
484EXPORT_SYMBOL(fail_migrate_page);
485
Christoph Lameterb20a3502006-03-22 00:09:12 -0800486/*
487 * Common logic to directly migrate a single page suitable for
David Howells266cf652009-04-03 16:42:36 +0100488 * pages that do not use PagePrivate/PagePrivate2.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800489 *
490 * Pages are locked upon entry and exit.
491 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700492int migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800493 struct page *newpage, struct page *page,
494 enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800495{
496 int rc;
497
498 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
499
Mel Gormana6bc32b2012-01-12 17:19:43 -0800500 rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800501
502 if (rc)
503 return rc;
504
505 migrate_page_copy(newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800506 return 0;
507}
508EXPORT_SYMBOL(migrate_page);
509
David Howells93614012006-09-30 20:45:40 +0200510#ifdef CONFIG_BLOCK
Christoph Lameterb20a3502006-03-22 00:09:12 -0800511/*
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700512 * Migration function for pages with buffers. This function can only be used
513 * if the underlying filesystem guarantees that no other references to "page"
514 * exist.
515 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700516int buffer_migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800517 struct page *newpage, struct page *page, enum migrate_mode mode)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700518{
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700519 struct buffer_head *bh, *head;
520 int rc;
521
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700522 if (!page_has_buffers(page))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800523 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700524
525 head = page_buffers(page);
526
Mel Gormana6bc32b2012-01-12 17:19:43 -0800527 rc = migrate_page_move_mapping(mapping, newpage, page, head, mode);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700528
529 if (rc)
530 return rc;
531
Mel Gormanb969c4a2012-01-12 17:19:34 -0800532 /*
533 * In the async case, migrate_page_move_mapping locked the buffers
534 * with an IRQ-safe spinlock held. In the sync case, the buffers
535 * need to be locked now
536 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800537 if (mode != MIGRATE_ASYNC)
538 BUG_ON(!buffer_migrate_lock_buffers(head, mode));
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700539
540 ClearPagePrivate(page);
541 set_page_private(newpage, page_private(page));
542 set_page_private(page, 0);
543 put_page(page);
544 get_page(newpage);
545
546 bh = head;
547 do {
548 set_bh_page(bh, newpage, bh_offset(bh));
549 bh = bh->b_this_page;
550
551 } while (bh != head);
552
553 SetPagePrivate(newpage);
554
555 migrate_page_copy(newpage, page);
556
557 bh = head;
558 do {
559 unlock_buffer(bh);
560 put_bh(bh);
561 bh = bh->b_this_page;
562
563 } while (bh != head);
564
565 return 0;
566}
567EXPORT_SYMBOL(buffer_migrate_page);
David Howells93614012006-09-30 20:45:40 +0200568#endif
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700569
Christoph Lameter04e62a22006-06-23 02:03:38 -0700570/*
571 * Writeback a page to clean the dirty state
572 */
573static int writeout(struct address_space *mapping, struct page *page)
574{
575 struct writeback_control wbc = {
576 .sync_mode = WB_SYNC_NONE,
577 .nr_to_write = 1,
578 .range_start = 0,
579 .range_end = LLONG_MAX,
Christoph Lameter04e62a22006-06-23 02:03:38 -0700580 .for_reclaim = 1
581 };
582 int rc;
583
584 if (!mapping->a_ops->writepage)
585 /* No write method for the address space */
586 return -EINVAL;
587
588 if (!clear_page_dirty_for_io(page))
589 /* Someone else already triggered a write */
590 return -EAGAIN;
591
592 /*
593 * A dirty page may imply that the underlying filesystem has
594 * the page on some queue. So the page must be clean for
595 * migration. Writeout may mean we loose the lock and the
596 * page state is no longer what we checked for earlier.
597 * At this point we know that the migration attempt cannot
598 * be successful.
599 */
600 remove_migration_ptes(page, page);
601
602 rc = mapping->a_ops->writepage(page, &wbc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700603
604 if (rc != AOP_WRITEPAGE_ACTIVATE)
605 /* unlocked. Relock */
606 lock_page(page);
607
Hugh Dickinsbda85502008-11-19 15:36:36 -0800608 return (rc < 0) ? -EIO : -EAGAIN;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700609}
610
611/*
612 * Default handling if a filesystem does not provide a migration function.
613 */
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700614static int fallback_migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800615 struct page *newpage, struct page *page, enum migrate_mode mode)
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700616{
Mel Gormanb969c4a2012-01-12 17:19:34 -0800617 if (PageDirty(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800618 /* Only writeback pages in full synchronous migration */
619 if (mode != MIGRATE_SYNC)
Mel Gormanb969c4a2012-01-12 17:19:34 -0800620 return -EBUSY;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700621 return writeout(mapping, page);
Mel Gormanb969c4a2012-01-12 17:19:34 -0800622 }
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700623
624 /*
625 * Buffers may be managed in a filesystem specific way.
626 * We must have no buffers or drop them.
627 */
David Howells266cf652009-04-03 16:42:36 +0100628 if (page_has_private(page) &&
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700629 !try_to_release_page(page, GFP_KERNEL))
630 return -EAGAIN;
631
Mel Gormana6bc32b2012-01-12 17:19:43 -0800632 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700633}
634
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700635/*
Christoph Lametere24f0b82006-06-23 02:03:51 -0700636 * Move a page to a newly allocated page
637 * The page is locked and all ptes have been successfully removed.
638 *
639 * The new page will have replaced the old page if this function
640 * is successful.
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700641 *
642 * Return value:
643 * < 0 - error code
644 * == 0 - success
Christoph Lametere24f0b82006-06-23 02:03:51 -0700645 */
Mel Gorman3fe20112010-05-24 14:32:20 -0700646static int move_to_new_page(struct page *newpage, struct page *page,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800647 int remap_swapcache, enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700648{
649 struct address_space *mapping;
650 int rc;
651
652 /*
653 * Block others from accessing the page when we get around to
654 * establishing additional references. We are the only one
655 * holding a reference to the new page at this point.
656 */
Nick Piggin529ae9a2008-08-02 12:01:03 +0200657 if (!trylock_page(newpage))
Christoph Lametere24f0b82006-06-23 02:03:51 -0700658 BUG();
659
660 /* Prepare mapping for the new page.*/
661 newpage->index = page->index;
662 newpage->mapping = page->mapping;
Rik van Rielb2e18532008-10-18 20:26:30 -0700663 if (PageSwapBacked(page))
664 SetPageSwapBacked(newpage);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700665
666 mapping = page_mapping(page);
667 if (!mapping)
Mel Gormana6bc32b2012-01-12 17:19:43 -0800668 rc = migrate_page(mapping, newpage, page, mode);
Mel Gormanb969c4a2012-01-12 17:19:34 -0800669 else if (mapping->a_ops->migratepage)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700670 /*
Mel Gormanb969c4a2012-01-12 17:19:34 -0800671 * Most pages have a mapping and most filesystems provide a
672 * migratepage callback. Anonymous pages are part of swap
673 * space which also has its own migratepage callback. This
674 * is the most common path for page migration.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700675 */
Mel Gormanb969c4a2012-01-12 17:19:34 -0800676 rc = mapping->a_ops->migratepage(mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800677 newpage, page, mode);
Mel Gormanb969c4a2012-01-12 17:19:34 -0800678 else
Mel Gormana6bc32b2012-01-12 17:19:43 -0800679 rc = fallback_migrate_page(mapping, newpage, page, mode);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700680
Mel Gorman3fe20112010-05-24 14:32:20 -0700681 if (rc) {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700682 newpage->mapping = NULL;
Mel Gorman3fe20112010-05-24 14:32:20 -0700683 } else {
684 if (remap_swapcache)
685 remove_migration_ptes(page, newpage);
Konstantin Khlebnikov35512ec2012-02-03 15:37:13 -0800686 page->mapping = NULL;
Mel Gorman3fe20112010-05-24 14:32:20 -0700687 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700688
689 unlock_page(newpage);
690
691 return rc;
692}
693
Minchan Kim0dabec92011-10-31 17:06:57 -0700694static int __unmap_and_move(struct page *page, struct page *newpage,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800695 int force, bool offlining, enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700696{
Minchan Kim0dabec92011-10-31 17:06:57 -0700697 int rc = -EAGAIN;
Mel Gorman3fe20112010-05-24 14:32:20 -0700698 int remap_swapcache = 1;
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800699 int charge = 0;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -0700700 struct mem_cgroup *mem;
Mel Gorman3f6c8272010-05-24 14:32:17 -0700701 struct anon_vma *anon_vma = NULL;
Christoph Lameter95a402c2006-06-23 02:03:53 -0700702
Nick Piggin529ae9a2008-08-02 12:01:03 +0200703 if (!trylock_page(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800704 if (!force || mode == MIGRATE_ASYNC)
Minchan Kim0dabec92011-10-31 17:06:57 -0700705 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800706
707 /*
708 * It's not safe for direct compaction to call lock_page.
709 * For example, during page readahead pages are added locked
710 * to the LRU. Later, when the IO completes the pages are
711 * marked uptodate and unlocked. However, the queueing
712 * could be merging multiple pages for one bio (e.g.
713 * mpage_readpages). If an allocation happens for the
714 * second or third page, the process can end up locking
715 * the same page twice and deadlocking. Rather than
716 * trying to be clever about what pages can be locked,
717 * avoid the use of lock_page for direct compaction
718 * altogether.
719 */
720 if (current->flags & PF_MEMALLOC)
Minchan Kim0dabec92011-10-31 17:06:57 -0700721 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800722
Christoph Lametere24f0b82006-06-23 02:03:51 -0700723 lock_page(page);
724 }
725
Hugh Dickins62b61f62009-12-14 17:59:33 -0800726 /*
727 * Only memory hotplug's offline_pages() caller has locked out KSM,
728 * and can safely migrate a KSM page. The other cases have skipped
729 * PageKsm along with PageReserved - but it is only now when we have
730 * the page lock that we can be certain it will not go KSM beneath us
731 * (KSM will not upgrade a page from PageAnon to PageKsm when it sees
732 * its pagecount raised, but only here do we take the page lock which
733 * serializes that).
734 */
735 if (PageKsm(page) && !offlining) {
736 rc = -EBUSY;
737 goto unlock;
738 }
739
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800740 /* charge against new page */
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700741 charge = mem_cgroup_prepare_migration(page, newpage, &mem, GFP_KERNEL);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800742 if (charge == -ENOMEM) {
743 rc = -ENOMEM;
744 goto unlock;
745 }
746 BUG_ON(charge);
747
Christoph Lametere24f0b82006-06-23 02:03:51 -0700748 if (PageWriteback(page)) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700749 /*
Mel Gormana6bc32b2012-01-12 17:19:43 -0800750 * Only in the case of a full syncronous migration is it
751 * necessary to wait for PageWriteback. In the async case,
752 * the retry loop is too short and in the sync-light case,
753 * the overhead of stalling is too much
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700754 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800755 if (mode != MIGRATE_SYNC) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700756 rc = -EBUSY;
757 goto uncharge;
758 }
759 if (!force)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800760 goto uncharge;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700761 wait_on_page_writeback(page);
762 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700763 /*
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700764 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
765 * we cannot notice that anon_vma is freed while we migrates a page.
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800766 * This get_anon_vma() delays freeing anon_vma pointer until the end
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700767 * of migration. File cache pages are no problem because of page_lock()
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700768 * File Caches may use write_page() or lock_page() in migration, then,
769 * just care Anon page here.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700770 */
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700771 if (PageAnon(page)) {
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800772 /*
773 * Only page_lock_anon_vma() understands the subtleties of
774 * getting a hold on an anon_vma from outside one of its mms.
775 */
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700776 anon_vma = page_get_anon_vma(page);
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800777 if (anon_vma) {
778 /*
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700779 * Anon page
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800780 */
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800781 } else if (PageSwapCache(page)) {
Mel Gorman3fe20112010-05-24 14:32:20 -0700782 /*
783 * We cannot be sure that the anon_vma of an unmapped
784 * swapcache page is safe to use because we don't
785 * know in advance if the VMA that this page belonged
786 * to still exists. If the VMA and others sharing the
787 * data have been freed, then the anon_vma could
788 * already be invalid.
789 *
790 * To avoid this possibility, swapcache pages get
791 * migrated but are not remapped when migration
792 * completes
793 */
794 remap_swapcache = 0;
795 } else {
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800796 goto uncharge;
Mel Gorman3fe20112010-05-24 14:32:20 -0700797 }
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700798 }
Shaohua Li62e1c552008-02-04 22:29:33 -0800799
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700800 /*
Shaohua Li62e1c552008-02-04 22:29:33 -0800801 * Corner case handling:
802 * 1. When a new swap-cache page is read into, it is added to the LRU
803 * and treated as swapcache but it has no rmap yet.
804 * Calling try_to_unmap() against a page->mapping==NULL page will
805 * trigger a BUG. So handle it here.
806 * 2. An orphaned page (see truncate_complete_page) might have
807 * fs-private metadata. The page can be picked up due to memory
808 * offlining. Everywhere else except page reclaim, the page is
809 * invisible to the vm, so the page can not be migrated. So try to
810 * free the metadata, so the page can be freed.
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700811 */
Shaohua Li62e1c552008-02-04 22:29:33 -0800812 if (!page->mapping) {
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800813 VM_BUG_ON(PageAnon(page));
814 if (page_has_private(page)) {
Shaohua Li62e1c552008-02-04 22:29:33 -0800815 try_to_free_buffers(page);
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800816 goto uncharge;
Shaohua Li62e1c552008-02-04 22:29:33 -0800817 }
Shaohua Liabfc3482009-09-21 17:01:19 -0700818 goto skip_unmap;
Shaohua Li62e1c552008-02-04 22:29:33 -0800819 }
820
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700821 /* Establish migration ptes or remove ptes */
Andi Kleen14fa31b2009-09-16 11:50:10 +0200822 try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700823
Shaohua Liabfc3482009-09-21 17:01:19 -0700824skip_unmap:
Christoph Lametere6a15302006-06-25 05:46:49 -0700825 if (!page_mapped(page))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800826 rc = move_to_new_page(newpage, page, remap_swapcache, mode);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700827
Mel Gorman3fe20112010-05-24 14:32:20 -0700828 if (rc && remap_swapcache)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700829 remove_migration_ptes(page, page);
Mel Gorman3f6c8272010-05-24 14:32:17 -0700830
831 /* Drop an anon_vma reference if we took one */
Rik van Riel76545062010-08-09 17:18:41 -0700832 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -0700833 put_anon_vma(anon_vma);
Mel Gorman3f6c8272010-05-24 14:32:17 -0700834
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800835uncharge:
836 if (!charge)
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -0800837 mem_cgroup_end_migration(mem, page, newpage, rc == 0);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700838unlock:
839 unlock_page(page);
Minchan Kim0dabec92011-10-31 17:06:57 -0700840out:
841 return rc;
842}
Christoph Lameter95a402c2006-06-23 02:03:53 -0700843
Minchan Kim0dabec92011-10-31 17:06:57 -0700844/*
845 * Obtain the lock on page, remove all ptes and migrate the page
846 * to the newly allocated page in newpage.
847 */
848static int unmap_and_move(new_page_t get_new_page, unsigned long private,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800849 struct page *page, int force, bool offlining,
850 enum migrate_mode mode)
Minchan Kim0dabec92011-10-31 17:06:57 -0700851{
852 int rc = 0;
853 int *result = NULL;
854 struct page *newpage = get_new_page(page, private, &result);
855
856 if (!newpage)
857 return -ENOMEM;
858
859 if (page_count(page) == 1) {
860 /* page was freed from under us. So we are done. */
861 goto out;
862 }
863
864 if (unlikely(PageTransHuge(page)))
865 if (unlikely(split_huge_page(page)))
866 goto out;
867
Mel Gormana6bc32b2012-01-12 17:19:43 -0800868 rc = __unmap_and_move(page, newpage, force, offlining, mode);
Minchan Kim0dabec92011-10-31 17:06:57 -0700869out:
Christoph Lametere24f0b82006-06-23 02:03:51 -0700870 if (rc != -EAGAIN) {
Minchan Kim0dabec92011-10-31 17:06:57 -0700871 /*
872 * A page that has been migrated has all references
873 * removed and will be freed. A page that has not been
874 * migrated will have kepts its references and be
875 * restored.
876 */
877 list_del(&page->lru);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -0700878 dec_zone_page_state(page, NR_ISOLATED_ANON +
Johannes Weiner6c0b1352009-09-21 17:02:59 -0700879 page_is_file_cache(page));
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700880 putback_lru_page(page);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700881 }
Christoph Lameter95a402c2006-06-23 02:03:53 -0700882 /*
883 * Move the new page to the LRU. If migration was not successful
884 * then this will free the page.
885 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700886 putback_lru_page(newpage);
Christoph Lameter742755a2006-06-23 02:03:55 -0700887 if (result) {
888 if (rc)
889 *result = rc;
890 else
891 *result = page_to_nid(newpage);
892 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700893 return rc;
894}
895
896/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900897 * Counterpart of unmap_and_move_page() for hugepage migration.
898 *
899 * This function doesn't wait the completion of hugepage I/O
900 * because there is no race between I/O and migration for hugepage.
901 * Note that currently hugepage I/O occurs only in direct I/O
902 * where no lock is held and PG_writeback is irrelevant,
903 * and writeback status of all subpages are counted in the reference
904 * count of the head page (i.e. if all subpages of a 2MB hugepage are
905 * under direct I/O, the reference of the head page is 512 and a bit more.)
906 * This means that when we try to migrate hugepage whose subpages are
907 * doing direct I/O, some references remain after try_to_unmap() and
908 * hugepage migration fails without data corruption.
909 *
910 * There is also no race when direct I/O is issued on the page under migration,
911 * because then pte is replaced with migration swap entry and direct I/O code
912 * will wait in the page fault for migration to complete.
913 */
914static int unmap_and_move_huge_page(new_page_t get_new_page,
915 unsigned long private, struct page *hpage,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800916 int force, bool offlining,
917 enum migrate_mode mode)
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900918{
919 int rc = 0;
920 int *result = NULL;
921 struct page *new_hpage = get_new_page(hpage, private, &result);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900922 struct anon_vma *anon_vma = NULL;
923
924 if (!new_hpage)
925 return -ENOMEM;
926
927 rc = -EAGAIN;
928
929 if (!trylock_page(hpage)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800930 if (!force || mode != MIGRATE_SYNC)
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900931 goto out;
932 lock_page(hpage);
933 }
934
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700935 if (PageAnon(hpage))
936 anon_vma = page_get_anon_vma(hpage);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900937
938 try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
939
940 if (!page_mapped(hpage))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800941 rc = move_to_new_page(new_hpage, hpage, 1, mode);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900942
943 if (rc)
944 remove_migration_ptes(hpage, hpage);
945
Hugh Dickinsfd4a4662011-01-13 15:47:31 -0800946 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -0700947 put_anon_vma(anon_vma);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900948 unlock_page(hpage);
949
Hillf Danton09761332011-12-08 14:34:20 -0800950out:
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900951 if (rc != -EAGAIN) {
952 list_del(&hpage->lru);
953 put_page(hpage);
954 }
955
956 put_page(new_hpage);
957
958 if (result) {
959 if (rc)
960 *result = rc;
961 else
962 *result = page_to_nid(new_hpage);
963 }
964 return rc;
965}
966
967/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800968 * migrate_pages
969 *
Christoph Lameter95a402c2006-06-23 02:03:53 -0700970 * The function takes one list of pages to migrate and a function
971 * that determines from the page to be migrated and the private data
972 * the target of the move and allocates the page.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800973 *
974 * The function returns after 10 attempts or if no pages
975 * are movable anymore because to has become empty
Minchan Kimcf608ac2010-10-26 14:21:29 -0700976 * or no retryable pages exist anymore.
977 * Caller should call putback_lru_pages to return pages to the LRU
Minchan Kim28bd6572011-01-25 15:07:26 -0800978 * or free list only if ret != 0.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800979 *
Christoph Lameter95a402c2006-06-23 02:03:53 -0700980 * Return: Number of pages not migrated or error code.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800981 */
Christoph Lameter95a402c2006-06-23 02:03:53 -0700982int migrate_pages(struct list_head *from,
Mel Gorman7f0f2492011-01-13 15:45:58 -0800983 new_page_t get_new_page, unsigned long private, bool offlining,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800984 enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800985{
Christoph Lametere24f0b82006-06-23 02:03:51 -0700986 int retry = 1;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800987 int nr_failed = 0;
Mel Gorman7bfd6e42012-10-19 10:46:20 +0100988 int nr_succeeded = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800989 int pass = 0;
990 struct page *page;
991 struct page *page2;
992 int swapwrite = current->flags & PF_SWAPWRITE;
993 int rc;
994
Liam Mark069398a2013-01-16 10:14:40 -0800995 trace_migrate_pages_start(mode);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800996 if (!swapwrite)
997 current->flags |= PF_SWAPWRITE;
998
Christoph Lametere24f0b82006-06-23 02:03:51 -0700999 for(pass = 0; pass < 10 && retry; pass++) {
1000 retry = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001001
Christoph Lametere24f0b82006-06-23 02:03:51 -07001002 list_for_each_entry_safe(page, page2, from, lru) {
Christoph Lametere24f0b82006-06-23 02:03:51 -07001003 cond_resched();
Christoph Lameterb20a3502006-03-22 00:09:12 -08001004
Christoph Lameter95a402c2006-06-23 02:03:53 -07001005 rc = unmap_and_move(get_new_page, private,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001006 page, pass > 2, offlining,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001007 mode);
Christoph Lameterb20a3502006-03-22 00:09:12 -08001008
Christoph Lametere24f0b82006-06-23 02:03:51 -07001009 switch(rc) {
Christoph Lameter95a402c2006-06-23 02:03:53 -07001010 case -ENOMEM:
1011 goto out;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001012 case -EAGAIN:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001013 retry++;
Liam Mark069398a2013-01-16 10:14:40 -08001014 trace_migrate_retry(retry);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001015 break;
1016 case 0:
Mel Gorman7bfd6e42012-10-19 10:46:20 +01001017 nr_succeeded++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001018 break;
1019 default:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001020 /* Permanent failure */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001021 nr_failed++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001022 break;
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001023 }
Christoph Lameterb20a3502006-03-22 00:09:12 -08001024 }
1025 }
Christoph Lameter95a402c2006-06-23 02:03:53 -07001026 rc = 0;
1027out:
Mel Gorman7bfd6e42012-10-19 10:46:20 +01001028 if (nr_succeeded)
1029 count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
1030 if (nr_failed)
1031 count_vm_events(PGMIGRATE_FAIL, nr_failed);
Christoph Lameterb20a3502006-03-22 00:09:12 -08001032 if (!swapwrite)
1033 current->flags &= ~PF_SWAPWRITE;
1034
Liam Mark069398a2013-01-16 10:14:40 -08001035 trace_migrate_pages_end(mode);
Christoph Lameter95a402c2006-06-23 02:03:53 -07001036 if (rc)
1037 return rc;
1038
Christoph Lameterb20a3502006-03-22 00:09:12 -08001039 return nr_failed + retry;
1040}
1041
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001042int migrate_huge_pages(struct list_head *from,
Mel Gorman7f0f2492011-01-13 15:45:58 -08001043 new_page_t get_new_page, unsigned long private, bool offlining,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001044 enum migrate_mode mode)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001045{
1046 int retry = 1;
1047 int nr_failed = 0;
1048 int pass = 0;
1049 struct page *page;
1050 struct page *page2;
1051 int rc;
1052
1053 for (pass = 0; pass < 10 && retry; pass++) {
1054 retry = 0;
1055
1056 list_for_each_entry_safe(page, page2, from, lru) {
1057 cond_resched();
1058
1059 rc = unmap_and_move_huge_page(get_new_page,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001060 private, page, pass > 2, offlining,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001061 mode);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001062
1063 switch(rc) {
1064 case -ENOMEM:
1065 goto out;
1066 case -EAGAIN:
1067 retry++;
1068 break;
1069 case 0:
1070 break;
1071 default:
1072 /* Permanent failure */
1073 nr_failed++;
1074 break;
1075 }
1076 }
1077 }
1078 rc = 0;
1079out:
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001080 if (rc)
1081 return rc;
1082
1083 return nr_failed + retry;
1084}
1085
Christoph Lameter742755a2006-06-23 02:03:55 -07001086#ifdef CONFIG_NUMA
1087/*
1088 * Move a list of individual pages
1089 */
1090struct page_to_node {
1091 unsigned long addr;
1092 struct page *page;
1093 int node;
1094 int status;
1095};
1096
1097static struct page *new_page_node(struct page *p, unsigned long private,
1098 int **result)
1099{
1100 struct page_to_node *pm = (struct page_to_node *)private;
1101
1102 while (pm->node != MAX_NUMNODES && pm->page != p)
1103 pm++;
1104
1105 if (pm->node == MAX_NUMNODES)
1106 return NULL;
1107
1108 *result = &pm->status;
1109
Mel Gorman6484eb32009-06-16 15:31:54 -07001110 return alloc_pages_exact_node(pm->node,
Mel Gorman769848c2007-07-17 04:03:05 -07001111 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
Christoph Lameter742755a2006-06-23 02:03:55 -07001112}
1113
1114/*
1115 * Move a set of pages as indicated in the pm array. The addr
1116 * field must be set to the virtual address of the page to be moved
1117 * and the node number must contain a valid target node.
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001118 * The pm array ends with node = MAX_NUMNODES.
Christoph Lameter742755a2006-06-23 02:03:55 -07001119 */
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001120static int do_move_page_to_node_array(struct mm_struct *mm,
1121 struct page_to_node *pm,
1122 int migrate_all)
Christoph Lameter742755a2006-06-23 02:03:55 -07001123{
1124 int err;
1125 struct page_to_node *pp;
1126 LIST_HEAD(pagelist);
1127
1128 down_read(&mm->mmap_sem);
1129
1130 /*
1131 * Build a list of pages to migrate
1132 */
Christoph Lameter742755a2006-06-23 02:03:55 -07001133 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
1134 struct vm_area_struct *vma;
1135 struct page *page;
1136
Christoph Lameter742755a2006-06-23 02:03:55 -07001137 err = -EFAULT;
1138 vma = find_vma(mm, pp->addr);
Gleb Natapov70384dc2010-10-26 14:22:07 -07001139 if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
Christoph Lameter742755a2006-06-23 02:03:55 -07001140 goto set_status;
1141
Andrea Arcangeli500d65d2011-01-13 15:46:55 -08001142 page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001143
1144 err = PTR_ERR(page);
1145 if (IS_ERR(page))
1146 goto set_status;
1147
Christoph Lameter742755a2006-06-23 02:03:55 -07001148 err = -ENOENT;
1149 if (!page)
1150 goto set_status;
1151
Hugh Dickins62b61f62009-12-14 17:59:33 -08001152 /* Use PageReserved to check for zero page */
1153 if (PageReserved(page) || PageKsm(page))
Christoph Lameter742755a2006-06-23 02:03:55 -07001154 goto put_and_set;
1155
1156 pp->page = page;
1157 err = page_to_nid(page);
1158
1159 if (err == pp->node)
1160 /*
1161 * Node already in the right place
1162 */
1163 goto put_and_set;
1164
1165 err = -EACCES;
1166 if (page_mapcount(page) > 1 &&
1167 !migrate_all)
1168 goto put_and_set;
1169
Nick Piggin62695a82008-10-18 20:26:09 -07001170 err = isolate_lru_page(page);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001171 if (!err) {
Nick Piggin62695a82008-10-18 20:26:09 -07001172 list_add_tail(&page->lru, &pagelist);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001173 inc_zone_page_state(page, NR_ISOLATED_ANON +
1174 page_is_file_cache(page));
1175 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001176put_and_set:
1177 /*
1178 * Either remove the duplicate refcount from
1179 * isolate_lru_page() or drop the page ref if it was
1180 * not isolated.
1181 */
1182 put_page(page);
1183set_status:
1184 pp->status = err;
1185 }
1186
Brice Gogline78bbfa2008-10-18 20:27:15 -07001187 err = 0;
Minchan Kimcf608ac2010-10-26 14:21:29 -07001188 if (!list_empty(&pagelist)) {
Christoph Lameter742755a2006-06-23 02:03:55 -07001189 err = migrate_pages(&pagelist, new_page_node,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001190 (unsigned long)pm, 0, MIGRATE_SYNC);
Minchan Kimcf608ac2010-10-26 14:21:29 -07001191 if (err)
1192 putback_lru_pages(&pagelist);
1193 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001194
1195 up_read(&mm->mmap_sem);
1196 return err;
1197}
1198
1199/*
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001200 * Migrate an array of page address onto an array of nodes and fill
1201 * the corresponding array of status.
1202 */
Christoph Lameter3268c632012-03-21 16:34:06 -07001203static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001204 unsigned long nr_pages,
1205 const void __user * __user *pages,
1206 const int __user *nodes,
1207 int __user *status, int flags)
1208{
Brice Goglin3140a222009-01-06 14:38:57 -08001209 struct page_to_node *pm;
Brice Goglin3140a222009-01-06 14:38:57 -08001210 unsigned long chunk_nr_pages;
1211 unsigned long chunk_start;
1212 int err;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001213
Brice Goglin3140a222009-01-06 14:38:57 -08001214 err = -ENOMEM;
1215 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
1216 if (!pm)
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001217 goto out;
Brice Goglin35282a22009-06-16 15:32:43 -07001218
1219 migrate_prep();
1220
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001221 /*
Brice Goglin3140a222009-01-06 14:38:57 -08001222 * Store a chunk of page_to_node array in a page,
1223 * but keep the last one as a marker
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001224 */
Brice Goglin3140a222009-01-06 14:38:57 -08001225 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001226
Brice Goglin3140a222009-01-06 14:38:57 -08001227 for (chunk_start = 0;
1228 chunk_start < nr_pages;
1229 chunk_start += chunk_nr_pages) {
1230 int j;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001231
Brice Goglin3140a222009-01-06 14:38:57 -08001232 if (chunk_start + chunk_nr_pages > nr_pages)
1233 chunk_nr_pages = nr_pages - chunk_start;
1234
1235 /* fill the chunk pm with addrs and nodes from user-space */
1236 for (j = 0; j < chunk_nr_pages; j++) {
1237 const void __user *p;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001238 int node;
1239
Brice Goglin3140a222009-01-06 14:38:57 -08001240 err = -EFAULT;
1241 if (get_user(p, pages + j + chunk_start))
1242 goto out_pm;
1243 pm[j].addr = (unsigned long) p;
1244
1245 if (get_user(node, nodes + j + chunk_start))
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001246 goto out_pm;
1247
1248 err = -ENODEV;
Linus Torvalds6f5a55f2010-02-05 16:16:50 -08001249 if (node < 0 || node >= MAX_NUMNODES)
1250 goto out_pm;
1251
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001252 if (!node_state(node, N_HIGH_MEMORY))
1253 goto out_pm;
1254
1255 err = -EACCES;
1256 if (!node_isset(node, task_nodes))
1257 goto out_pm;
1258
Brice Goglin3140a222009-01-06 14:38:57 -08001259 pm[j].node = node;
1260 }
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001261
Brice Goglin3140a222009-01-06 14:38:57 -08001262 /* End marker for this chunk */
1263 pm[chunk_nr_pages].node = MAX_NUMNODES;
1264
1265 /* Migrate this chunk */
1266 err = do_move_page_to_node_array(mm, pm,
1267 flags & MPOL_MF_MOVE_ALL);
1268 if (err < 0)
1269 goto out_pm;
1270
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001271 /* Return status information */
Brice Goglin3140a222009-01-06 14:38:57 -08001272 for (j = 0; j < chunk_nr_pages; j++)
1273 if (put_user(pm[j].status, status + j + chunk_start)) {
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001274 err = -EFAULT;
Brice Goglin3140a222009-01-06 14:38:57 -08001275 goto out_pm;
1276 }
1277 }
1278 err = 0;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001279
1280out_pm:
Brice Goglin3140a222009-01-06 14:38:57 -08001281 free_page((unsigned long)pm);
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001282out:
1283 return err;
1284}
1285
1286/*
Brice Goglin2f007e72008-10-18 20:27:16 -07001287 * Determine the nodes of an array of pages and store it in an array of status.
Christoph Lameter742755a2006-06-23 02:03:55 -07001288 */
Brice Goglin80bba122008-12-09 13:14:23 -08001289static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1290 const void __user **pages, int *status)
Christoph Lameter742755a2006-06-23 02:03:55 -07001291{
Brice Goglin2f007e72008-10-18 20:27:16 -07001292 unsigned long i;
Brice Goglin2f007e72008-10-18 20:27:16 -07001293
Christoph Lameter742755a2006-06-23 02:03:55 -07001294 down_read(&mm->mmap_sem);
1295
Brice Goglin2f007e72008-10-18 20:27:16 -07001296 for (i = 0; i < nr_pages; i++) {
Brice Goglin80bba122008-12-09 13:14:23 -08001297 unsigned long addr = (unsigned long)(*pages);
Christoph Lameter742755a2006-06-23 02:03:55 -07001298 struct vm_area_struct *vma;
1299 struct page *page;
KOSAKI Motohiroc095adb2008-12-16 16:06:43 +09001300 int err = -EFAULT;
Brice Goglin2f007e72008-10-18 20:27:16 -07001301
1302 vma = find_vma(mm, addr);
Gleb Natapov70384dc2010-10-26 14:22:07 -07001303 if (!vma || addr < vma->vm_start)
Christoph Lameter742755a2006-06-23 02:03:55 -07001304 goto set_status;
1305
Brice Goglin2f007e72008-10-18 20:27:16 -07001306 page = follow_page(vma, addr, 0);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001307
1308 err = PTR_ERR(page);
1309 if (IS_ERR(page))
1310 goto set_status;
1311
Christoph Lameter742755a2006-06-23 02:03:55 -07001312 err = -ENOENT;
1313 /* Use PageReserved to check for zero page */
Hugh Dickins62b61f62009-12-14 17:59:33 -08001314 if (!page || PageReserved(page) || PageKsm(page))
Christoph Lameter742755a2006-06-23 02:03:55 -07001315 goto set_status;
1316
1317 err = page_to_nid(page);
1318set_status:
Brice Goglin80bba122008-12-09 13:14:23 -08001319 *status = err;
1320
1321 pages++;
1322 status++;
1323 }
1324
1325 up_read(&mm->mmap_sem);
1326}
1327
1328/*
1329 * Determine the nodes of a user array of pages and store it in
1330 * a user array of status.
1331 */
1332static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1333 const void __user * __user *pages,
1334 int __user *status)
1335{
1336#define DO_PAGES_STAT_CHUNK_NR 16
1337 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1338 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
Brice Goglin80bba122008-12-09 13:14:23 -08001339
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001340 while (nr_pages) {
1341 unsigned long chunk_nr;
Brice Goglin80bba122008-12-09 13:14:23 -08001342
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001343 chunk_nr = nr_pages;
1344 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1345 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1346
1347 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
1348 break;
Brice Goglin80bba122008-12-09 13:14:23 -08001349
1350 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1351
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001352 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1353 break;
Christoph Lameter742755a2006-06-23 02:03:55 -07001354
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001355 pages += chunk_nr;
1356 status += chunk_nr;
1357 nr_pages -= chunk_nr;
1358 }
1359 return nr_pages ? -EFAULT : 0;
Christoph Lameter742755a2006-06-23 02:03:55 -07001360}
1361
1362/*
1363 * Move a list of pages in the address space of the currently executing
1364 * process.
1365 */
Heiko Carstens938bb9f2009-01-14 14:14:30 +01001366SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1367 const void __user * __user *, pages,
1368 const int __user *, nodes,
1369 int __user *, status, int, flags)
Christoph Lameter742755a2006-06-23 02:03:55 -07001370{
David Howellsc69e8d92008-11-14 10:39:19 +11001371 const struct cred *cred = current_cred(), *tcred;
Christoph Lameter742755a2006-06-23 02:03:55 -07001372 struct task_struct *task;
Christoph Lameter742755a2006-06-23 02:03:55 -07001373 struct mm_struct *mm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001374 int err;
Christoph Lameter3268c632012-03-21 16:34:06 -07001375 nodemask_t task_nodes;
Christoph Lameter742755a2006-06-23 02:03:55 -07001376
1377 /* Check flags */
1378 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1379 return -EINVAL;
1380
1381 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1382 return -EPERM;
1383
1384 /* Find the mm_struct */
Greg Thelena879bf52011-02-25 14:44:13 -08001385 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07001386 task = pid ? find_task_by_vpid(pid) : current;
Christoph Lameter742755a2006-06-23 02:03:55 -07001387 if (!task) {
Greg Thelena879bf52011-02-25 14:44:13 -08001388 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001389 return -ESRCH;
1390 }
Christoph Lameter3268c632012-03-21 16:34:06 -07001391 get_task_struct(task);
Christoph Lameter742755a2006-06-23 02:03:55 -07001392
1393 /*
1394 * Check if this process has the right to modify the specified
1395 * process. The right exists if the process has administrative
1396 * capabilities, superuser privileges or the same
1397 * userid as the target process.
1398 */
David Howellsc69e8d92008-11-14 10:39:19 +11001399 tcred = __task_cred(task);
David Howellsb6dff3e2008-11-14 10:39:16 +11001400 if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
1401 cred->uid != tcred->suid && cred->uid != tcred->uid &&
Christoph Lameter742755a2006-06-23 02:03:55 -07001402 !capable(CAP_SYS_NICE)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001403 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001404 err = -EPERM;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001405 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001406 }
David Howellsc69e8d92008-11-14 10:39:19 +11001407 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001408
David Quigley86c3a762006-06-23 02:04:02 -07001409 err = security_task_movememory(task);
1410 if (err)
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001411 goto out;
David Quigley86c3a762006-06-23 02:04:02 -07001412
Christoph Lameter3268c632012-03-21 16:34:06 -07001413 task_nodes = cpuset_mems_allowed(task);
1414 mm = get_task_mm(task);
1415 put_task_struct(task);
1416
Sasha Levin6e8b09e2012-04-25 16:01:53 -07001417 if (!mm)
1418 return -EINVAL;
1419
1420 if (nodes)
1421 err = do_pages_move(mm, task_nodes, nr_pages, pages,
1422 nodes, status, flags);
1423 else
1424 err = do_pages_stat(mm, nr_pages, pages, status);
Christoph Lameter3268c632012-03-21 16:34:06 -07001425
1426 mmput(mm);
1427 return err;
David Quigley86c3a762006-06-23 02:04:02 -07001428
Christoph Lameter742755a2006-06-23 02:03:55 -07001429out:
Christoph Lameter3268c632012-03-21 16:34:06 -07001430 put_task_struct(task);
Christoph Lameter742755a2006-06-23 02:03:55 -07001431 return err;
1432}
Christoph Lameter742755a2006-06-23 02:03:55 -07001433
Christoph Lameter7b2259b2006-06-25 05:46:48 -07001434/*
1435 * Call migration functions in the vma_ops that may prepare
1436 * memory in a vm for migration. migration functions may perform
1437 * the migration for vmas that do not have an underlying page struct.
1438 */
1439int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1440 const nodemask_t *from, unsigned long flags)
1441{
1442 struct vm_area_struct *vma;
1443 int err = 0;
1444
Daisuke Nishimura1001c9f2009-02-11 13:04:18 -08001445 for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
Christoph Lameter7b2259b2006-06-25 05:46:48 -07001446 if (vma->vm_ops && vma->vm_ops->migrate) {
1447 err = vma->vm_ops->migrate(vma, to, from, flags);
1448 if (err)
1449 break;
1450 }
1451 }
1452 return err;
1453}
Gerald Schaefer83d16742008-07-23 21:28:22 -07001454#endif