blob: 0b714747c02855badf9f9b39e347ea650aad31f5 [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>
Christoph Lameterb20a3502006-03-22 00:09:12 -080035
36#include "internal.h"
37
Christoph Lameterb20a3502006-03-22 00:09:12 -080038#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
39
40/*
Christoph Lameter742755a2006-06-23 02:03:55 -070041 * migrate_prep() needs to be called before we start compiling a list of pages
42 * to be migrated using isolate_lru_page().
Christoph Lameterb20a3502006-03-22 00:09:12 -080043 */
44int migrate_prep(void)
45{
Christoph Lameterb20a3502006-03-22 00:09:12 -080046 /*
47 * Clear the LRU lists so pages can be isolated.
48 * Note that pages may be moved off the LRU after we have
49 * drained them. Those pages will fail to migrate like other
50 * pages that may be busy.
51 */
52 lru_add_drain_all();
53
54 return 0;
55}
56
Christoph Lameterb20a3502006-03-22 00:09:12 -080057/*
Lee Schermerhorn894bc312008-10-18 20:26:39 -070058 * Add isolated pages on the list back to the LRU under page lock
59 * to avoid leaking evictable pages back onto unevictable list.
Christoph Lameterb20a3502006-03-22 00:09:12 -080060 *
61 * returns the number of pages put back.
62 */
63int putback_lru_pages(struct list_head *l)
64{
65 struct page *page;
66 struct page *page2;
67 int count = 0;
68
69 list_for_each_entry_safe(page, page2, l, lru) {
Christoph Lametere24f0b82006-06-23 02:03:51 -070070 list_del(&page->lru);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -070071 dec_zone_page_state(page, NR_ISOLATED_ANON +
Johannes Weiner6c0b1352009-09-21 17:02:59 -070072 page_is_file_cache(page));
Lee Schermerhorn894bc312008-10-18 20:26:39 -070073 putback_lru_page(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -080074 count++;
75 }
76 return count;
77}
78
Christoph Lameter06972122006-06-23 02:03:35 -070079/*
80 * Restore a potential migration pte to a working pte entry
81 */
Hugh Dickinse9995ef2009-12-14 17:59:31 -080082static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,
83 unsigned long addr, void *old)
Christoph Lameter06972122006-06-23 02:03:35 -070084{
85 struct mm_struct *mm = vma->vm_mm;
86 swp_entry_t entry;
87 pgd_t *pgd;
88 pud_t *pud;
89 pmd_t *pmd;
90 pte_t *ptep, pte;
91 spinlock_t *ptl;
92
93 pgd = pgd_offset(mm, addr);
94 if (!pgd_present(*pgd))
Hugh Dickinse9995ef2009-12-14 17:59:31 -080095 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -070096
97 pud = pud_offset(pgd, addr);
98 if (!pud_present(*pud))
Hugh Dickinse9995ef2009-12-14 17:59:31 -080099 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700100
101 pmd = pmd_offset(pud, addr);
102 if (!pmd_present(*pmd))
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800103 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700104
105 ptep = pte_offset_map(pmd, addr);
106
107 if (!is_swap_pte(*ptep)) {
108 pte_unmap(ptep);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800109 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700110 }
111
112 ptl = pte_lockptr(mm, pmd);
113 spin_lock(ptl);
114 pte = *ptep;
115 if (!is_swap_pte(pte))
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800116 goto unlock;
Christoph Lameter06972122006-06-23 02:03:35 -0700117
118 entry = pte_to_swp_entry(pte);
119
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800120 if (!is_migration_entry(entry) ||
121 migration_entry_to_page(entry) != old)
122 goto unlock;
Christoph Lameter06972122006-06-23 02:03:35 -0700123
Christoph Lameter06972122006-06-23 02:03:35 -0700124 get_page(new);
125 pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
126 if (is_write_migration_entry(entry))
127 pte = pte_mkwrite(pte);
KAMEZAWA Hiroyuki97ee0522007-10-16 01:25:43 -0700128 flush_cache_page(vma, addr, pte_pfn(pte));
Christoph Lameter06972122006-06-23 02:03:35 -0700129 set_pte_at(mm, addr, ptep, pte);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700130
131 if (PageAnon(new))
132 page_add_anon_rmap(new, vma, addr);
133 else
134 page_add_file_rmap(new);
135
136 /* No need to invalidate - it was non-present before */
137 update_mmu_cache(vma, addr, pte);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800138unlock:
Christoph Lameter06972122006-06-23 02:03:35 -0700139 pte_unmap_unlock(ptep, ptl);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800140out:
141 return SWAP_AGAIN;
Christoph Lameter06972122006-06-23 02:03:35 -0700142}
143
144/*
Christoph Lameter04e62a22006-06-23 02:03:38 -0700145 * Get rid of all migration entries and replace them by
146 * references to the indicated page.
147 */
148static void remove_migration_ptes(struct page *old, struct page *new)
149{
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800150 rmap_walk(new, remove_migration_pte, old);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700151}
152
153/*
Christoph Lameter06972122006-06-23 02:03:35 -0700154 * Something used the pte of a page under migration. We need to
155 * get to the page and wait until migration is finished.
156 * When we return from this function the fault will be retried.
157 *
158 * This function is called from do_swap_page().
159 */
160void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
161 unsigned long address)
162{
163 pte_t *ptep, pte;
164 spinlock_t *ptl;
165 swp_entry_t entry;
166 struct page *page;
167
168 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
169 pte = *ptep;
170 if (!is_swap_pte(pte))
171 goto out;
172
173 entry = pte_to_swp_entry(pte);
174 if (!is_migration_entry(entry))
175 goto out;
176
177 page = migration_entry_to_page(entry);
178
Nick Piggine2867812008-07-25 19:45:30 -0700179 /*
180 * Once radix-tree replacement of page migration started, page_count
181 * *must* be zero. And, we don't want to call wait_on_page_locked()
182 * against a page without get_page().
183 * So, we use get_page_unless_zero(), here. Even failed, page fault
184 * will occur again.
185 */
186 if (!get_page_unless_zero(page))
187 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700188 pte_unmap_unlock(ptep, ptl);
189 wait_on_page_locked(page);
190 put_page(page);
191 return;
192out:
193 pte_unmap_unlock(ptep, ptl);
194}
195
Christoph Lameterb20a3502006-03-22 00:09:12 -0800196/*
Christoph Lameterc3fcf8a2006-06-23 02:03:32 -0700197 * Replace the page in the mapping.
Christoph Lameter5b5c7122006-06-23 02:03:29 -0700198 *
199 * The number of remaining references must be:
200 * 1 for anonymous pages without a mapping
201 * 2 for pages with a mapping
David Howells266cf652009-04-03 16:42:36 +0100202 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800203 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700204static int migrate_page_move_mapping(struct address_space *mapping,
205 struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800206{
Nick Piggine2867812008-07-25 19:45:30 -0700207 int expected_count;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800208 void **pslot;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800209
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700210 if (!mapping) {
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700211 /* Anonymous page without mapping */
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700212 if (page_count(page) != 1)
213 return -EAGAIN;
214 return 0;
215 }
216
Nick Piggin19fd6232008-07-25 19:45:32 -0700217 spin_lock_irq(&mapping->tree_lock);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800218
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800219 pslot = radix_tree_lookup_slot(&mapping->page_tree,
220 page_index(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800221
Johannes Weineredcf4742009-09-21 17:02:59 -0700222 expected_count = 2 + page_has_private(page);
Nick Piggine2867812008-07-25 19:45:30 -0700223 if (page_count(page) != expected_count ||
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800224 (struct page *)radix_tree_deref_slot(pslot) != page) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700225 spin_unlock_irq(&mapping->tree_lock);
Christoph Lametere23ca002006-04-10 22:52:57 -0700226 return -EAGAIN;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800227 }
228
Nick Piggine2867812008-07-25 19:45:30 -0700229 if (!page_freeze_refs(page, expected_count)) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700230 spin_unlock_irq(&mapping->tree_lock);
Nick Piggine2867812008-07-25 19:45:30 -0700231 return -EAGAIN;
232 }
233
Christoph Lameterb20a3502006-03-22 00:09:12 -0800234 /*
235 * Now we know that no one else is looking at the page.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800236 */
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800237 get_page(newpage); /* add cache reference */
Christoph Lameterb20a3502006-03-22 00:09:12 -0800238 if (PageSwapCache(page)) {
239 SetPageSwapCache(newpage);
240 set_page_private(newpage, page_private(page));
241 }
242
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800243 radix_tree_replace_slot(pslot, newpage);
244
Nick Piggine2867812008-07-25 19:45:30 -0700245 page_unfreeze_refs(page, expected_count);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800246 /*
247 * Drop cache reference from old page.
248 * We know this isn't the last reference.
249 */
Christoph Lameterb20a3502006-03-22 00:09:12 -0800250 __put_page(page);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800251
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700252 /*
253 * If moved to a different zone then also account
254 * the page for that zone. Other VM counters will be
255 * taken care of when we establish references to the
256 * new page and drop references to the old page.
257 *
258 * Note that anonymous pages are accounted for
259 * via NR_FILE_PAGES and NR_ANON_PAGES if they
260 * are mapped to swap space.
261 */
262 __dec_zone_page_state(page, NR_FILE_PAGES);
263 __inc_zone_page_state(newpage, NR_FILE_PAGES);
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700264 if (PageSwapBacked(page)) {
265 __dec_zone_page_state(page, NR_SHMEM);
266 __inc_zone_page_state(newpage, NR_SHMEM);
267 }
Nick Piggin19fd6232008-07-25 19:45:32 -0700268 spin_unlock_irq(&mapping->tree_lock);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800269
270 return 0;
271}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800272
273/*
274 * Copy the page to its new location
275 */
Christoph Lametere7340f72006-06-23 02:03:29 -0700276static void migrate_page_copy(struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800277{
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -0700278 int anon;
279
Christoph Lameterb20a3502006-03-22 00:09:12 -0800280 copy_highpage(newpage, page);
281
282 if (PageError(page))
283 SetPageError(newpage);
284 if (PageReferenced(page))
285 SetPageReferenced(newpage);
286 if (PageUptodate(page))
287 SetPageUptodate(newpage);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700288 if (TestClearPageActive(page)) {
289 VM_BUG_ON(PageUnevictable(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800290 SetPageActive(newpage);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700291 } else
292 unevictable_migrate_page(newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800293 if (PageChecked(page))
294 SetPageChecked(newpage);
295 if (PageMappedToDisk(page))
296 SetPageMappedToDisk(newpage);
297
298 if (PageDirty(page)) {
299 clear_page_dirty_for_io(page);
Nick Piggin3a902c52008-04-30 00:55:16 -0700300 /*
301 * Want to mark the page and the radix tree as dirty, and
302 * redo the accounting that clear_page_dirty_for_io undid,
303 * but we can't use set_page_dirty because that function
304 * is actually a signal that all of the page has become dirty.
305 * Wheras only part of our page may be dirty.
306 */
307 __set_page_dirty_nobuffers(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800308 }
309
Nick Pigginb291f002008-10-18 20:26:44 -0700310 mlock_migrate_page(newpage, page);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800311 ksm_migrate_page(newpage, page);
Nick Pigginb291f002008-10-18 20:26:44 -0700312
Christoph Lameterb20a3502006-03-22 00:09:12 -0800313 ClearPageSwapCache(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800314 ClearPagePrivate(page);
315 set_page_private(page, 0);
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -0700316 /* page->mapping contains a flag for PageAnon() */
317 anon = PageAnon(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800318 page->mapping = NULL;
319
320 /*
321 * If any waiters have accumulated on the new page then
322 * wake them up.
323 */
324 if (PageWriteback(newpage))
325 end_page_writeback(newpage);
326}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800327
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700328/************************************************************
329 * Migration functions
330 ***********************************************************/
331
332/* Always fail migration. Used for mappings that are not movable */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700333int fail_migrate_page(struct address_space *mapping,
334 struct page *newpage, struct page *page)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700335{
336 return -EIO;
337}
338EXPORT_SYMBOL(fail_migrate_page);
339
Christoph Lameterb20a3502006-03-22 00:09:12 -0800340/*
341 * Common logic to directly migrate a single page suitable for
David Howells266cf652009-04-03 16:42:36 +0100342 * pages that do not use PagePrivate/PagePrivate2.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800343 *
344 * Pages are locked upon entry and exit.
345 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700346int migrate_page(struct address_space *mapping,
347 struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800348{
349 int rc;
350
351 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
352
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700353 rc = migrate_page_move_mapping(mapping, newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800354
355 if (rc)
356 return rc;
357
358 migrate_page_copy(newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800359 return 0;
360}
361EXPORT_SYMBOL(migrate_page);
362
David Howells93614012006-09-30 20:45:40 +0200363#ifdef CONFIG_BLOCK
Christoph Lameterb20a3502006-03-22 00:09:12 -0800364/*
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700365 * Migration function for pages with buffers. This function can only be used
366 * if the underlying filesystem guarantees that no other references to "page"
367 * exist.
368 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700369int buffer_migrate_page(struct address_space *mapping,
370 struct page *newpage, struct page *page)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700371{
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700372 struct buffer_head *bh, *head;
373 int rc;
374
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700375 if (!page_has_buffers(page))
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700376 return migrate_page(mapping, newpage, page);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700377
378 head = page_buffers(page);
379
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700380 rc = migrate_page_move_mapping(mapping, newpage, page);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700381
382 if (rc)
383 return rc;
384
385 bh = head;
386 do {
387 get_bh(bh);
388 lock_buffer(bh);
389 bh = bh->b_this_page;
390
391 } while (bh != head);
392
393 ClearPagePrivate(page);
394 set_page_private(newpage, page_private(page));
395 set_page_private(page, 0);
396 put_page(page);
397 get_page(newpage);
398
399 bh = head;
400 do {
401 set_bh_page(bh, newpage, bh_offset(bh));
402 bh = bh->b_this_page;
403
404 } while (bh != head);
405
406 SetPagePrivate(newpage);
407
408 migrate_page_copy(newpage, page);
409
410 bh = head;
411 do {
412 unlock_buffer(bh);
413 put_bh(bh);
414 bh = bh->b_this_page;
415
416 } while (bh != head);
417
418 return 0;
419}
420EXPORT_SYMBOL(buffer_migrate_page);
David Howells93614012006-09-30 20:45:40 +0200421#endif
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700422
Christoph Lameter04e62a22006-06-23 02:03:38 -0700423/*
424 * Writeback a page to clean the dirty state
425 */
426static int writeout(struct address_space *mapping, struct page *page)
427{
428 struct writeback_control wbc = {
429 .sync_mode = WB_SYNC_NONE,
430 .nr_to_write = 1,
431 .range_start = 0,
432 .range_end = LLONG_MAX,
433 .nonblocking = 1,
434 .for_reclaim = 1
435 };
436 int rc;
437
438 if (!mapping->a_ops->writepage)
439 /* No write method for the address space */
440 return -EINVAL;
441
442 if (!clear_page_dirty_for_io(page))
443 /* Someone else already triggered a write */
444 return -EAGAIN;
445
446 /*
447 * A dirty page may imply that the underlying filesystem has
448 * the page on some queue. So the page must be clean for
449 * migration. Writeout may mean we loose the lock and the
450 * page state is no longer what we checked for earlier.
451 * At this point we know that the migration attempt cannot
452 * be successful.
453 */
454 remove_migration_ptes(page, page);
455
456 rc = mapping->a_ops->writepage(page, &wbc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700457
458 if (rc != AOP_WRITEPAGE_ACTIVATE)
459 /* unlocked. Relock */
460 lock_page(page);
461
Hugh Dickinsbda85502008-11-19 15:36:36 -0800462 return (rc < 0) ? -EIO : -EAGAIN;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700463}
464
465/*
466 * Default handling if a filesystem does not provide a migration function.
467 */
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700468static int fallback_migrate_page(struct address_space *mapping,
469 struct page *newpage, struct page *page)
470{
Christoph Lameter04e62a22006-06-23 02:03:38 -0700471 if (PageDirty(page))
472 return writeout(mapping, page);
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700473
474 /*
475 * Buffers may be managed in a filesystem specific way.
476 * We must have no buffers or drop them.
477 */
David Howells266cf652009-04-03 16:42:36 +0100478 if (page_has_private(page) &&
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700479 !try_to_release_page(page, GFP_KERNEL))
480 return -EAGAIN;
481
482 return migrate_page(mapping, newpage, page);
483}
484
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700485/*
Christoph Lametere24f0b82006-06-23 02:03:51 -0700486 * Move a page to a newly allocated page
487 * The page is locked and all ptes have been successfully removed.
488 *
489 * The new page will have replaced the old page if this function
490 * is successful.
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700491 *
492 * Return value:
493 * < 0 - error code
494 * == 0 - success
Christoph Lametere24f0b82006-06-23 02:03:51 -0700495 */
496static int move_to_new_page(struct page *newpage, struct page *page)
497{
498 struct address_space *mapping;
499 int rc;
500
501 /*
502 * Block others from accessing the page when we get around to
503 * establishing additional references. We are the only one
504 * holding a reference to the new page at this point.
505 */
Nick Piggin529ae9a2008-08-02 12:01:03 +0200506 if (!trylock_page(newpage))
Christoph Lametere24f0b82006-06-23 02:03:51 -0700507 BUG();
508
509 /* Prepare mapping for the new page.*/
510 newpage->index = page->index;
511 newpage->mapping = page->mapping;
Rik van Rielb2e18532008-10-18 20:26:30 -0700512 if (PageSwapBacked(page))
513 SetPageSwapBacked(newpage);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700514
515 mapping = page_mapping(page);
516 if (!mapping)
517 rc = migrate_page(mapping, newpage, page);
518 else if (mapping->a_ops->migratepage)
519 /*
520 * Most pages have a mapping and most filesystems
521 * should provide a migration function. Anonymous
522 * pages are part of swap space which also has its
523 * own migration function. This is the most common
524 * path for page migration.
525 */
526 rc = mapping->a_ops->migratepage(mapping,
527 newpage, page);
528 else
529 rc = fallback_migrate_page(mapping, newpage, page);
530
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800531 if (!rc)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700532 remove_migration_ptes(page, newpage);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800533 else
Christoph Lametere24f0b82006-06-23 02:03:51 -0700534 newpage->mapping = NULL;
535
536 unlock_page(newpage);
537
538 return rc;
539}
540
541/*
542 * Obtain the lock on page, remove all ptes and migrate the page
543 * to the newly allocated page in newpage.
544 */
Christoph Lameter95a402c2006-06-23 02:03:53 -0700545static int unmap_and_move(new_page_t get_new_page, unsigned long private,
546 struct page *page, int force)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700547{
548 int rc = 0;
Christoph Lameter742755a2006-06-23 02:03:55 -0700549 int *result = NULL;
550 struct page *newpage = get_new_page(page, private, &result);
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700551 int rcu_locked = 0;
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800552 int charge = 0;
KAMEZAWA Hiroyukie00e4312009-11-11 14:26:26 -0800553 struct mem_cgroup *mem = NULL;
Christoph Lameter95a402c2006-06-23 02:03:53 -0700554
555 if (!newpage)
556 return -ENOMEM;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700557
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700558 if (page_count(page) == 1) {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700559 /* page was freed from under us. So we are done. */
Christoph Lameter95a402c2006-06-23 02:03:53 -0700560 goto move_newpage;
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700561 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700562
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700563 /* prepare cgroup just returns 0 or -ENOMEM */
Christoph Lametere24f0b82006-06-23 02:03:51 -0700564 rc = -EAGAIN;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800565
Nick Piggin529ae9a2008-08-02 12:01:03 +0200566 if (!trylock_page(page)) {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700567 if (!force)
Christoph Lameter95a402c2006-06-23 02:03:53 -0700568 goto move_newpage;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700569 lock_page(page);
570 }
571
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800572 /* charge against new page */
573 charge = mem_cgroup_prepare_migration(page, &mem);
574 if (charge == -ENOMEM) {
575 rc = -ENOMEM;
576 goto unlock;
577 }
578 BUG_ON(charge);
579
Christoph Lametere24f0b82006-06-23 02:03:51 -0700580 if (PageWriteback(page)) {
581 if (!force)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800582 goto uncharge;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700583 wait_on_page_writeback(page);
584 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700585 /*
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700586 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
587 * we cannot notice that anon_vma is freed while we migrates a page.
588 * This rcu_read_lock() delays freeing anon_vma pointer until the end
589 * of migration. File cache pages are no problem because of page_lock()
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700590 * File Caches may use write_page() or lock_page() in migration, then,
591 * just care Anon page here.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700592 */
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700593 if (PageAnon(page)) {
594 rcu_read_lock();
595 rcu_locked = 1;
596 }
Shaohua Li62e1c552008-02-04 22:29:33 -0800597
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700598 /*
Shaohua Li62e1c552008-02-04 22:29:33 -0800599 * Corner case handling:
600 * 1. When a new swap-cache page is read into, it is added to the LRU
601 * and treated as swapcache but it has no rmap yet.
602 * Calling try_to_unmap() against a page->mapping==NULL page will
603 * trigger a BUG. So handle it here.
604 * 2. An orphaned page (see truncate_complete_page) might have
605 * fs-private metadata. The page can be picked up due to memory
606 * offlining. Everywhere else except page reclaim, the page is
607 * invisible to the vm, so the page can not be migrated. So try to
608 * free the metadata, so the page can be freed.
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700609 */
Shaohua Li62e1c552008-02-04 22:29:33 -0800610 if (!page->mapping) {
David Howells266cf652009-04-03 16:42:36 +0100611 if (!PageAnon(page) && page_has_private(page)) {
Shaohua Li62e1c552008-02-04 22:29:33 -0800612 /*
613 * Go direct to try_to_free_buffers() here because
614 * a) that's what try_to_release_page() would do anyway
615 * b) we may be under rcu_read_lock() here, so we can't
616 * use GFP_KERNEL which is what try_to_release_page()
617 * needs to be effective.
618 */
619 try_to_free_buffers(page);
Shaohua Liabfc3482009-09-21 17:01:19 -0700620 goto rcu_unlock;
Shaohua Li62e1c552008-02-04 22:29:33 -0800621 }
Shaohua Liabfc3482009-09-21 17:01:19 -0700622 goto skip_unmap;
Shaohua Li62e1c552008-02-04 22:29:33 -0800623 }
624
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700625 /* Establish migration ptes or remove ptes */
Andi Kleen14fa31b2009-09-16 11:50:10 +0200626 try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700627
Shaohua Liabfc3482009-09-21 17:01:19 -0700628skip_unmap:
Christoph Lametere6a15302006-06-25 05:46:49 -0700629 if (!page_mapped(page))
630 rc = move_to_new_page(newpage, page);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700631
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700632 if (rc)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700633 remove_migration_ptes(page, page);
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700634rcu_unlock:
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700635 if (rcu_locked)
636 rcu_read_unlock();
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800637uncharge:
638 if (!charge)
639 mem_cgroup_end_migration(mem, page, newpage);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700640unlock:
641 unlock_page(page);
Christoph Lameter95a402c2006-06-23 02:03:53 -0700642
Christoph Lametere24f0b82006-06-23 02:03:51 -0700643 if (rc != -EAGAIN) {
Christoph Lameteraaa994b2006-06-23 02:03:52 -0700644 /*
645 * A page that has been migrated has all references
646 * removed and will be freed. A page that has not been
647 * migrated will have kepts its references and be
648 * restored.
649 */
650 list_del(&page->lru);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -0700651 dec_zone_page_state(page, NR_ISOLATED_ANON +
Johannes Weiner6c0b1352009-09-21 17:02:59 -0700652 page_is_file_cache(page));
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700653 putback_lru_page(page);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700654 }
Christoph Lameter95a402c2006-06-23 02:03:53 -0700655
656move_newpage:
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700657
Christoph Lameter95a402c2006-06-23 02:03:53 -0700658 /*
659 * Move the new page to the LRU. If migration was not successful
660 * then this will free the page.
661 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700662 putback_lru_page(newpage);
663
Christoph Lameter742755a2006-06-23 02:03:55 -0700664 if (result) {
665 if (rc)
666 *result = rc;
667 else
668 *result = page_to_nid(newpage);
669 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700670 return rc;
671}
672
673/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800674 * migrate_pages
675 *
Christoph Lameter95a402c2006-06-23 02:03:53 -0700676 * The function takes one list of pages to migrate and a function
677 * that determines from the page to be migrated and the private data
678 * the target of the move and allocates the page.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800679 *
680 * The function returns after 10 attempts or if no pages
681 * are movable anymore because to has become empty
Christoph Lameteraaa994b2006-06-23 02:03:52 -0700682 * or no retryable pages exist anymore. All pages will be
Gabriel Craciunescue9534b32007-10-20 02:13:26 +0200683 * returned to the LRU or freed.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800684 *
Christoph Lameter95a402c2006-06-23 02:03:53 -0700685 * Return: Number of pages not migrated or error code.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800686 */
Christoph Lameter95a402c2006-06-23 02:03:53 -0700687int migrate_pages(struct list_head *from,
688 new_page_t get_new_page, unsigned long private)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800689{
Christoph Lametere24f0b82006-06-23 02:03:51 -0700690 int retry = 1;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800691 int nr_failed = 0;
692 int pass = 0;
693 struct page *page;
694 struct page *page2;
695 int swapwrite = current->flags & PF_SWAPWRITE;
696 int rc;
697
698 if (!swapwrite)
699 current->flags |= PF_SWAPWRITE;
700
Christoph Lametere24f0b82006-06-23 02:03:51 -0700701 for(pass = 0; pass < 10 && retry; pass++) {
702 retry = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800703
Christoph Lametere24f0b82006-06-23 02:03:51 -0700704 list_for_each_entry_safe(page, page2, from, lru) {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700705 cond_resched();
Christoph Lameterb20a3502006-03-22 00:09:12 -0800706
Christoph Lameter95a402c2006-06-23 02:03:53 -0700707 rc = unmap_and_move(get_new_page, private,
708 page, pass > 2);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800709
Christoph Lametere24f0b82006-06-23 02:03:51 -0700710 switch(rc) {
Christoph Lameter95a402c2006-06-23 02:03:53 -0700711 case -ENOMEM:
712 goto out;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700713 case -EAGAIN:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700714 retry++;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700715 break;
716 case 0:
Christoph Lametere24f0b82006-06-23 02:03:51 -0700717 break;
718 default:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700719 /* Permanent failure */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700720 nr_failed++;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700721 break;
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700722 }
Christoph Lameterb20a3502006-03-22 00:09:12 -0800723 }
724 }
Christoph Lameter95a402c2006-06-23 02:03:53 -0700725 rc = 0;
726out:
Christoph Lameterb20a3502006-03-22 00:09:12 -0800727 if (!swapwrite)
728 current->flags &= ~PF_SWAPWRITE;
729
Christoph Lameteraaa994b2006-06-23 02:03:52 -0700730 putback_lru_pages(from);
Christoph Lameter95a402c2006-06-23 02:03:53 -0700731
732 if (rc)
733 return rc;
734
Christoph Lameterb20a3502006-03-22 00:09:12 -0800735 return nr_failed + retry;
736}
737
Christoph Lameter742755a2006-06-23 02:03:55 -0700738#ifdef CONFIG_NUMA
739/*
740 * Move a list of individual pages
741 */
742struct page_to_node {
743 unsigned long addr;
744 struct page *page;
745 int node;
746 int status;
747};
748
749static struct page *new_page_node(struct page *p, unsigned long private,
750 int **result)
751{
752 struct page_to_node *pm = (struct page_to_node *)private;
753
754 while (pm->node != MAX_NUMNODES && pm->page != p)
755 pm++;
756
757 if (pm->node == MAX_NUMNODES)
758 return NULL;
759
760 *result = &pm->status;
761
Mel Gorman6484eb32009-06-16 15:31:54 -0700762 return alloc_pages_exact_node(pm->node,
Mel Gorman769848c2007-07-17 04:03:05 -0700763 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
Christoph Lameter742755a2006-06-23 02:03:55 -0700764}
765
766/*
767 * Move a set of pages as indicated in the pm array. The addr
768 * field must be set to the virtual address of the page to be moved
769 * and the node number must contain a valid target node.
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700770 * The pm array ends with node = MAX_NUMNODES.
Christoph Lameter742755a2006-06-23 02:03:55 -0700771 */
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700772static int do_move_page_to_node_array(struct mm_struct *mm,
773 struct page_to_node *pm,
774 int migrate_all)
Christoph Lameter742755a2006-06-23 02:03:55 -0700775{
776 int err;
777 struct page_to_node *pp;
778 LIST_HEAD(pagelist);
779
780 down_read(&mm->mmap_sem);
781
782 /*
783 * Build a list of pages to migrate
784 */
Christoph Lameter742755a2006-06-23 02:03:55 -0700785 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
786 struct vm_area_struct *vma;
787 struct page *page;
788
Christoph Lameter742755a2006-06-23 02:03:55 -0700789 err = -EFAULT;
790 vma = find_vma(mm, pp->addr);
Christoph Lameter0dc952d2007-03-05 00:30:33 -0800791 if (!vma || !vma_migratable(vma))
Christoph Lameter742755a2006-06-23 02:03:55 -0700792 goto set_status;
793
794 page = follow_page(vma, pp->addr, FOLL_GET);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -0700795
796 err = PTR_ERR(page);
797 if (IS_ERR(page))
798 goto set_status;
799
Christoph Lameter742755a2006-06-23 02:03:55 -0700800 err = -ENOENT;
801 if (!page)
802 goto set_status;
803
804 if (PageReserved(page)) /* Check for zero page */
805 goto put_and_set;
806
807 pp->page = page;
808 err = page_to_nid(page);
809
810 if (err == pp->node)
811 /*
812 * Node already in the right place
813 */
814 goto put_and_set;
815
816 err = -EACCES;
817 if (page_mapcount(page) > 1 &&
818 !migrate_all)
819 goto put_and_set;
820
Nick Piggin62695a82008-10-18 20:26:09 -0700821 err = isolate_lru_page(page);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -0800822 if (!err) {
Nick Piggin62695a82008-10-18 20:26:09 -0700823 list_add_tail(&page->lru, &pagelist);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -0800824 inc_zone_page_state(page, NR_ISOLATED_ANON +
825 page_is_file_cache(page));
826 }
Christoph Lameter742755a2006-06-23 02:03:55 -0700827put_and_set:
828 /*
829 * Either remove the duplicate refcount from
830 * isolate_lru_page() or drop the page ref if it was
831 * not isolated.
832 */
833 put_page(page);
834set_status:
835 pp->status = err;
836 }
837
Brice Gogline78bbfa2008-10-18 20:27:15 -0700838 err = 0;
Christoph Lameter742755a2006-06-23 02:03:55 -0700839 if (!list_empty(&pagelist))
840 err = migrate_pages(&pagelist, new_page_node,
841 (unsigned long)pm);
Christoph Lameter742755a2006-06-23 02:03:55 -0700842
843 up_read(&mm->mmap_sem);
844 return err;
845}
846
847/*
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700848 * Migrate an array of page address onto an array of nodes and fill
849 * the corresponding array of status.
850 */
851static int do_pages_move(struct mm_struct *mm, struct task_struct *task,
852 unsigned long nr_pages,
853 const void __user * __user *pages,
854 const int __user *nodes,
855 int __user *status, int flags)
856{
Brice Goglin3140a222009-01-06 14:38:57 -0800857 struct page_to_node *pm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700858 nodemask_t task_nodes;
Brice Goglin3140a222009-01-06 14:38:57 -0800859 unsigned long chunk_nr_pages;
860 unsigned long chunk_start;
861 int err;
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700862
863 task_nodes = cpuset_mems_allowed(task);
864
Brice Goglin3140a222009-01-06 14:38:57 -0800865 err = -ENOMEM;
866 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
867 if (!pm)
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700868 goto out;
Brice Goglin35282a22009-06-16 15:32:43 -0700869
870 migrate_prep();
871
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700872 /*
Brice Goglin3140a222009-01-06 14:38:57 -0800873 * Store a chunk of page_to_node array in a page,
874 * but keep the last one as a marker
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700875 */
Brice Goglin3140a222009-01-06 14:38:57 -0800876 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700877
Brice Goglin3140a222009-01-06 14:38:57 -0800878 for (chunk_start = 0;
879 chunk_start < nr_pages;
880 chunk_start += chunk_nr_pages) {
881 int j;
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700882
Brice Goglin3140a222009-01-06 14:38:57 -0800883 if (chunk_start + chunk_nr_pages > nr_pages)
884 chunk_nr_pages = nr_pages - chunk_start;
885
886 /* fill the chunk pm with addrs and nodes from user-space */
887 for (j = 0; j < chunk_nr_pages; j++) {
888 const void __user *p;
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700889 int node;
890
Brice Goglin3140a222009-01-06 14:38:57 -0800891 err = -EFAULT;
892 if (get_user(p, pages + j + chunk_start))
893 goto out_pm;
894 pm[j].addr = (unsigned long) p;
895
896 if (get_user(node, nodes + j + chunk_start))
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700897 goto out_pm;
898
899 err = -ENODEV;
900 if (!node_state(node, N_HIGH_MEMORY))
901 goto out_pm;
902
903 err = -EACCES;
904 if (!node_isset(node, task_nodes))
905 goto out_pm;
906
Brice Goglin3140a222009-01-06 14:38:57 -0800907 pm[j].node = node;
908 }
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700909
Brice Goglin3140a222009-01-06 14:38:57 -0800910 /* End marker for this chunk */
911 pm[chunk_nr_pages].node = MAX_NUMNODES;
912
913 /* Migrate this chunk */
914 err = do_move_page_to_node_array(mm, pm,
915 flags & MPOL_MF_MOVE_ALL);
916 if (err < 0)
917 goto out_pm;
918
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700919 /* Return status information */
Brice Goglin3140a222009-01-06 14:38:57 -0800920 for (j = 0; j < chunk_nr_pages; j++)
921 if (put_user(pm[j].status, status + j + chunk_start)) {
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700922 err = -EFAULT;
Brice Goglin3140a222009-01-06 14:38:57 -0800923 goto out_pm;
924 }
925 }
926 err = 0;
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700927
928out_pm:
Brice Goglin3140a222009-01-06 14:38:57 -0800929 free_page((unsigned long)pm);
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700930out:
931 return err;
932}
933
934/*
Brice Goglin2f007e72008-10-18 20:27:16 -0700935 * Determine the nodes of an array of pages and store it in an array of status.
Christoph Lameter742755a2006-06-23 02:03:55 -0700936 */
Brice Goglin80bba122008-12-09 13:14:23 -0800937static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
938 const void __user **pages, int *status)
Christoph Lameter742755a2006-06-23 02:03:55 -0700939{
Brice Goglin2f007e72008-10-18 20:27:16 -0700940 unsigned long i;
Brice Goglin2f007e72008-10-18 20:27:16 -0700941
Christoph Lameter742755a2006-06-23 02:03:55 -0700942 down_read(&mm->mmap_sem);
943
Brice Goglin2f007e72008-10-18 20:27:16 -0700944 for (i = 0; i < nr_pages; i++) {
Brice Goglin80bba122008-12-09 13:14:23 -0800945 unsigned long addr = (unsigned long)(*pages);
Christoph Lameter742755a2006-06-23 02:03:55 -0700946 struct vm_area_struct *vma;
947 struct page *page;
KOSAKI Motohiroc095adb2008-12-16 16:06:43 +0900948 int err = -EFAULT;
Brice Goglin2f007e72008-10-18 20:27:16 -0700949
950 vma = find_vma(mm, addr);
Christoph Lameter742755a2006-06-23 02:03:55 -0700951 if (!vma)
952 goto set_status;
953
Brice Goglin2f007e72008-10-18 20:27:16 -0700954 page = follow_page(vma, addr, 0);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -0700955
956 err = PTR_ERR(page);
957 if (IS_ERR(page))
958 goto set_status;
959
Christoph Lameter742755a2006-06-23 02:03:55 -0700960 err = -ENOENT;
961 /* Use PageReserved to check for zero page */
962 if (!page || PageReserved(page))
963 goto set_status;
964
965 err = page_to_nid(page);
966set_status:
Brice Goglin80bba122008-12-09 13:14:23 -0800967 *status = err;
968
969 pages++;
970 status++;
971 }
972
973 up_read(&mm->mmap_sem);
974}
975
976/*
977 * Determine the nodes of a user array of pages and store it in
978 * a user array of status.
979 */
980static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
981 const void __user * __user *pages,
982 int __user *status)
983{
984#define DO_PAGES_STAT_CHUNK_NR 16
985 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
986 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
987 unsigned long i, chunk_nr = DO_PAGES_STAT_CHUNK_NR;
988 int err;
989
990 for (i = 0; i < nr_pages; i += chunk_nr) {
H. Peter Anvinb92558502009-12-08 14:01:32 -0800991 if (chunk_nr > nr_pages - i)
Brice Goglin80bba122008-12-09 13:14:23 -0800992 chunk_nr = nr_pages - i;
993
994 err = copy_from_user(chunk_pages, &pages[i],
995 chunk_nr * sizeof(*chunk_pages));
996 if (err) {
997 err = -EFAULT;
998 goto out;
999 }
1000
1001 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1002
1003 err = copy_to_user(&status[i], chunk_status,
1004 chunk_nr * sizeof(*chunk_status));
1005 if (err) {
1006 err = -EFAULT;
1007 goto out;
1008 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001009 }
Brice Goglin2f007e72008-10-18 20:27:16 -07001010 err = 0;
Christoph Lameter742755a2006-06-23 02:03:55 -07001011
Brice Goglin2f007e72008-10-18 20:27:16 -07001012out:
Brice Goglin2f007e72008-10-18 20:27:16 -07001013 return err;
Christoph Lameter742755a2006-06-23 02:03:55 -07001014}
1015
1016/*
1017 * Move a list of pages in the address space of the currently executing
1018 * process.
1019 */
Heiko Carstens938bb9f2009-01-14 14:14:30 +01001020SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1021 const void __user * __user *, pages,
1022 const int __user *, nodes,
1023 int __user *, status, int, flags)
Christoph Lameter742755a2006-06-23 02:03:55 -07001024{
David Howellsc69e8d92008-11-14 10:39:19 +11001025 const struct cred *cred = current_cred(), *tcred;
Christoph Lameter742755a2006-06-23 02:03:55 -07001026 struct task_struct *task;
Christoph Lameter742755a2006-06-23 02:03:55 -07001027 struct mm_struct *mm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001028 int err;
Christoph Lameter742755a2006-06-23 02:03:55 -07001029
1030 /* Check flags */
1031 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1032 return -EINVAL;
1033
1034 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1035 return -EPERM;
1036
1037 /* Find the mm_struct */
1038 read_lock(&tasklist_lock);
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07001039 task = pid ? find_task_by_vpid(pid) : current;
Christoph Lameter742755a2006-06-23 02:03:55 -07001040 if (!task) {
1041 read_unlock(&tasklist_lock);
1042 return -ESRCH;
1043 }
1044 mm = get_task_mm(task);
1045 read_unlock(&tasklist_lock);
1046
1047 if (!mm)
1048 return -EINVAL;
1049
1050 /*
1051 * Check if this process has the right to modify the specified
1052 * process. The right exists if the process has administrative
1053 * capabilities, superuser privileges or the same
1054 * userid as the target process.
1055 */
David Howellsc69e8d92008-11-14 10:39:19 +11001056 rcu_read_lock();
1057 tcred = __task_cred(task);
David Howellsb6dff3e2008-11-14 10:39:16 +11001058 if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
1059 cred->uid != tcred->suid && cred->uid != tcred->uid &&
Christoph Lameter742755a2006-06-23 02:03:55 -07001060 !capable(CAP_SYS_NICE)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001061 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001062 err = -EPERM;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001063 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001064 }
David Howellsc69e8d92008-11-14 10:39:19 +11001065 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001066
David Quigley86c3a762006-06-23 02:04:02 -07001067 err = security_task_movememory(task);
1068 if (err)
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001069 goto out;
David Quigley86c3a762006-06-23 02:04:02 -07001070
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001071 if (nodes) {
1072 err = do_pages_move(mm, task, nr_pages, pages, nodes, status,
1073 flags);
1074 } else {
Brice Goglin2f007e72008-10-18 20:27:16 -07001075 err = do_pages_stat(mm, nr_pages, pages, status);
Brice Goglin2f007e72008-10-18 20:27:16 -07001076 }
David Quigley86c3a762006-06-23 02:04:02 -07001077
Christoph Lameter742755a2006-06-23 02:03:55 -07001078out:
Christoph Lameter742755a2006-06-23 02:03:55 -07001079 mmput(mm);
1080 return err;
1081}
Christoph Lameter742755a2006-06-23 02:03:55 -07001082
Christoph Lameter7b2259b2006-06-25 05:46:48 -07001083/*
1084 * Call migration functions in the vma_ops that may prepare
1085 * memory in a vm for migration. migration functions may perform
1086 * the migration for vmas that do not have an underlying page struct.
1087 */
1088int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1089 const nodemask_t *from, unsigned long flags)
1090{
1091 struct vm_area_struct *vma;
1092 int err = 0;
1093
Daisuke Nishimura1001c9f2009-02-11 13:04:18 -08001094 for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
Christoph Lameter7b2259b2006-06-25 05:46:48 -07001095 if (vma->vm_ops && vma->vm_ops->migrate) {
1096 err = vma->vm_ops->migrate(vma, to, from, flags);
1097 if (err)
1098 break;
1099 }
1100 }
1101 return err;
1102}
Gerald Schaefer83d16742008-07-23 21:28:22 -07001103#endif