blob: d3f3f7f81075eb1eb2e7f5dbe2c0f292a489dacf [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 *
62 * returns the number of pages put back.
63 */
64int putback_lru_pages(struct list_head *l)
65{
66 struct page *page;
67 struct page *page2;
68 int count = 0;
69
70 list_for_each_entry_safe(page, page2, l, lru) {
Christoph Lametere24f0b82006-06-23 02:03:51 -070071 list_del(&page->lru);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -070072 dec_zone_page_state(page, NR_ISOLATED_ANON +
Johannes Weiner6c0b1352009-09-21 17:02:59 -070073 page_is_file_cache(page));
Lee Schermerhorn894bc312008-10-18 20:26:39 -070074 putback_lru_page(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -080075 count++;
76 }
77 return count;
78}
79
Christoph Lameter06972122006-06-23 02:03:35 -070080/*
81 * Restore a potential migration pte to a working pte entry
82 */
Hugh Dickinse9995ef2009-12-14 17:59:31 -080083static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,
84 unsigned long addr, void *old)
Christoph Lameter06972122006-06-23 02:03:35 -070085{
86 struct mm_struct *mm = vma->vm_mm;
87 swp_entry_t entry;
88 pgd_t *pgd;
89 pud_t *pud;
90 pmd_t *pmd;
91 pte_t *ptep, pte;
92 spinlock_t *ptl;
93
94 pgd = pgd_offset(mm, addr);
95 if (!pgd_present(*pgd))
Hugh Dickinse9995ef2009-12-14 17:59:31 -080096 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -070097
98 pud = pud_offset(pgd, addr);
99 if (!pud_present(*pud))
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800100 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700101
102 pmd = pmd_offset(pud, addr);
103 if (!pmd_present(*pmd))
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800104 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700105
106 ptep = pte_offset_map(pmd, addr);
107
108 if (!is_swap_pte(*ptep)) {
109 pte_unmap(ptep);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800110 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700111 }
112
113 ptl = pte_lockptr(mm, pmd);
114 spin_lock(ptl);
115 pte = *ptep;
116 if (!is_swap_pte(pte))
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800117 goto unlock;
Christoph Lameter06972122006-06-23 02:03:35 -0700118
119 entry = pte_to_swp_entry(pte);
120
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800121 if (!is_migration_entry(entry) ||
122 migration_entry_to_page(entry) != old)
123 goto unlock;
Christoph Lameter06972122006-06-23 02:03:35 -0700124
Christoph Lameter06972122006-06-23 02:03:35 -0700125 get_page(new);
126 pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
127 if (is_write_migration_entry(entry))
128 pte = pte_mkwrite(pte);
KAMEZAWA Hiroyuki97ee0522007-10-16 01:25:43 -0700129 flush_cache_page(vma, addr, pte_pfn(pte));
Christoph Lameter06972122006-06-23 02:03:35 -0700130 set_pte_at(mm, addr, ptep, pte);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700131
132 if (PageAnon(new))
133 page_add_anon_rmap(new, vma, addr);
134 else
135 page_add_file_rmap(new);
136
137 /* No need to invalidate - it was non-present before */
Russell King4b3073e2009-12-18 16:40:18 +0000138 update_mmu_cache(vma, addr, ptep);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800139unlock:
Christoph Lameter06972122006-06-23 02:03:35 -0700140 pte_unmap_unlock(ptep, ptl);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800141out:
142 return SWAP_AGAIN;
Christoph Lameter06972122006-06-23 02:03:35 -0700143}
144
145/*
Christoph Lameter04e62a22006-06-23 02:03:38 -0700146 * Get rid of all migration entries and replace them by
147 * references to the indicated page.
148 */
149static void remove_migration_ptes(struct page *old, struct page *new)
150{
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800151 rmap_walk(new, remove_migration_pte, old);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700152}
153
154/*
Christoph Lameter06972122006-06-23 02:03:35 -0700155 * Something used the pte of a page under migration. We need to
156 * get to the page and wait until migration is finished.
157 * When we return from this function the fault will be retried.
158 *
159 * This function is called from do_swap_page().
160 */
161void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
162 unsigned long address)
163{
164 pte_t *ptep, pte;
165 spinlock_t *ptl;
166 swp_entry_t entry;
167 struct page *page;
168
169 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
170 pte = *ptep;
171 if (!is_swap_pte(pte))
172 goto out;
173
174 entry = pte_to_swp_entry(pte);
175 if (!is_migration_entry(entry))
176 goto out;
177
178 page = migration_entry_to_page(entry);
179
Nick Piggine2867812008-07-25 19:45:30 -0700180 /*
181 * Once radix-tree replacement of page migration started, page_count
182 * *must* be zero. And, we don't want to call wait_on_page_locked()
183 * against a page without get_page().
184 * So, we use get_page_unless_zero(), here. Even failed, page fault
185 * will occur again.
186 */
187 if (!get_page_unless_zero(page))
188 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700189 pte_unmap_unlock(ptep, ptl);
190 wait_on_page_locked(page);
191 put_page(page);
192 return;
193out:
194 pte_unmap_unlock(ptep, ptl);
195}
196
Christoph Lameterb20a3502006-03-22 00:09:12 -0800197/*
Christoph Lameterc3fcf8a2006-06-23 02:03:32 -0700198 * Replace the page in the mapping.
Christoph Lameter5b5c7122006-06-23 02:03:29 -0700199 *
200 * The number of remaining references must be:
201 * 1 for anonymous pages without a mapping
202 * 2 for pages with a mapping
David Howells266cf652009-04-03 16:42:36 +0100203 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800204 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700205static int migrate_page_move_mapping(struct address_space *mapping,
206 struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800207{
Nick Piggine2867812008-07-25 19:45:30 -0700208 int expected_count;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800209 void **pslot;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800210
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700211 if (!mapping) {
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700212 /* Anonymous page without mapping */
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700213 if (page_count(page) != 1)
214 return -EAGAIN;
215 return 0;
216 }
217
Nick Piggin19fd6232008-07-25 19:45:32 -0700218 spin_lock_irq(&mapping->tree_lock);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800219
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800220 pslot = radix_tree_lookup_slot(&mapping->page_tree,
221 page_index(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800222
Johannes Weineredcf4742009-09-21 17:02:59 -0700223 expected_count = 2 + page_has_private(page);
Nick Piggine2867812008-07-25 19:45:30 -0700224 if (page_count(page) != expected_count ||
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800225 (struct page *)radix_tree_deref_slot(pslot) != page) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700226 spin_unlock_irq(&mapping->tree_lock);
Christoph Lametere23ca002006-04-10 22:52:57 -0700227 return -EAGAIN;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800228 }
229
Nick Piggine2867812008-07-25 19:45:30 -0700230 if (!page_freeze_refs(page, expected_count)) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700231 spin_unlock_irq(&mapping->tree_lock);
Nick Piggine2867812008-07-25 19:45:30 -0700232 return -EAGAIN;
233 }
234
Christoph Lameterb20a3502006-03-22 00:09:12 -0800235 /*
236 * Now we know that no one else is looking at the page.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800237 */
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800238 get_page(newpage); /* add cache reference */
Christoph Lameterb20a3502006-03-22 00:09:12 -0800239 if (PageSwapCache(page)) {
240 SetPageSwapCache(newpage);
241 set_page_private(newpage, page_private(page));
242 }
243
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800244 radix_tree_replace_slot(pslot, newpage);
245
Nick Piggine2867812008-07-25 19:45:30 -0700246 page_unfreeze_refs(page, expected_count);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800247 /*
248 * Drop cache reference from old page.
249 * We know this isn't the last reference.
250 */
Christoph Lameterb20a3502006-03-22 00:09:12 -0800251 __put_page(page);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800252
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700253 /*
254 * If moved to a different zone then also account
255 * the page for that zone. Other VM counters will be
256 * taken care of when we establish references to the
257 * new page and drop references to the old page.
258 *
259 * Note that anonymous pages are accounted for
260 * via NR_FILE_PAGES and NR_ANON_PAGES if they
261 * are mapped to swap space.
262 */
263 __dec_zone_page_state(page, NR_FILE_PAGES);
264 __inc_zone_page_state(newpage, NR_FILE_PAGES);
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700265 if (PageSwapBacked(page)) {
266 __dec_zone_page_state(page, NR_SHMEM);
267 __inc_zone_page_state(newpage, NR_SHMEM);
268 }
Nick Piggin19fd6232008-07-25 19:45:32 -0700269 spin_unlock_irq(&mapping->tree_lock);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800270
271 return 0;
272}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800273
274/*
275 * Copy the page to its new location
276 */
Christoph Lametere7340f72006-06-23 02:03:29 -0700277static void migrate_page_copy(struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800278{
279 copy_highpage(newpage, page);
280
281 if (PageError(page))
282 SetPageError(newpage);
283 if (PageReferenced(page))
284 SetPageReferenced(newpage);
285 if (PageUptodate(page))
286 SetPageUptodate(newpage);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700287 if (TestClearPageActive(page)) {
288 VM_BUG_ON(PageUnevictable(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800289 SetPageActive(newpage);
Lee Schermerhorn418b27e2009-12-14 17:59:54 -0800290 } else if (TestClearPageUnevictable(page))
291 SetPageUnevictable(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800292 if (PageChecked(page))
293 SetPageChecked(newpage);
294 if (PageMappedToDisk(page))
295 SetPageMappedToDisk(newpage);
296
297 if (PageDirty(page)) {
298 clear_page_dirty_for_io(page);
Nick Piggin3a902c52008-04-30 00:55:16 -0700299 /*
300 * Want to mark the page and the radix tree as dirty, and
301 * redo the accounting that clear_page_dirty_for_io undid,
302 * but we can't use set_page_dirty because that function
303 * is actually a signal that all of the page has become dirty.
304 * Wheras only part of our page may be dirty.
305 */
306 __set_page_dirty_nobuffers(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800307 }
308
Nick Pigginb291f002008-10-18 20:26:44 -0700309 mlock_migrate_page(newpage, page);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800310 ksm_migrate_page(newpage, page);
Nick Pigginb291f002008-10-18 20:26:44 -0700311
Christoph Lameterb20a3502006-03-22 00:09:12 -0800312 ClearPageSwapCache(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800313 ClearPagePrivate(page);
314 set_page_private(page, 0);
315 page->mapping = NULL;
316
317 /*
318 * If any waiters have accumulated on the new page then
319 * wake them up.
320 */
321 if (PageWriteback(newpage))
322 end_page_writeback(newpage);
323}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800324
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700325/************************************************************
326 * Migration functions
327 ***********************************************************/
328
329/* Always fail migration. Used for mappings that are not movable */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700330int fail_migrate_page(struct address_space *mapping,
331 struct page *newpage, struct page *page)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700332{
333 return -EIO;
334}
335EXPORT_SYMBOL(fail_migrate_page);
336
Christoph Lameterb20a3502006-03-22 00:09:12 -0800337/*
338 * Common logic to directly migrate a single page suitable for
David Howells266cf652009-04-03 16:42:36 +0100339 * pages that do not use PagePrivate/PagePrivate2.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800340 *
341 * Pages are locked upon entry and exit.
342 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700343int migrate_page(struct address_space *mapping,
344 struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800345{
346 int rc;
347
348 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
349
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700350 rc = migrate_page_move_mapping(mapping, newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800351
352 if (rc)
353 return rc;
354
355 migrate_page_copy(newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800356 return 0;
357}
358EXPORT_SYMBOL(migrate_page);
359
David Howells93614012006-09-30 20:45:40 +0200360#ifdef CONFIG_BLOCK
Christoph Lameterb20a3502006-03-22 00:09:12 -0800361/*
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700362 * Migration function for pages with buffers. This function can only be used
363 * if the underlying filesystem guarantees that no other references to "page"
364 * exist.
365 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700366int buffer_migrate_page(struct address_space *mapping,
367 struct page *newpage, struct page *page)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700368{
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700369 struct buffer_head *bh, *head;
370 int rc;
371
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700372 if (!page_has_buffers(page))
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700373 return migrate_page(mapping, newpage, page);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700374
375 head = page_buffers(page);
376
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700377 rc = migrate_page_move_mapping(mapping, newpage, page);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700378
379 if (rc)
380 return rc;
381
382 bh = head;
383 do {
384 get_bh(bh);
385 lock_buffer(bh);
386 bh = bh->b_this_page;
387
388 } while (bh != head);
389
390 ClearPagePrivate(page);
391 set_page_private(newpage, page_private(page));
392 set_page_private(page, 0);
393 put_page(page);
394 get_page(newpage);
395
396 bh = head;
397 do {
398 set_bh_page(bh, newpage, bh_offset(bh));
399 bh = bh->b_this_page;
400
401 } while (bh != head);
402
403 SetPagePrivate(newpage);
404
405 migrate_page_copy(newpage, page);
406
407 bh = head;
408 do {
409 unlock_buffer(bh);
410 put_bh(bh);
411 bh = bh->b_this_page;
412
413 } while (bh != head);
414
415 return 0;
416}
417EXPORT_SYMBOL(buffer_migrate_page);
David Howells93614012006-09-30 20:45:40 +0200418#endif
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700419
Christoph Lameter04e62a22006-06-23 02:03:38 -0700420/*
421 * Writeback a page to clean the dirty state
422 */
423static int writeout(struct address_space *mapping, struct page *page)
424{
425 struct writeback_control wbc = {
426 .sync_mode = WB_SYNC_NONE,
427 .nr_to_write = 1,
428 .range_start = 0,
429 .range_end = LLONG_MAX,
430 .nonblocking = 1,
431 .for_reclaim = 1
432 };
433 int rc;
434
435 if (!mapping->a_ops->writepage)
436 /* No write method for the address space */
437 return -EINVAL;
438
439 if (!clear_page_dirty_for_io(page))
440 /* Someone else already triggered a write */
441 return -EAGAIN;
442
443 /*
444 * A dirty page may imply that the underlying filesystem has
445 * the page on some queue. So the page must be clean for
446 * migration. Writeout may mean we loose the lock and the
447 * page state is no longer what we checked for earlier.
448 * At this point we know that the migration attempt cannot
449 * be successful.
450 */
451 remove_migration_ptes(page, page);
452
453 rc = mapping->a_ops->writepage(page, &wbc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700454
455 if (rc != AOP_WRITEPAGE_ACTIVATE)
456 /* unlocked. Relock */
457 lock_page(page);
458
Hugh Dickinsbda85502008-11-19 15:36:36 -0800459 return (rc < 0) ? -EIO : -EAGAIN;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700460}
461
462/*
463 * Default handling if a filesystem does not provide a migration function.
464 */
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700465static int fallback_migrate_page(struct address_space *mapping,
466 struct page *newpage, struct page *page)
467{
Christoph Lameter04e62a22006-06-23 02:03:38 -0700468 if (PageDirty(page))
469 return writeout(mapping, page);
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700470
471 /*
472 * Buffers may be managed in a filesystem specific way.
473 * We must have no buffers or drop them.
474 */
David Howells266cf652009-04-03 16:42:36 +0100475 if (page_has_private(page) &&
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700476 !try_to_release_page(page, GFP_KERNEL))
477 return -EAGAIN;
478
479 return migrate_page(mapping, newpage, page);
480}
481
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700482/*
Christoph Lametere24f0b82006-06-23 02:03:51 -0700483 * Move a page to a newly allocated page
484 * The page is locked and all ptes have been successfully removed.
485 *
486 * The new page will have replaced the old page if this function
487 * is successful.
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700488 *
489 * Return value:
490 * < 0 - error code
491 * == 0 - success
Christoph Lametere24f0b82006-06-23 02:03:51 -0700492 */
493static int move_to_new_page(struct page *newpage, struct page *page)
494{
495 struct address_space *mapping;
496 int rc;
497
498 /*
499 * Block others from accessing the page when we get around to
500 * establishing additional references. We are the only one
501 * holding a reference to the new page at this point.
502 */
Nick Piggin529ae9a2008-08-02 12:01:03 +0200503 if (!trylock_page(newpage))
Christoph Lametere24f0b82006-06-23 02:03:51 -0700504 BUG();
505
506 /* Prepare mapping for the new page.*/
507 newpage->index = page->index;
508 newpage->mapping = page->mapping;
Rik van Rielb2e18532008-10-18 20:26:30 -0700509 if (PageSwapBacked(page))
510 SetPageSwapBacked(newpage);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700511
512 mapping = page_mapping(page);
513 if (!mapping)
514 rc = migrate_page(mapping, newpage, page);
515 else if (mapping->a_ops->migratepage)
516 /*
517 * Most pages have a mapping and most filesystems
518 * should provide a migration function. Anonymous
519 * pages are part of swap space which also has its
520 * own migration function. This is the most common
521 * path for page migration.
522 */
523 rc = mapping->a_ops->migratepage(mapping,
524 newpage, page);
525 else
526 rc = fallback_migrate_page(mapping, newpage, page);
527
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800528 if (!rc)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700529 remove_migration_ptes(page, newpage);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800530 else
Christoph Lametere24f0b82006-06-23 02:03:51 -0700531 newpage->mapping = NULL;
532
533 unlock_page(newpage);
534
535 return rc;
536}
537
538/*
539 * Obtain the lock on page, remove all ptes and migrate the page
540 * to the newly allocated page in newpage.
541 */
Christoph Lameter95a402c2006-06-23 02:03:53 -0700542static int unmap_and_move(new_page_t get_new_page, unsigned long private,
Hugh Dickins62b61f62009-12-14 17:59:33 -0800543 struct page *page, int force, int offlining)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700544{
545 int rc = 0;
Christoph Lameter742755a2006-06-23 02:03:55 -0700546 int *result = NULL;
547 struct page *newpage = get_new_page(page, private, &result);
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700548 int rcu_locked = 0;
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800549 int charge = 0;
KAMEZAWA Hiroyukie00e4312009-11-11 14:26:26 -0800550 struct mem_cgroup *mem = NULL;
Christoph Lameter95a402c2006-06-23 02:03:53 -0700551
552 if (!newpage)
553 return -ENOMEM;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700554
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700555 if (page_count(page) == 1) {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700556 /* page was freed from under us. So we are done. */
Christoph Lameter95a402c2006-06-23 02:03:53 -0700557 goto move_newpage;
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700558 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700559
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700560 /* prepare cgroup just returns 0 or -ENOMEM */
Christoph Lametere24f0b82006-06-23 02:03:51 -0700561 rc = -EAGAIN;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800562
Nick Piggin529ae9a2008-08-02 12:01:03 +0200563 if (!trylock_page(page)) {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700564 if (!force)
Christoph Lameter95a402c2006-06-23 02:03:53 -0700565 goto move_newpage;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700566 lock_page(page);
567 }
568
Hugh Dickins62b61f62009-12-14 17:59:33 -0800569 /*
570 * Only memory hotplug's offline_pages() caller has locked out KSM,
571 * and can safely migrate a KSM page. The other cases have skipped
572 * PageKsm along with PageReserved - but it is only now when we have
573 * the page lock that we can be certain it will not go KSM beneath us
574 * (KSM will not upgrade a page from PageAnon to PageKsm when it sees
575 * its pagecount raised, but only here do we take the page lock which
576 * serializes that).
577 */
578 if (PageKsm(page) && !offlining) {
579 rc = -EBUSY;
580 goto unlock;
581 }
582
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800583 /* charge against new page */
584 charge = mem_cgroup_prepare_migration(page, &mem);
585 if (charge == -ENOMEM) {
586 rc = -ENOMEM;
587 goto unlock;
588 }
589 BUG_ON(charge);
590
Christoph Lametere24f0b82006-06-23 02:03:51 -0700591 if (PageWriteback(page)) {
592 if (!force)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800593 goto uncharge;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700594 wait_on_page_writeback(page);
595 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700596 /*
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700597 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
598 * we cannot notice that anon_vma is freed while we migrates a page.
599 * This rcu_read_lock() delays freeing anon_vma pointer until the end
600 * of migration. File cache pages are no problem because of page_lock()
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700601 * File Caches may use write_page() or lock_page() in migration, then,
602 * just care Anon page here.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700603 */
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700604 if (PageAnon(page)) {
605 rcu_read_lock();
606 rcu_locked = 1;
607 }
Shaohua Li62e1c552008-02-04 22:29:33 -0800608
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700609 /*
Shaohua Li62e1c552008-02-04 22:29:33 -0800610 * Corner case handling:
611 * 1. When a new swap-cache page is read into, it is added to the LRU
612 * and treated as swapcache but it has no rmap yet.
613 * Calling try_to_unmap() against a page->mapping==NULL page will
614 * trigger a BUG. So handle it here.
615 * 2. An orphaned page (see truncate_complete_page) might have
616 * fs-private metadata. The page can be picked up due to memory
617 * offlining. Everywhere else except page reclaim, the page is
618 * invisible to the vm, so the page can not be migrated. So try to
619 * free the metadata, so the page can be freed.
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700620 */
Shaohua Li62e1c552008-02-04 22:29:33 -0800621 if (!page->mapping) {
David Howells266cf652009-04-03 16:42:36 +0100622 if (!PageAnon(page) && page_has_private(page)) {
Shaohua Li62e1c552008-02-04 22:29:33 -0800623 /*
624 * Go direct to try_to_free_buffers() here because
625 * a) that's what try_to_release_page() would do anyway
626 * b) we may be under rcu_read_lock() here, so we can't
627 * use GFP_KERNEL which is what try_to_release_page()
628 * needs to be effective.
629 */
630 try_to_free_buffers(page);
Shaohua Liabfc3482009-09-21 17:01:19 -0700631 goto rcu_unlock;
Shaohua Li62e1c552008-02-04 22:29:33 -0800632 }
Shaohua Liabfc3482009-09-21 17:01:19 -0700633 goto skip_unmap;
Shaohua Li62e1c552008-02-04 22:29:33 -0800634 }
635
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700636 /* Establish migration ptes or remove ptes */
Andi Kleen14fa31b2009-09-16 11:50:10 +0200637 try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700638
Shaohua Liabfc3482009-09-21 17:01:19 -0700639skip_unmap:
Christoph Lametere6a15302006-06-25 05:46:49 -0700640 if (!page_mapped(page))
641 rc = move_to_new_page(newpage, page);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700642
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700643 if (rc)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700644 remove_migration_ptes(page, page);
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700645rcu_unlock:
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700646 if (rcu_locked)
647 rcu_read_unlock();
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800648uncharge:
649 if (!charge)
650 mem_cgroup_end_migration(mem, page, newpage);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700651unlock:
652 unlock_page(page);
Christoph Lameter95a402c2006-06-23 02:03:53 -0700653
Christoph Lametere24f0b82006-06-23 02:03:51 -0700654 if (rc != -EAGAIN) {
Christoph Lameteraaa994b2006-06-23 02:03:52 -0700655 /*
656 * A page that has been migrated has all references
657 * removed and will be freed. A page that has not been
658 * migrated will have kepts its references and be
659 * restored.
660 */
661 list_del(&page->lru);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -0700662 dec_zone_page_state(page, NR_ISOLATED_ANON +
Johannes Weiner6c0b1352009-09-21 17:02:59 -0700663 page_is_file_cache(page));
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700664 putback_lru_page(page);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700665 }
Christoph Lameter95a402c2006-06-23 02:03:53 -0700666
667move_newpage:
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700668
Christoph Lameter95a402c2006-06-23 02:03:53 -0700669 /*
670 * Move the new page to the LRU. If migration was not successful
671 * then this will free the page.
672 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700673 putback_lru_page(newpage);
674
Christoph Lameter742755a2006-06-23 02:03:55 -0700675 if (result) {
676 if (rc)
677 *result = rc;
678 else
679 *result = page_to_nid(newpage);
680 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700681 return rc;
682}
683
684/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800685 * migrate_pages
686 *
Christoph Lameter95a402c2006-06-23 02:03:53 -0700687 * The function takes one list of pages to migrate and a function
688 * that determines from the page to be migrated and the private data
689 * the target of the move and allocates the page.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800690 *
691 * The function returns after 10 attempts or if no pages
692 * are movable anymore because to has become empty
Christoph Lameteraaa994b2006-06-23 02:03:52 -0700693 * or no retryable pages exist anymore. All pages will be
Gabriel Craciunescue9534b32007-10-20 02:13:26 +0200694 * returned to the LRU or freed.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800695 *
Christoph Lameter95a402c2006-06-23 02:03:53 -0700696 * Return: Number of pages not migrated or error code.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800697 */
Christoph Lameter95a402c2006-06-23 02:03:53 -0700698int migrate_pages(struct list_head *from,
Hugh Dickins62b61f62009-12-14 17:59:33 -0800699 new_page_t get_new_page, unsigned long private, int offlining)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800700{
Christoph Lametere24f0b82006-06-23 02:03:51 -0700701 int retry = 1;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800702 int nr_failed = 0;
703 int pass = 0;
704 struct page *page;
705 struct page *page2;
706 int swapwrite = current->flags & PF_SWAPWRITE;
707 int rc;
708
709 if (!swapwrite)
710 current->flags |= PF_SWAPWRITE;
711
Christoph Lametere24f0b82006-06-23 02:03:51 -0700712 for(pass = 0; pass < 10 && retry; pass++) {
713 retry = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800714
Christoph Lametere24f0b82006-06-23 02:03:51 -0700715 list_for_each_entry_safe(page, page2, from, lru) {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700716 cond_resched();
Christoph Lameterb20a3502006-03-22 00:09:12 -0800717
Christoph Lameter95a402c2006-06-23 02:03:53 -0700718 rc = unmap_and_move(get_new_page, private,
Hugh Dickins62b61f62009-12-14 17:59:33 -0800719 page, pass > 2, offlining);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800720
Christoph Lametere24f0b82006-06-23 02:03:51 -0700721 switch(rc) {
Christoph Lameter95a402c2006-06-23 02:03:53 -0700722 case -ENOMEM:
723 goto out;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700724 case -EAGAIN:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700725 retry++;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700726 break;
727 case 0:
Christoph Lametere24f0b82006-06-23 02:03:51 -0700728 break;
729 default:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700730 /* Permanent failure */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700731 nr_failed++;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700732 break;
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700733 }
Christoph Lameterb20a3502006-03-22 00:09:12 -0800734 }
735 }
Christoph Lameter95a402c2006-06-23 02:03:53 -0700736 rc = 0;
737out:
Christoph Lameterb20a3502006-03-22 00:09:12 -0800738 if (!swapwrite)
739 current->flags &= ~PF_SWAPWRITE;
740
Christoph Lameteraaa994b2006-06-23 02:03:52 -0700741 putback_lru_pages(from);
Christoph Lameter95a402c2006-06-23 02:03:53 -0700742
743 if (rc)
744 return rc;
745
Christoph Lameterb20a3502006-03-22 00:09:12 -0800746 return nr_failed + retry;
747}
748
Christoph Lameter742755a2006-06-23 02:03:55 -0700749#ifdef CONFIG_NUMA
750/*
751 * Move a list of individual pages
752 */
753struct page_to_node {
754 unsigned long addr;
755 struct page *page;
756 int node;
757 int status;
758};
759
760static struct page *new_page_node(struct page *p, unsigned long private,
761 int **result)
762{
763 struct page_to_node *pm = (struct page_to_node *)private;
764
765 while (pm->node != MAX_NUMNODES && pm->page != p)
766 pm++;
767
768 if (pm->node == MAX_NUMNODES)
769 return NULL;
770
771 *result = &pm->status;
772
Mel Gorman6484eb32009-06-16 15:31:54 -0700773 return alloc_pages_exact_node(pm->node,
Mel Gorman769848c2007-07-17 04:03:05 -0700774 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
Christoph Lameter742755a2006-06-23 02:03:55 -0700775}
776
777/*
778 * Move a set of pages as indicated in the pm array. The addr
779 * field must be set to the virtual address of the page to be moved
780 * and the node number must contain a valid target node.
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700781 * The pm array ends with node = MAX_NUMNODES.
Christoph Lameter742755a2006-06-23 02:03:55 -0700782 */
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700783static int do_move_page_to_node_array(struct mm_struct *mm,
784 struct page_to_node *pm,
785 int migrate_all)
Christoph Lameter742755a2006-06-23 02:03:55 -0700786{
787 int err;
788 struct page_to_node *pp;
789 LIST_HEAD(pagelist);
790
791 down_read(&mm->mmap_sem);
792
793 /*
794 * Build a list of pages to migrate
795 */
Christoph Lameter742755a2006-06-23 02:03:55 -0700796 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
797 struct vm_area_struct *vma;
798 struct page *page;
799
Christoph Lameter742755a2006-06-23 02:03:55 -0700800 err = -EFAULT;
801 vma = find_vma(mm, pp->addr);
Christoph Lameter0dc952d2007-03-05 00:30:33 -0800802 if (!vma || !vma_migratable(vma))
Christoph Lameter742755a2006-06-23 02:03:55 -0700803 goto set_status;
804
805 page = follow_page(vma, pp->addr, FOLL_GET);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -0700806
807 err = PTR_ERR(page);
808 if (IS_ERR(page))
809 goto set_status;
810
Christoph Lameter742755a2006-06-23 02:03:55 -0700811 err = -ENOENT;
812 if (!page)
813 goto set_status;
814
Hugh Dickins62b61f62009-12-14 17:59:33 -0800815 /* Use PageReserved to check for zero page */
816 if (PageReserved(page) || PageKsm(page))
Christoph Lameter742755a2006-06-23 02:03:55 -0700817 goto put_and_set;
818
819 pp->page = page;
820 err = page_to_nid(page);
821
822 if (err == pp->node)
823 /*
824 * Node already in the right place
825 */
826 goto put_and_set;
827
828 err = -EACCES;
829 if (page_mapcount(page) > 1 &&
830 !migrate_all)
831 goto put_and_set;
832
Nick Piggin62695a82008-10-18 20:26:09 -0700833 err = isolate_lru_page(page);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -0800834 if (!err) {
Nick Piggin62695a82008-10-18 20:26:09 -0700835 list_add_tail(&page->lru, &pagelist);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -0800836 inc_zone_page_state(page, NR_ISOLATED_ANON +
837 page_is_file_cache(page));
838 }
Christoph Lameter742755a2006-06-23 02:03:55 -0700839put_and_set:
840 /*
841 * Either remove the duplicate refcount from
842 * isolate_lru_page() or drop the page ref if it was
843 * not isolated.
844 */
845 put_page(page);
846set_status:
847 pp->status = err;
848 }
849
Brice Gogline78bbfa2008-10-18 20:27:15 -0700850 err = 0;
Christoph Lameter742755a2006-06-23 02:03:55 -0700851 if (!list_empty(&pagelist))
852 err = migrate_pages(&pagelist, new_page_node,
Hugh Dickins62b61f62009-12-14 17:59:33 -0800853 (unsigned long)pm, 0);
Christoph Lameter742755a2006-06-23 02:03:55 -0700854
855 up_read(&mm->mmap_sem);
856 return err;
857}
858
859/*
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700860 * Migrate an array of page address onto an array of nodes and fill
861 * the corresponding array of status.
862 */
863static int do_pages_move(struct mm_struct *mm, struct task_struct *task,
864 unsigned long nr_pages,
865 const void __user * __user *pages,
866 const int __user *nodes,
867 int __user *status, int flags)
868{
Brice Goglin3140a222009-01-06 14:38:57 -0800869 struct page_to_node *pm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700870 nodemask_t task_nodes;
Brice Goglin3140a222009-01-06 14:38:57 -0800871 unsigned long chunk_nr_pages;
872 unsigned long chunk_start;
873 int err;
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700874
875 task_nodes = cpuset_mems_allowed(task);
876
Brice Goglin3140a222009-01-06 14:38:57 -0800877 err = -ENOMEM;
878 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
879 if (!pm)
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700880 goto out;
Brice Goglin35282a22009-06-16 15:32:43 -0700881
882 migrate_prep();
883
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700884 /*
Brice Goglin3140a222009-01-06 14:38:57 -0800885 * Store a chunk of page_to_node array in a page,
886 * but keep the last one as a marker
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700887 */
Brice Goglin3140a222009-01-06 14:38:57 -0800888 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700889
Brice Goglin3140a222009-01-06 14:38:57 -0800890 for (chunk_start = 0;
891 chunk_start < nr_pages;
892 chunk_start += chunk_nr_pages) {
893 int j;
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700894
Brice Goglin3140a222009-01-06 14:38:57 -0800895 if (chunk_start + chunk_nr_pages > nr_pages)
896 chunk_nr_pages = nr_pages - chunk_start;
897
898 /* fill the chunk pm with addrs and nodes from user-space */
899 for (j = 0; j < chunk_nr_pages; j++) {
900 const void __user *p;
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700901 int node;
902
Brice Goglin3140a222009-01-06 14:38:57 -0800903 err = -EFAULT;
904 if (get_user(p, pages + j + chunk_start))
905 goto out_pm;
906 pm[j].addr = (unsigned long) p;
907
908 if (get_user(node, nodes + j + chunk_start))
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700909 goto out_pm;
910
911 err = -ENODEV;
Linus Torvalds6f5a55f2010-02-05 16:16:50 -0800912 if (node < 0 || node >= MAX_NUMNODES)
913 goto out_pm;
914
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700915 if (!node_state(node, N_HIGH_MEMORY))
916 goto out_pm;
917
918 err = -EACCES;
919 if (!node_isset(node, task_nodes))
920 goto out_pm;
921
Brice Goglin3140a222009-01-06 14:38:57 -0800922 pm[j].node = node;
923 }
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700924
Brice Goglin3140a222009-01-06 14:38:57 -0800925 /* End marker for this chunk */
926 pm[chunk_nr_pages].node = MAX_NUMNODES;
927
928 /* Migrate this chunk */
929 err = do_move_page_to_node_array(mm, pm,
930 flags & MPOL_MF_MOVE_ALL);
931 if (err < 0)
932 goto out_pm;
933
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700934 /* Return status information */
Brice Goglin3140a222009-01-06 14:38:57 -0800935 for (j = 0; j < chunk_nr_pages; j++)
936 if (put_user(pm[j].status, status + j + chunk_start)) {
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700937 err = -EFAULT;
Brice Goglin3140a222009-01-06 14:38:57 -0800938 goto out_pm;
939 }
940 }
941 err = 0;
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700942
943out_pm:
Brice Goglin3140a222009-01-06 14:38:57 -0800944 free_page((unsigned long)pm);
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700945out:
946 return err;
947}
948
949/*
Brice Goglin2f007e72008-10-18 20:27:16 -0700950 * Determine the nodes of an array of pages and store it in an array of status.
Christoph Lameter742755a2006-06-23 02:03:55 -0700951 */
Brice Goglin80bba122008-12-09 13:14:23 -0800952static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
953 const void __user **pages, int *status)
Christoph Lameter742755a2006-06-23 02:03:55 -0700954{
Brice Goglin2f007e72008-10-18 20:27:16 -0700955 unsigned long i;
Brice Goglin2f007e72008-10-18 20:27:16 -0700956
Christoph Lameter742755a2006-06-23 02:03:55 -0700957 down_read(&mm->mmap_sem);
958
Brice Goglin2f007e72008-10-18 20:27:16 -0700959 for (i = 0; i < nr_pages; i++) {
Brice Goglin80bba122008-12-09 13:14:23 -0800960 unsigned long addr = (unsigned long)(*pages);
Christoph Lameter742755a2006-06-23 02:03:55 -0700961 struct vm_area_struct *vma;
962 struct page *page;
KOSAKI Motohiroc095adb2008-12-16 16:06:43 +0900963 int err = -EFAULT;
Brice Goglin2f007e72008-10-18 20:27:16 -0700964
965 vma = find_vma(mm, addr);
Christoph Lameter742755a2006-06-23 02:03:55 -0700966 if (!vma)
967 goto set_status;
968
Brice Goglin2f007e72008-10-18 20:27:16 -0700969 page = follow_page(vma, addr, 0);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -0700970
971 err = PTR_ERR(page);
972 if (IS_ERR(page))
973 goto set_status;
974
Christoph Lameter742755a2006-06-23 02:03:55 -0700975 err = -ENOENT;
976 /* Use PageReserved to check for zero page */
Hugh Dickins62b61f62009-12-14 17:59:33 -0800977 if (!page || PageReserved(page) || PageKsm(page))
Christoph Lameter742755a2006-06-23 02:03:55 -0700978 goto set_status;
979
980 err = page_to_nid(page);
981set_status:
Brice Goglin80bba122008-12-09 13:14:23 -0800982 *status = err;
983
984 pages++;
985 status++;
986 }
987
988 up_read(&mm->mmap_sem);
989}
990
991/*
992 * Determine the nodes of a user array of pages and store it in
993 * a user array of status.
994 */
995static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
996 const void __user * __user *pages,
997 int __user *status)
998{
999#define DO_PAGES_STAT_CHUNK_NR 16
1000 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1001 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
Brice Goglin80bba122008-12-09 13:14:23 -08001002
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001003 while (nr_pages) {
1004 unsigned long chunk_nr;
Brice Goglin80bba122008-12-09 13:14:23 -08001005
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001006 chunk_nr = nr_pages;
1007 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1008 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1009
1010 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
1011 break;
Brice Goglin80bba122008-12-09 13:14:23 -08001012
1013 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1014
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001015 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1016 break;
Christoph Lameter742755a2006-06-23 02:03:55 -07001017
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001018 pages += chunk_nr;
1019 status += chunk_nr;
1020 nr_pages -= chunk_nr;
1021 }
1022 return nr_pages ? -EFAULT : 0;
Christoph Lameter742755a2006-06-23 02:03:55 -07001023}
1024
1025/*
1026 * Move a list of pages in the address space of the currently executing
1027 * process.
1028 */
Heiko Carstens938bb9f2009-01-14 14:14:30 +01001029SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1030 const void __user * __user *, pages,
1031 const int __user *, nodes,
1032 int __user *, status, int, flags)
Christoph Lameter742755a2006-06-23 02:03:55 -07001033{
David Howellsc69e8d92008-11-14 10:39:19 +11001034 const struct cred *cred = current_cred(), *tcred;
Christoph Lameter742755a2006-06-23 02:03:55 -07001035 struct task_struct *task;
Christoph Lameter742755a2006-06-23 02:03:55 -07001036 struct mm_struct *mm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001037 int err;
Christoph Lameter742755a2006-06-23 02:03:55 -07001038
1039 /* Check flags */
1040 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1041 return -EINVAL;
1042
1043 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1044 return -EPERM;
1045
1046 /* Find the mm_struct */
1047 read_lock(&tasklist_lock);
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07001048 task = pid ? find_task_by_vpid(pid) : current;
Christoph Lameter742755a2006-06-23 02:03:55 -07001049 if (!task) {
1050 read_unlock(&tasklist_lock);
1051 return -ESRCH;
1052 }
1053 mm = get_task_mm(task);
1054 read_unlock(&tasklist_lock);
1055
1056 if (!mm)
1057 return -EINVAL;
1058
1059 /*
1060 * Check if this process has the right to modify the specified
1061 * process. The right exists if the process has administrative
1062 * capabilities, superuser privileges or the same
1063 * userid as the target process.
1064 */
David Howellsc69e8d92008-11-14 10:39:19 +11001065 rcu_read_lock();
1066 tcred = __task_cred(task);
David Howellsb6dff3e2008-11-14 10:39:16 +11001067 if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
1068 cred->uid != tcred->suid && cred->uid != tcred->uid &&
Christoph Lameter742755a2006-06-23 02:03:55 -07001069 !capable(CAP_SYS_NICE)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001070 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001071 err = -EPERM;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001072 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001073 }
David Howellsc69e8d92008-11-14 10:39:19 +11001074 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001075
David Quigley86c3a762006-06-23 02:04:02 -07001076 err = security_task_movememory(task);
1077 if (err)
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001078 goto out;
David Quigley86c3a762006-06-23 02:04:02 -07001079
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001080 if (nodes) {
1081 err = do_pages_move(mm, task, nr_pages, pages, nodes, status,
1082 flags);
1083 } else {
Brice Goglin2f007e72008-10-18 20:27:16 -07001084 err = do_pages_stat(mm, nr_pages, pages, status);
Brice Goglin2f007e72008-10-18 20:27:16 -07001085 }
David Quigley86c3a762006-06-23 02:04:02 -07001086
Christoph Lameter742755a2006-06-23 02:03:55 -07001087out:
Christoph Lameter742755a2006-06-23 02:03:55 -07001088 mmput(mm);
1089 return err;
1090}
Christoph Lameter742755a2006-06-23 02:03:55 -07001091
Christoph Lameter7b2259b2006-06-25 05:46:48 -07001092/*
1093 * Call migration functions in the vma_ops that may prepare
1094 * memory in a vm for migration. migration functions may perform
1095 * the migration for vmas that do not have an underlying page struct.
1096 */
1097int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1098 const nodemask_t *from, unsigned long flags)
1099{
1100 struct vm_area_struct *vma;
1101 int err = 0;
1102
Daisuke Nishimura1001c9f2009-02-11 13:04:18 -08001103 for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
Christoph Lameter7b2259b2006-06-25 05:46:48 -07001104 if (vma->vm_ops && vma->vm_ops->migrate) {
1105 err = vma->vm_ops->migrate(vma, to, from, flags);
1106 if (err)
1107 break;
1108 }
1109 }
1110 return err;
1111}
Gerald Schaefer83d16742008-07-23 21:28:22 -07001112#endif