blob: b114635962dc4c8eb90201db31d3ccb5f28f7d36 [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>
16#include <linux/module.h>
17#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/gfp.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080036
37#include "internal.h"
38
Christoph Lameterb20a3502006-03-22 00:09:12 -080039#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
40
41/*
Christoph Lameter742755a2006-06-23 02:03:55 -070042 * migrate_prep() needs to be called before we start compiling a list of pages
43 * to be migrated using isolate_lru_page().
Christoph Lameterb20a3502006-03-22 00:09:12 -080044 */
45int migrate_prep(void)
46{
Christoph Lameterb20a3502006-03-22 00:09:12 -080047 /*
48 * Clear the LRU lists so pages can be isolated.
49 * Note that pages may be moved off the LRU after we have
50 * drained them. Those pages will fail to migrate like other
51 * pages that may be busy.
52 */
53 lru_add_drain_all();
54
55 return 0;
56}
57
Christoph Lameterb20a3502006-03-22 00:09:12 -080058/*
Lee Schermerhorn894bc312008-10-18 20:26:39 -070059 * Add isolated pages on the list back to the LRU under page lock
60 * to avoid leaking evictable pages back onto unevictable list.
Christoph Lameterb20a3502006-03-22 00:09:12 -080061 */
Minchan Kime13861d2010-05-24 14:31:59 -070062void putback_lru_pages(struct list_head *l)
Christoph Lameterb20a3502006-03-22 00:09:12 -080063{
64 struct page *page;
65 struct page *page2;
Christoph Lameterb20a3502006-03-22 00:09:12 -080066
67 list_for_each_entry_safe(page, page2, l, lru) {
Christoph Lametere24f0b82006-06-23 02:03:51 -070068 list_del(&page->lru);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -070069 dec_zone_page_state(page, NR_ISOLATED_ANON +
Johannes Weiner6c0b1352009-09-21 17:02:59 -070070 page_is_file_cache(page));
Lee Schermerhorn894bc312008-10-18 20:26:39 -070071 putback_lru_page(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -080072 }
Christoph Lameterb20a3502006-03-22 00:09:12 -080073}
74
Christoph Lameter06972122006-06-23 02:03:35 -070075/*
76 * Restore a potential migration pte to a working pte entry
77 */
Hugh Dickinse9995ef2009-12-14 17:59:31 -080078static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,
79 unsigned long addr, void *old)
Christoph Lameter06972122006-06-23 02:03:35 -070080{
81 struct mm_struct *mm = vma->vm_mm;
82 swp_entry_t entry;
83 pgd_t *pgd;
84 pud_t *pud;
85 pmd_t *pmd;
86 pte_t *ptep, pte;
87 spinlock_t *ptl;
88
89 pgd = pgd_offset(mm, addr);
90 if (!pgd_present(*pgd))
Hugh Dickinse9995ef2009-12-14 17:59:31 -080091 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -070092
93 pud = pud_offset(pgd, addr);
94 if (!pud_present(*pud))
Hugh Dickinse9995ef2009-12-14 17:59:31 -080095 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -070096
97 pmd = pmd_offset(pud, addr);
98 if (!pmd_present(*pmd))
Hugh Dickinse9995ef2009-12-14 17:59:31 -080099 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700100
101 ptep = pte_offset_map(pmd, addr);
102
103 if (!is_swap_pte(*ptep)) {
104 pte_unmap(ptep);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800105 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700106 }
107
108 ptl = pte_lockptr(mm, pmd);
109 spin_lock(ptl);
110 pte = *ptep;
111 if (!is_swap_pte(pte))
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800112 goto unlock;
Christoph Lameter06972122006-06-23 02:03:35 -0700113
114 entry = pte_to_swp_entry(pte);
115
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800116 if (!is_migration_entry(entry) ||
117 migration_entry_to_page(entry) != old)
118 goto unlock;
Christoph Lameter06972122006-06-23 02:03:35 -0700119
Christoph Lameter06972122006-06-23 02:03:35 -0700120 get_page(new);
121 pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
122 if (is_write_migration_entry(entry))
123 pte = pte_mkwrite(pte);
KAMEZAWA Hiroyuki97ee0522007-10-16 01:25:43 -0700124 flush_cache_page(vma, addr, pte_pfn(pte));
Christoph Lameter06972122006-06-23 02:03:35 -0700125 set_pte_at(mm, addr, ptep, pte);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700126
127 if (PageAnon(new))
128 page_add_anon_rmap(new, vma, addr);
129 else
130 page_add_file_rmap(new);
131
132 /* No need to invalidate - it was non-present before */
Russell King4b3073e2009-12-18 16:40:18 +0000133 update_mmu_cache(vma, addr, ptep);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800134unlock:
Christoph Lameter06972122006-06-23 02:03:35 -0700135 pte_unmap_unlock(ptep, ptl);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800136out:
137 return SWAP_AGAIN;
Christoph Lameter06972122006-06-23 02:03:35 -0700138}
139
140/*
Christoph Lameter04e62a22006-06-23 02:03:38 -0700141 * Get rid of all migration entries and replace them by
142 * references to the indicated page.
143 */
144static void remove_migration_ptes(struct page *old, struct page *new)
145{
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800146 rmap_walk(new, remove_migration_pte, old);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700147}
148
149/*
Christoph Lameter06972122006-06-23 02:03:35 -0700150 * Something used the pte of a page under migration. We need to
151 * get to the page and wait until migration is finished.
152 * When we return from this function the fault will be retried.
153 *
154 * This function is called from do_swap_page().
155 */
156void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
157 unsigned long address)
158{
159 pte_t *ptep, pte;
160 spinlock_t *ptl;
161 swp_entry_t entry;
162 struct page *page;
163
164 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
165 pte = *ptep;
166 if (!is_swap_pte(pte))
167 goto out;
168
169 entry = pte_to_swp_entry(pte);
170 if (!is_migration_entry(entry))
171 goto out;
172
173 page = migration_entry_to_page(entry);
174
Nick Piggine2867812008-07-25 19:45:30 -0700175 /*
176 * Once radix-tree replacement of page migration started, page_count
177 * *must* be zero. And, we don't want to call wait_on_page_locked()
178 * against a page without get_page().
179 * So, we use get_page_unless_zero(), here. Even failed, page fault
180 * will occur again.
181 */
182 if (!get_page_unless_zero(page))
183 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700184 pte_unmap_unlock(ptep, ptl);
185 wait_on_page_locked(page);
186 put_page(page);
187 return;
188out:
189 pte_unmap_unlock(ptep, ptl);
190}
191
Christoph Lameterb20a3502006-03-22 00:09:12 -0800192/*
Christoph Lameterc3fcf8a2006-06-23 02:03:32 -0700193 * Replace the page in the mapping.
Christoph Lameter5b5c7122006-06-23 02:03:29 -0700194 *
195 * The number of remaining references must be:
196 * 1 for anonymous pages without a mapping
197 * 2 for pages with a mapping
David Howells266cf652009-04-03 16:42:36 +0100198 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800199 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700200static int migrate_page_move_mapping(struct address_space *mapping,
201 struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800202{
Nick Piggine2867812008-07-25 19:45:30 -0700203 int expected_count;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800204 void **pslot;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800205
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700206 if (!mapping) {
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700207 /* Anonymous page without mapping */
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700208 if (page_count(page) != 1)
209 return -EAGAIN;
210 return 0;
211 }
212
Nick Piggin19fd6232008-07-25 19:45:32 -0700213 spin_lock_irq(&mapping->tree_lock);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800214
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800215 pslot = radix_tree_lookup_slot(&mapping->page_tree,
216 page_index(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800217
Johannes Weineredcf4742009-09-21 17:02:59 -0700218 expected_count = 2 + page_has_private(page);
Nick Piggine2867812008-07-25 19:45:30 -0700219 if (page_count(page) != expected_count ||
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800220 (struct page *)radix_tree_deref_slot(pslot) != page) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700221 spin_unlock_irq(&mapping->tree_lock);
Christoph Lametere23ca002006-04-10 22:52:57 -0700222 return -EAGAIN;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800223 }
224
Nick Piggine2867812008-07-25 19:45:30 -0700225 if (!page_freeze_refs(page, expected_count)) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700226 spin_unlock_irq(&mapping->tree_lock);
Nick Piggine2867812008-07-25 19:45:30 -0700227 return -EAGAIN;
228 }
229
Christoph Lameterb20a3502006-03-22 00:09:12 -0800230 /*
231 * Now we know that no one else is looking at the page.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800232 */
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800233 get_page(newpage); /* add cache reference */
Christoph Lameterb20a3502006-03-22 00:09:12 -0800234 if (PageSwapCache(page)) {
235 SetPageSwapCache(newpage);
236 set_page_private(newpage, page_private(page));
237 }
238
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800239 radix_tree_replace_slot(pslot, newpage);
240
Nick Piggine2867812008-07-25 19:45:30 -0700241 page_unfreeze_refs(page, expected_count);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800242 /*
243 * Drop cache reference from old page.
244 * We know this isn't the last reference.
245 */
Christoph Lameterb20a3502006-03-22 00:09:12 -0800246 __put_page(page);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800247
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700248 /*
249 * If moved to a different zone then also account
250 * the page for that zone. Other VM counters will be
251 * taken care of when we establish references to the
252 * new page and drop references to the old page.
253 *
254 * Note that anonymous pages are accounted for
255 * via NR_FILE_PAGES and NR_ANON_PAGES if they
256 * are mapped to swap space.
257 */
258 __dec_zone_page_state(page, NR_FILE_PAGES);
259 __inc_zone_page_state(newpage, NR_FILE_PAGES);
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700260 if (PageSwapBacked(page)) {
261 __dec_zone_page_state(page, NR_SHMEM);
262 __inc_zone_page_state(newpage, NR_SHMEM);
263 }
Nick Piggin19fd6232008-07-25 19:45:32 -0700264 spin_unlock_irq(&mapping->tree_lock);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800265
266 return 0;
267}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800268
269/*
270 * Copy the page to its new location
271 */
Christoph Lametere7340f72006-06-23 02:03:29 -0700272static void migrate_page_copy(struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800273{
274 copy_highpage(newpage, page);
275
276 if (PageError(page))
277 SetPageError(newpage);
278 if (PageReferenced(page))
279 SetPageReferenced(newpage);
280 if (PageUptodate(page))
281 SetPageUptodate(newpage);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700282 if (TestClearPageActive(page)) {
283 VM_BUG_ON(PageUnevictable(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800284 SetPageActive(newpage);
Lee Schermerhorn418b27e2009-12-14 17:59:54 -0800285 } else if (TestClearPageUnevictable(page))
286 SetPageUnevictable(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800287 if (PageChecked(page))
288 SetPageChecked(newpage);
289 if (PageMappedToDisk(page))
290 SetPageMappedToDisk(newpage);
291
292 if (PageDirty(page)) {
293 clear_page_dirty_for_io(page);
Nick Piggin3a902c52008-04-30 00:55:16 -0700294 /*
295 * Want to mark the page and the radix tree as dirty, and
296 * redo the accounting that clear_page_dirty_for_io undid,
297 * but we can't use set_page_dirty because that function
298 * is actually a signal that all of the page has become dirty.
299 * Wheras only part of our page may be dirty.
300 */
301 __set_page_dirty_nobuffers(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800302 }
303
Nick Pigginb291f002008-10-18 20:26:44 -0700304 mlock_migrate_page(newpage, page);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800305 ksm_migrate_page(newpage, page);
Nick Pigginb291f002008-10-18 20:26:44 -0700306
Christoph Lameterb20a3502006-03-22 00:09:12 -0800307 ClearPageSwapCache(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800308 ClearPagePrivate(page);
309 set_page_private(page, 0);
310 page->mapping = NULL;
311
312 /*
313 * If any waiters have accumulated on the new page then
314 * wake them up.
315 */
316 if (PageWriteback(newpage))
317 end_page_writeback(newpage);
318}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800319
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700320/************************************************************
321 * Migration functions
322 ***********************************************************/
323
324/* Always fail migration. Used for mappings that are not movable */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700325int fail_migrate_page(struct address_space *mapping,
326 struct page *newpage, struct page *page)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700327{
328 return -EIO;
329}
330EXPORT_SYMBOL(fail_migrate_page);
331
Christoph Lameterb20a3502006-03-22 00:09:12 -0800332/*
333 * Common logic to directly migrate a single page suitable for
David Howells266cf652009-04-03 16:42:36 +0100334 * pages that do not use PagePrivate/PagePrivate2.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800335 *
336 * Pages are locked upon entry and exit.
337 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700338int migrate_page(struct address_space *mapping,
339 struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800340{
341 int rc;
342
343 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
344
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700345 rc = migrate_page_move_mapping(mapping, newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800346
347 if (rc)
348 return rc;
349
350 migrate_page_copy(newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800351 return 0;
352}
353EXPORT_SYMBOL(migrate_page);
354
David Howells93614012006-09-30 20:45:40 +0200355#ifdef CONFIG_BLOCK
Christoph Lameterb20a3502006-03-22 00:09:12 -0800356/*
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700357 * Migration function for pages with buffers. This function can only be used
358 * if the underlying filesystem guarantees that no other references to "page"
359 * exist.
360 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700361int buffer_migrate_page(struct address_space *mapping,
362 struct page *newpage, struct page *page)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700363{
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700364 struct buffer_head *bh, *head;
365 int rc;
366
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700367 if (!page_has_buffers(page))
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700368 return migrate_page(mapping, newpage, page);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700369
370 head = page_buffers(page);
371
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700372 rc = migrate_page_move_mapping(mapping, newpage, page);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700373
374 if (rc)
375 return rc;
376
377 bh = head;
378 do {
379 get_bh(bh);
380 lock_buffer(bh);
381 bh = bh->b_this_page;
382
383 } while (bh != head);
384
385 ClearPagePrivate(page);
386 set_page_private(newpage, page_private(page));
387 set_page_private(page, 0);
388 put_page(page);
389 get_page(newpage);
390
391 bh = head;
392 do {
393 set_bh_page(bh, newpage, bh_offset(bh));
394 bh = bh->b_this_page;
395
396 } while (bh != head);
397
398 SetPagePrivate(newpage);
399
400 migrate_page_copy(newpage, page);
401
402 bh = head;
403 do {
404 unlock_buffer(bh);
405 put_bh(bh);
406 bh = bh->b_this_page;
407
408 } while (bh != head);
409
410 return 0;
411}
412EXPORT_SYMBOL(buffer_migrate_page);
David Howells93614012006-09-30 20:45:40 +0200413#endif
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700414
Christoph Lameter04e62a22006-06-23 02:03:38 -0700415/*
416 * Writeback a page to clean the dirty state
417 */
418static int writeout(struct address_space *mapping, struct page *page)
419{
420 struct writeback_control wbc = {
421 .sync_mode = WB_SYNC_NONE,
422 .nr_to_write = 1,
423 .range_start = 0,
424 .range_end = LLONG_MAX,
425 .nonblocking = 1,
426 .for_reclaim = 1
427 };
428 int rc;
429
430 if (!mapping->a_ops->writepage)
431 /* No write method for the address space */
432 return -EINVAL;
433
434 if (!clear_page_dirty_for_io(page))
435 /* Someone else already triggered a write */
436 return -EAGAIN;
437
438 /*
439 * A dirty page may imply that the underlying filesystem has
440 * the page on some queue. So the page must be clean for
441 * migration. Writeout may mean we loose the lock and the
442 * page state is no longer what we checked for earlier.
443 * At this point we know that the migration attempt cannot
444 * be successful.
445 */
446 remove_migration_ptes(page, page);
447
448 rc = mapping->a_ops->writepage(page, &wbc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700449
450 if (rc != AOP_WRITEPAGE_ACTIVATE)
451 /* unlocked. Relock */
452 lock_page(page);
453
Hugh Dickinsbda85502008-11-19 15:36:36 -0800454 return (rc < 0) ? -EIO : -EAGAIN;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700455}
456
457/*
458 * Default handling if a filesystem does not provide a migration function.
459 */
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700460static int fallback_migrate_page(struct address_space *mapping,
461 struct page *newpage, struct page *page)
462{
Christoph Lameter04e62a22006-06-23 02:03:38 -0700463 if (PageDirty(page))
464 return writeout(mapping, page);
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700465
466 /*
467 * Buffers may be managed in a filesystem specific way.
468 * We must have no buffers or drop them.
469 */
David Howells266cf652009-04-03 16:42:36 +0100470 if (page_has_private(page) &&
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700471 !try_to_release_page(page, GFP_KERNEL))
472 return -EAGAIN;
473
474 return migrate_page(mapping, newpage, page);
475}
476
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700477/*
Christoph Lametere24f0b82006-06-23 02:03:51 -0700478 * Move a page to a newly allocated page
479 * The page is locked and all ptes have been successfully removed.
480 *
481 * The new page will have replaced the old page if this function
482 * is successful.
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700483 *
484 * Return value:
485 * < 0 - error code
486 * == 0 - success
Christoph Lametere24f0b82006-06-23 02:03:51 -0700487 */
488static int move_to_new_page(struct page *newpage, struct page *page)
489{
490 struct address_space *mapping;
491 int rc;
492
493 /*
494 * Block others from accessing the page when we get around to
495 * establishing additional references. We are the only one
496 * holding a reference to the new page at this point.
497 */
Nick Piggin529ae9a2008-08-02 12:01:03 +0200498 if (!trylock_page(newpage))
Christoph Lametere24f0b82006-06-23 02:03:51 -0700499 BUG();
500
501 /* Prepare mapping for the new page.*/
502 newpage->index = page->index;
503 newpage->mapping = page->mapping;
Rik van Rielb2e18532008-10-18 20:26:30 -0700504 if (PageSwapBacked(page))
505 SetPageSwapBacked(newpage);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700506
507 mapping = page_mapping(page);
508 if (!mapping)
509 rc = migrate_page(mapping, newpage, page);
510 else if (mapping->a_ops->migratepage)
511 /*
512 * Most pages have a mapping and most filesystems
513 * should provide a migration function. Anonymous
514 * pages are part of swap space which also has its
515 * own migration function. This is the most common
516 * path for page migration.
517 */
518 rc = mapping->a_ops->migratepage(mapping,
519 newpage, page);
520 else
521 rc = fallback_migrate_page(mapping, newpage, page);
522
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800523 if (!rc)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700524 remove_migration_ptes(page, newpage);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800525 else
Christoph Lametere24f0b82006-06-23 02:03:51 -0700526 newpage->mapping = NULL;
527
528 unlock_page(newpage);
529
530 return rc;
531}
532
533/*
534 * Obtain the lock on page, remove all ptes and migrate the page
535 * to the newly allocated page in newpage.
536 */
Christoph Lameter95a402c2006-06-23 02:03:53 -0700537static int unmap_and_move(new_page_t get_new_page, unsigned long private,
Hugh Dickins62b61f62009-12-14 17:59:33 -0800538 struct page *page, int force, int offlining)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700539{
540 int rc = 0;
Christoph Lameter742755a2006-06-23 02:03:55 -0700541 int *result = NULL;
542 struct page *newpage = get_new_page(page, private, &result);
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700543 int rcu_locked = 0;
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800544 int charge = 0;
KAMEZAWA Hiroyukie00e4312009-11-11 14:26:26 -0800545 struct mem_cgroup *mem = NULL;
Mel Gorman3f6c8272010-05-24 14:32:17 -0700546 struct anon_vma *anon_vma = NULL;
Christoph Lameter95a402c2006-06-23 02:03:53 -0700547
548 if (!newpage)
549 return -ENOMEM;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700550
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700551 if (page_count(page) == 1) {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700552 /* page was freed from under us. So we are done. */
Christoph Lameter95a402c2006-06-23 02:03:53 -0700553 goto move_newpage;
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700554 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700555
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700556 /* prepare cgroup just returns 0 or -ENOMEM */
Christoph Lametere24f0b82006-06-23 02:03:51 -0700557 rc = -EAGAIN;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800558
Nick Piggin529ae9a2008-08-02 12:01:03 +0200559 if (!trylock_page(page)) {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700560 if (!force)
Christoph Lameter95a402c2006-06-23 02:03:53 -0700561 goto move_newpage;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700562 lock_page(page);
563 }
564
Hugh Dickins62b61f62009-12-14 17:59:33 -0800565 /*
566 * Only memory hotplug's offline_pages() caller has locked out KSM,
567 * and can safely migrate a KSM page. The other cases have skipped
568 * PageKsm along with PageReserved - but it is only now when we have
569 * the page lock that we can be certain it will not go KSM beneath us
570 * (KSM will not upgrade a page from PageAnon to PageKsm when it sees
571 * its pagecount raised, but only here do we take the page lock which
572 * serializes that).
573 */
574 if (PageKsm(page) && !offlining) {
575 rc = -EBUSY;
576 goto unlock;
577 }
578
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800579 /* charge against new page */
580 charge = mem_cgroup_prepare_migration(page, &mem);
581 if (charge == -ENOMEM) {
582 rc = -ENOMEM;
583 goto unlock;
584 }
585 BUG_ON(charge);
586
Christoph Lametere24f0b82006-06-23 02:03:51 -0700587 if (PageWriteback(page)) {
588 if (!force)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800589 goto uncharge;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700590 wait_on_page_writeback(page);
591 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700592 /*
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700593 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
594 * we cannot notice that anon_vma is freed while we migrates a page.
595 * This rcu_read_lock() delays freeing anon_vma pointer until the end
596 * of migration. File cache pages are no problem because of page_lock()
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700597 * File Caches may use write_page() or lock_page() in migration, then,
598 * just care Anon page here.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700599 */
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700600 if (PageAnon(page)) {
601 rcu_read_lock();
602 rcu_locked = 1;
Mel Gorman67b95092010-05-24 14:32:19 -0700603
604 /*
605 * If the page has no mappings any more, just bail. An
606 * unmapped anon page is likely to be freed soon but worse,
607 * it's possible its anon_vma disappeared between when
608 * the page was isolated and when we reached here while
609 * the RCU lock was not held
610 */
611 if (!page_mapped(page))
612 goto rcu_unlock;
613
Mel Gorman3f6c8272010-05-24 14:32:17 -0700614 anon_vma = page_anon_vma(page);
Mel Gorman7f60c212010-05-24 14:32:18 -0700615 atomic_inc(&anon_vma->external_refcount);
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700616 }
Shaohua Li62e1c552008-02-04 22:29:33 -0800617
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700618 /*
Shaohua Li62e1c552008-02-04 22:29:33 -0800619 * Corner case handling:
620 * 1. When a new swap-cache page is read into, it is added to the LRU
621 * and treated as swapcache but it has no rmap yet.
622 * Calling try_to_unmap() against a page->mapping==NULL page will
623 * trigger a BUG. So handle it here.
624 * 2. An orphaned page (see truncate_complete_page) might have
625 * fs-private metadata. The page can be picked up due to memory
626 * offlining. Everywhere else except page reclaim, the page is
627 * invisible to the vm, so the page can not be migrated. So try to
628 * free the metadata, so the page can be freed.
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700629 */
Shaohua Li62e1c552008-02-04 22:29:33 -0800630 if (!page->mapping) {
David Howells266cf652009-04-03 16:42:36 +0100631 if (!PageAnon(page) && page_has_private(page)) {
Shaohua Li62e1c552008-02-04 22:29:33 -0800632 /*
633 * Go direct to try_to_free_buffers() here because
634 * a) that's what try_to_release_page() would do anyway
635 * b) we may be under rcu_read_lock() here, so we can't
636 * use GFP_KERNEL which is what try_to_release_page()
637 * needs to be effective.
638 */
639 try_to_free_buffers(page);
Shaohua Liabfc3482009-09-21 17:01:19 -0700640 goto rcu_unlock;
Shaohua Li62e1c552008-02-04 22:29:33 -0800641 }
Shaohua Liabfc3482009-09-21 17:01:19 -0700642 goto skip_unmap;
Shaohua Li62e1c552008-02-04 22:29:33 -0800643 }
644
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700645 /* Establish migration ptes or remove ptes */
Andi Kleen14fa31b2009-09-16 11:50:10 +0200646 try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700647
Shaohua Liabfc3482009-09-21 17:01:19 -0700648skip_unmap:
Christoph Lametere6a15302006-06-25 05:46:49 -0700649 if (!page_mapped(page))
650 rc = move_to_new_page(newpage, page);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700651
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700652 if (rc)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700653 remove_migration_ptes(page, page);
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700654rcu_unlock:
Mel Gorman3f6c8272010-05-24 14:32:17 -0700655
656 /* Drop an anon_vma reference if we took one */
Mel Gorman7f60c212010-05-24 14:32:18 -0700657 if (anon_vma && atomic_dec_and_lock(&anon_vma->external_refcount, &anon_vma->lock)) {
Mel Gorman3f6c8272010-05-24 14:32:17 -0700658 int empty = list_empty(&anon_vma->head);
659 spin_unlock(&anon_vma->lock);
660 if (empty)
661 anon_vma_free(anon_vma);
662 }
663
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700664 if (rcu_locked)
665 rcu_read_unlock();
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800666uncharge:
667 if (!charge)
668 mem_cgroup_end_migration(mem, page, newpage);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700669unlock:
670 unlock_page(page);
Christoph Lameter95a402c2006-06-23 02:03:53 -0700671
Christoph Lametere24f0b82006-06-23 02:03:51 -0700672 if (rc != -EAGAIN) {
Christoph Lameteraaa994b2006-06-23 02:03:52 -0700673 /*
674 * A page that has been migrated has all references
675 * removed and will be freed. A page that has not been
676 * migrated will have kepts its references and be
677 * restored.
678 */
679 list_del(&page->lru);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -0700680 dec_zone_page_state(page, NR_ISOLATED_ANON +
Johannes Weiner6c0b1352009-09-21 17:02:59 -0700681 page_is_file_cache(page));
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700682 putback_lru_page(page);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700683 }
Christoph Lameter95a402c2006-06-23 02:03:53 -0700684
685move_newpage:
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700686
Christoph Lameter95a402c2006-06-23 02:03:53 -0700687 /*
688 * Move the new page to the LRU. If migration was not successful
689 * then this will free the page.
690 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700691 putback_lru_page(newpage);
692
Christoph Lameter742755a2006-06-23 02:03:55 -0700693 if (result) {
694 if (rc)
695 *result = rc;
696 else
697 *result = page_to_nid(newpage);
698 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700699 return rc;
700}
701
702/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800703 * migrate_pages
704 *
Christoph Lameter95a402c2006-06-23 02:03:53 -0700705 * The function takes one list of pages to migrate and a function
706 * that determines from the page to be migrated and the private data
707 * the target of the move and allocates the page.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800708 *
709 * The function returns after 10 attempts or if no pages
710 * are movable anymore because to has become empty
Christoph Lameteraaa994b2006-06-23 02:03:52 -0700711 * or no retryable pages exist anymore. All pages will be
Gabriel Craciunescue9534b32007-10-20 02:13:26 +0200712 * returned to the LRU or freed.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800713 *
Christoph Lameter95a402c2006-06-23 02:03:53 -0700714 * Return: Number of pages not migrated or error code.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800715 */
Christoph Lameter95a402c2006-06-23 02:03:53 -0700716int migrate_pages(struct list_head *from,
Hugh Dickins62b61f62009-12-14 17:59:33 -0800717 new_page_t get_new_page, unsigned long private, int offlining)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800718{
Christoph Lametere24f0b82006-06-23 02:03:51 -0700719 int retry = 1;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800720 int nr_failed = 0;
721 int pass = 0;
722 struct page *page;
723 struct page *page2;
724 int swapwrite = current->flags & PF_SWAPWRITE;
725 int rc;
726
727 if (!swapwrite)
728 current->flags |= PF_SWAPWRITE;
729
Christoph Lametere24f0b82006-06-23 02:03:51 -0700730 for(pass = 0; pass < 10 && retry; pass++) {
731 retry = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800732
Christoph Lametere24f0b82006-06-23 02:03:51 -0700733 list_for_each_entry_safe(page, page2, from, lru) {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700734 cond_resched();
Christoph Lameterb20a3502006-03-22 00:09:12 -0800735
Christoph Lameter95a402c2006-06-23 02:03:53 -0700736 rc = unmap_and_move(get_new_page, private,
Hugh Dickins62b61f62009-12-14 17:59:33 -0800737 page, pass > 2, offlining);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800738
Christoph Lametere24f0b82006-06-23 02:03:51 -0700739 switch(rc) {
Christoph Lameter95a402c2006-06-23 02:03:53 -0700740 case -ENOMEM:
741 goto out;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700742 case -EAGAIN:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700743 retry++;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700744 break;
745 case 0:
Christoph Lametere24f0b82006-06-23 02:03:51 -0700746 break;
747 default:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700748 /* Permanent failure */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700749 nr_failed++;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700750 break;
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700751 }
Christoph Lameterb20a3502006-03-22 00:09:12 -0800752 }
753 }
Christoph Lameter95a402c2006-06-23 02:03:53 -0700754 rc = 0;
755out:
Christoph Lameterb20a3502006-03-22 00:09:12 -0800756 if (!swapwrite)
757 current->flags &= ~PF_SWAPWRITE;
758
Christoph Lameteraaa994b2006-06-23 02:03:52 -0700759 putback_lru_pages(from);
Christoph Lameter95a402c2006-06-23 02:03:53 -0700760
761 if (rc)
762 return rc;
763
Christoph Lameterb20a3502006-03-22 00:09:12 -0800764 return nr_failed + retry;
765}
766
Christoph Lameter742755a2006-06-23 02:03:55 -0700767#ifdef CONFIG_NUMA
768/*
769 * Move a list of individual pages
770 */
771struct page_to_node {
772 unsigned long addr;
773 struct page *page;
774 int node;
775 int status;
776};
777
778static struct page *new_page_node(struct page *p, unsigned long private,
779 int **result)
780{
781 struct page_to_node *pm = (struct page_to_node *)private;
782
783 while (pm->node != MAX_NUMNODES && pm->page != p)
784 pm++;
785
786 if (pm->node == MAX_NUMNODES)
787 return NULL;
788
789 *result = &pm->status;
790
Mel Gorman6484eb32009-06-16 15:31:54 -0700791 return alloc_pages_exact_node(pm->node,
Mel Gorman769848c2007-07-17 04:03:05 -0700792 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
Christoph Lameter742755a2006-06-23 02:03:55 -0700793}
794
795/*
796 * Move a set of pages as indicated in the pm array. The addr
797 * field must be set to the virtual address of the page to be moved
798 * and the node number must contain a valid target node.
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700799 * The pm array ends with node = MAX_NUMNODES.
Christoph Lameter742755a2006-06-23 02:03:55 -0700800 */
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700801static int do_move_page_to_node_array(struct mm_struct *mm,
802 struct page_to_node *pm,
803 int migrate_all)
Christoph Lameter742755a2006-06-23 02:03:55 -0700804{
805 int err;
806 struct page_to_node *pp;
807 LIST_HEAD(pagelist);
808
809 down_read(&mm->mmap_sem);
810
811 /*
812 * Build a list of pages to migrate
813 */
Christoph Lameter742755a2006-06-23 02:03:55 -0700814 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
815 struct vm_area_struct *vma;
816 struct page *page;
817
Christoph Lameter742755a2006-06-23 02:03:55 -0700818 err = -EFAULT;
819 vma = find_vma(mm, pp->addr);
Christoph Lameter0dc952d2007-03-05 00:30:33 -0800820 if (!vma || !vma_migratable(vma))
Christoph Lameter742755a2006-06-23 02:03:55 -0700821 goto set_status;
822
823 page = follow_page(vma, pp->addr, FOLL_GET);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -0700824
825 err = PTR_ERR(page);
826 if (IS_ERR(page))
827 goto set_status;
828
Christoph Lameter742755a2006-06-23 02:03:55 -0700829 err = -ENOENT;
830 if (!page)
831 goto set_status;
832
Hugh Dickins62b61f62009-12-14 17:59:33 -0800833 /* Use PageReserved to check for zero page */
834 if (PageReserved(page) || PageKsm(page))
Christoph Lameter742755a2006-06-23 02:03:55 -0700835 goto put_and_set;
836
837 pp->page = page;
838 err = page_to_nid(page);
839
840 if (err == pp->node)
841 /*
842 * Node already in the right place
843 */
844 goto put_and_set;
845
846 err = -EACCES;
847 if (page_mapcount(page) > 1 &&
848 !migrate_all)
849 goto put_and_set;
850
Nick Piggin62695a82008-10-18 20:26:09 -0700851 err = isolate_lru_page(page);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -0800852 if (!err) {
Nick Piggin62695a82008-10-18 20:26:09 -0700853 list_add_tail(&page->lru, &pagelist);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -0800854 inc_zone_page_state(page, NR_ISOLATED_ANON +
855 page_is_file_cache(page));
856 }
Christoph Lameter742755a2006-06-23 02:03:55 -0700857put_and_set:
858 /*
859 * Either remove the duplicate refcount from
860 * isolate_lru_page() or drop the page ref if it was
861 * not isolated.
862 */
863 put_page(page);
864set_status:
865 pp->status = err;
866 }
867
Brice Gogline78bbfa2008-10-18 20:27:15 -0700868 err = 0;
Christoph Lameter742755a2006-06-23 02:03:55 -0700869 if (!list_empty(&pagelist))
870 err = migrate_pages(&pagelist, new_page_node,
Hugh Dickins62b61f62009-12-14 17:59:33 -0800871 (unsigned long)pm, 0);
Christoph Lameter742755a2006-06-23 02:03:55 -0700872
873 up_read(&mm->mmap_sem);
874 return err;
875}
876
877/*
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700878 * Migrate an array of page address onto an array of nodes and fill
879 * the corresponding array of status.
880 */
881static int do_pages_move(struct mm_struct *mm, struct task_struct *task,
882 unsigned long nr_pages,
883 const void __user * __user *pages,
884 const int __user *nodes,
885 int __user *status, int flags)
886{
Brice Goglin3140a222009-01-06 14:38:57 -0800887 struct page_to_node *pm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700888 nodemask_t task_nodes;
Brice Goglin3140a222009-01-06 14:38:57 -0800889 unsigned long chunk_nr_pages;
890 unsigned long chunk_start;
891 int err;
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700892
893 task_nodes = cpuset_mems_allowed(task);
894
Brice Goglin3140a222009-01-06 14:38:57 -0800895 err = -ENOMEM;
896 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
897 if (!pm)
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700898 goto out;
Brice Goglin35282a22009-06-16 15:32:43 -0700899
900 migrate_prep();
901
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700902 /*
Brice Goglin3140a222009-01-06 14:38:57 -0800903 * Store a chunk of page_to_node array in a page,
904 * but keep the last one as a marker
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700905 */
Brice Goglin3140a222009-01-06 14:38:57 -0800906 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700907
Brice Goglin3140a222009-01-06 14:38:57 -0800908 for (chunk_start = 0;
909 chunk_start < nr_pages;
910 chunk_start += chunk_nr_pages) {
911 int j;
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700912
Brice Goglin3140a222009-01-06 14:38:57 -0800913 if (chunk_start + chunk_nr_pages > nr_pages)
914 chunk_nr_pages = nr_pages - chunk_start;
915
916 /* fill the chunk pm with addrs and nodes from user-space */
917 for (j = 0; j < chunk_nr_pages; j++) {
918 const void __user *p;
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700919 int node;
920
Brice Goglin3140a222009-01-06 14:38:57 -0800921 err = -EFAULT;
922 if (get_user(p, pages + j + chunk_start))
923 goto out_pm;
924 pm[j].addr = (unsigned long) p;
925
926 if (get_user(node, nodes + j + chunk_start))
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700927 goto out_pm;
928
929 err = -ENODEV;
Linus Torvalds6f5a55f2010-02-05 16:16:50 -0800930 if (node < 0 || node >= MAX_NUMNODES)
931 goto out_pm;
932
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700933 if (!node_state(node, N_HIGH_MEMORY))
934 goto out_pm;
935
936 err = -EACCES;
937 if (!node_isset(node, task_nodes))
938 goto out_pm;
939
Brice Goglin3140a222009-01-06 14:38:57 -0800940 pm[j].node = node;
941 }
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700942
Brice Goglin3140a222009-01-06 14:38:57 -0800943 /* End marker for this chunk */
944 pm[chunk_nr_pages].node = MAX_NUMNODES;
945
946 /* Migrate this chunk */
947 err = do_move_page_to_node_array(mm, pm,
948 flags & MPOL_MF_MOVE_ALL);
949 if (err < 0)
950 goto out_pm;
951
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700952 /* Return status information */
Brice Goglin3140a222009-01-06 14:38:57 -0800953 for (j = 0; j < chunk_nr_pages; j++)
954 if (put_user(pm[j].status, status + j + chunk_start)) {
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700955 err = -EFAULT;
Brice Goglin3140a222009-01-06 14:38:57 -0800956 goto out_pm;
957 }
958 }
959 err = 0;
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700960
961out_pm:
Brice Goglin3140a222009-01-06 14:38:57 -0800962 free_page((unsigned long)pm);
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700963out:
964 return err;
965}
966
967/*
Brice Goglin2f007e72008-10-18 20:27:16 -0700968 * Determine the nodes of an array of pages and store it in an array of status.
Christoph Lameter742755a2006-06-23 02:03:55 -0700969 */
Brice Goglin80bba122008-12-09 13:14:23 -0800970static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
971 const void __user **pages, int *status)
Christoph Lameter742755a2006-06-23 02:03:55 -0700972{
Brice Goglin2f007e72008-10-18 20:27:16 -0700973 unsigned long i;
Brice Goglin2f007e72008-10-18 20:27:16 -0700974
Christoph Lameter742755a2006-06-23 02:03:55 -0700975 down_read(&mm->mmap_sem);
976
Brice Goglin2f007e72008-10-18 20:27:16 -0700977 for (i = 0; i < nr_pages; i++) {
Brice Goglin80bba122008-12-09 13:14:23 -0800978 unsigned long addr = (unsigned long)(*pages);
Christoph Lameter742755a2006-06-23 02:03:55 -0700979 struct vm_area_struct *vma;
980 struct page *page;
KOSAKI Motohiroc095adb2008-12-16 16:06:43 +0900981 int err = -EFAULT;
Brice Goglin2f007e72008-10-18 20:27:16 -0700982
983 vma = find_vma(mm, addr);
Christoph Lameter742755a2006-06-23 02:03:55 -0700984 if (!vma)
985 goto set_status;
986
Brice Goglin2f007e72008-10-18 20:27:16 -0700987 page = follow_page(vma, addr, 0);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -0700988
989 err = PTR_ERR(page);
990 if (IS_ERR(page))
991 goto set_status;
992
Christoph Lameter742755a2006-06-23 02:03:55 -0700993 err = -ENOENT;
994 /* Use PageReserved to check for zero page */
Hugh Dickins62b61f62009-12-14 17:59:33 -0800995 if (!page || PageReserved(page) || PageKsm(page))
Christoph Lameter742755a2006-06-23 02:03:55 -0700996 goto set_status;
997
998 err = page_to_nid(page);
999set_status:
Brice Goglin80bba122008-12-09 13:14:23 -08001000 *status = err;
1001
1002 pages++;
1003 status++;
1004 }
1005
1006 up_read(&mm->mmap_sem);
1007}
1008
1009/*
1010 * Determine the nodes of a user array of pages and store it in
1011 * a user array of status.
1012 */
1013static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1014 const void __user * __user *pages,
1015 int __user *status)
1016{
1017#define DO_PAGES_STAT_CHUNK_NR 16
1018 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1019 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
Brice Goglin80bba122008-12-09 13:14:23 -08001020
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001021 while (nr_pages) {
1022 unsigned long chunk_nr;
Brice Goglin80bba122008-12-09 13:14:23 -08001023
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001024 chunk_nr = nr_pages;
1025 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1026 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1027
1028 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
1029 break;
Brice Goglin80bba122008-12-09 13:14:23 -08001030
1031 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1032
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001033 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1034 break;
Christoph Lameter742755a2006-06-23 02:03:55 -07001035
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001036 pages += chunk_nr;
1037 status += chunk_nr;
1038 nr_pages -= chunk_nr;
1039 }
1040 return nr_pages ? -EFAULT : 0;
Christoph Lameter742755a2006-06-23 02:03:55 -07001041}
1042
1043/*
1044 * Move a list of pages in the address space of the currently executing
1045 * process.
1046 */
Heiko Carstens938bb9f2009-01-14 14:14:30 +01001047SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1048 const void __user * __user *, pages,
1049 const int __user *, nodes,
1050 int __user *, status, int, flags)
Christoph Lameter742755a2006-06-23 02:03:55 -07001051{
David Howellsc69e8d92008-11-14 10:39:19 +11001052 const struct cred *cred = current_cred(), *tcred;
Christoph Lameter742755a2006-06-23 02:03:55 -07001053 struct task_struct *task;
Christoph Lameter742755a2006-06-23 02:03:55 -07001054 struct mm_struct *mm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001055 int err;
Christoph Lameter742755a2006-06-23 02:03:55 -07001056
1057 /* Check flags */
1058 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1059 return -EINVAL;
1060
1061 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1062 return -EPERM;
1063
1064 /* Find the mm_struct */
1065 read_lock(&tasklist_lock);
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07001066 task = pid ? find_task_by_vpid(pid) : current;
Christoph Lameter742755a2006-06-23 02:03:55 -07001067 if (!task) {
1068 read_unlock(&tasklist_lock);
1069 return -ESRCH;
1070 }
1071 mm = get_task_mm(task);
1072 read_unlock(&tasklist_lock);
1073
1074 if (!mm)
1075 return -EINVAL;
1076
1077 /*
1078 * Check if this process has the right to modify the specified
1079 * process. The right exists if the process has administrative
1080 * capabilities, superuser privileges or the same
1081 * userid as the target process.
1082 */
David Howellsc69e8d92008-11-14 10:39:19 +11001083 rcu_read_lock();
1084 tcred = __task_cred(task);
David Howellsb6dff3e2008-11-14 10:39:16 +11001085 if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
1086 cred->uid != tcred->suid && cred->uid != tcred->uid &&
Christoph Lameter742755a2006-06-23 02:03:55 -07001087 !capable(CAP_SYS_NICE)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001088 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001089 err = -EPERM;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001090 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001091 }
David Howellsc69e8d92008-11-14 10:39:19 +11001092 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001093
David Quigley86c3a762006-06-23 02:04:02 -07001094 err = security_task_movememory(task);
1095 if (err)
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001096 goto out;
David Quigley86c3a762006-06-23 02:04:02 -07001097
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001098 if (nodes) {
1099 err = do_pages_move(mm, task, nr_pages, pages, nodes, status,
1100 flags);
1101 } else {
Brice Goglin2f007e72008-10-18 20:27:16 -07001102 err = do_pages_stat(mm, nr_pages, pages, status);
Brice Goglin2f007e72008-10-18 20:27:16 -07001103 }
David Quigley86c3a762006-06-23 02:04:02 -07001104
Christoph Lameter742755a2006-06-23 02:03:55 -07001105out:
Christoph Lameter742755a2006-06-23 02:03:55 -07001106 mmput(mm);
1107 return err;
1108}
Christoph Lameter742755a2006-06-23 02:03:55 -07001109
Christoph Lameter7b2259b2006-06-25 05:46:48 -07001110/*
1111 * Call migration functions in the vma_ops that may prepare
1112 * memory in a vm for migration. migration functions may perform
1113 * the migration for vmas that do not have an underlying page struct.
1114 */
1115int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1116 const nodemask_t *from, unsigned long flags)
1117{
1118 struct vm_area_struct *vma;
1119 int err = 0;
1120
Daisuke Nishimura1001c9f2009-02-11 13:04:18 -08001121 for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
Christoph Lameter7b2259b2006-06-25 05:46:48 -07001122 if (vma->vm_ops && vma->vm_ops->migrate) {
1123 err = vma->vm_ops->migrate(vma, to, from, flags);
1124 if (err)
1125 break;
1126 }
1127 }
1128 return err;
1129}
Gerald Schaefer83d16742008-07-23 21:28:22 -07001130#endif