blob: 367272d0442311c9ec4e38f3ae2c99618ea04f63 [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>
24#include <linux/rmap.h>
25#include <linux/topology.h>
26#include <linux/cpu.h>
27#include <linux/cpuset.h>
Christoph Lameter04e62a22006-06-23 02:03:38 -070028#include <linux/writeback.h>
Christoph Lameter742755a2006-06-23 02:03:55 -070029#include <linux/mempolicy.h>
30#include <linux/vmalloc.h>
David Quigley86c3a762006-06-23 02:04:02 -070031#include <linux/security.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080032#include <linux/memcontrol.h>
Adrian Bunk4f5ca262008-07-23 21:27:02 -070033#include <linux/syscalls.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080034
35#include "internal.h"
36
Christoph Lameterb20a3502006-03-22 00:09:12 -080037#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
38
39/*
Christoph Lameter742755a2006-06-23 02:03:55 -070040 * migrate_prep() needs to be called before we start compiling a list of pages
41 * to be migrated using isolate_lru_page().
Christoph Lameterb20a3502006-03-22 00:09:12 -080042 */
43int migrate_prep(void)
44{
Christoph Lameterb20a3502006-03-22 00:09:12 -080045 /*
46 * Clear the LRU lists so pages can be isolated.
47 * Note that pages may be moved off the LRU after we have
48 * drained them. Those pages will fail to migrate like other
49 * pages that may be busy.
50 */
51 lru_add_drain_all();
52
53 return 0;
54}
55
Christoph Lameterb20a3502006-03-22 00:09:12 -080056/*
Lee Schermerhorn894bc312008-10-18 20:26:39 -070057 * Add isolated pages on the list back to the LRU under page lock
58 * to avoid leaking evictable pages back onto unevictable list.
Christoph Lameterb20a3502006-03-22 00:09:12 -080059 *
60 * returns the number of pages put back.
61 */
62int putback_lru_pages(struct list_head *l)
63{
64 struct page *page;
65 struct page *page2;
66 int count = 0;
67
68 list_for_each_entry_safe(page, page2, l, lru) {
Christoph Lametere24f0b82006-06-23 02:03:51 -070069 list_del(&page->lru);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -070070 dec_zone_page_state(page, NR_ISOLATED_ANON +
Johannes Weiner6c0b1352009-09-21 17:02:59 -070071 page_is_file_cache(page));
Lee Schermerhorn894bc312008-10-18 20:26:39 -070072 putback_lru_page(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -080073 count++;
74 }
75 return count;
76}
77
Christoph Lameter06972122006-06-23 02:03:35 -070078/*
79 * Restore a potential migration pte to a working pte entry
80 */
Christoph Lameter04e62a22006-06-23 02:03:38 -070081static void remove_migration_pte(struct vm_area_struct *vma,
Christoph Lameter06972122006-06-23 02:03:35 -070082 struct page *old, struct page *new)
83{
84 struct mm_struct *mm = vma->vm_mm;
85 swp_entry_t entry;
86 pgd_t *pgd;
87 pud_t *pud;
88 pmd_t *pmd;
89 pte_t *ptep, pte;
90 spinlock_t *ptl;
Christoph Lameter04e62a22006-06-23 02:03:38 -070091 unsigned long addr = page_address_in_vma(new, vma);
92
93 if (addr == -EFAULT)
94 return;
Christoph Lameter06972122006-06-23 02:03:35 -070095
96 pgd = pgd_offset(mm, addr);
97 if (!pgd_present(*pgd))
98 return;
99
100 pud = pud_offset(pgd, addr);
101 if (!pud_present(*pud))
102 return;
103
104 pmd = pmd_offset(pud, addr);
105 if (!pmd_present(*pmd))
106 return;
107
108 ptep = pte_offset_map(pmd, addr);
109
110 if (!is_swap_pte(*ptep)) {
111 pte_unmap(ptep);
112 return;
113 }
114
115 ptl = pte_lockptr(mm, pmd);
116 spin_lock(ptl);
117 pte = *ptep;
118 if (!is_swap_pte(pte))
119 goto out;
120
121 entry = pte_to_swp_entry(pte);
122
123 if (!is_migration_entry(entry) || migration_entry_to_page(entry) != old)
124 goto out;
125
Christoph Lameter06972122006-06-23 02:03:35 -0700126 get_page(new);
127 pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
128 if (is_write_migration_entry(entry))
129 pte = pte_mkwrite(pte);
KAMEZAWA Hiroyuki97ee0522007-10-16 01:25:43 -0700130 flush_cache_page(vma, addr, pte_pfn(pte));
Christoph Lameter06972122006-06-23 02:03:35 -0700131 set_pte_at(mm, addr, ptep, pte);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700132
133 if (PageAnon(new))
134 page_add_anon_rmap(new, vma, addr);
135 else
136 page_add_file_rmap(new);
137
138 /* No need to invalidate - it was non-present before */
139 update_mmu_cache(vma, addr, pte);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700140
Christoph Lameter06972122006-06-23 02:03:35 -0700141out:
142 pte_unmap_unlock(ptep, ptl);
143}
144
145/*
Christoph Lameter04e62a22006-06-23 02:03:38 -0700146 * Note that remove_file_migration_ptes will only work on regular mappings,
147 * Nonlinear mappings do not use migration entries.
148 */
149static void remove_file_migration_ptes(struct page *old, struct page *new)
150{
151 struct vm_area_struct *vma;
Shaohua Liabfc3482009-09-21 17:01:19 -0700152 struct address_space *mapping = new->mapping;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700153 struct prio_tree_iter iter;
154 pgoff_t pgoff = new->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
155
156 if (!mapping)
157 return;
158
159 spin_lock(&mapping->i_mmap_lock);
160
161 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff)
162 remove_migration_pte(vma, old, new);
163
164 spin_unlock(&mapping->i_mmap_lock);
165}
166
167/*
Christoph Lameter06972122006-06-23 02:03:35 -0700168 * Must hold mmap_sem lock on at least one of the vmas containing
169 * the page so that the anon_vma cannot vanish.
170 */
Christoph Lameter04e62a22006-06-23 02:03:38 -0700171static void remove_anon_migration_ptes(struct page *old, struct page *new)
Christoph Lameter06972122006-06-23 02:03:35 -0700172{
173 struct anon_vma *anon_vma;
174 struct vm_area_struct *vma;
Christoph Lameter06972122006-06-23 02:03:35 -0700175
176 /*
177 * We hold the mmap_sem lock. So no need to call page_lock_anon_vma.
178 */
Hugh Dickins3ca7b3c2009-12-14 17:58:57 -0800179 anon_vma = page_anon_vma(new);
180 if (!anon_vma)
181 return;
182
Christoph Lameter06972122006-06-23 02:03:35 -0700183 spin_lock(&anon_vma->lock);
184
185 list_for_each_entry(vma, &anon_vma->head, anon_vma_node)
Christoph Lameter04e62a22006-06-23 02:03:38 -0700186 remove_migration_pte(vma, old, new);
Christoph Lameter06972122006-06-23 02:03:35 -0700187
188 spin_unlock(&anon_vma->lock);
189}
190
191/*
Christoph Lameter04e62a22006-06-23 02:03:38 -0700192 * Get rid of all migration entries and replace them by
193 * references to the indicated page.
194 */
195static void remove_migration_ptes(struct page *old, struct page *new)
196{
197 if (PageAnon(new))
198 remove_anon_migration_ptes(old, new);
199 else
200 remove_file_migration_ptes(old, new);
201}
202
203/*
Christoph Lameter06972122006-06-23 02:03:35 -0700204 * Something used the pte of a page under migration. We need to
205 * get to the page and wait until migration is finished.
206 * When we return from this function the fault will be retried.
207 *
208 * This function is called from do_swap_page().
209 */
210void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
211 unsigned long address)
212{
213 pte_t *ptep, pte;
214 spinlock_t *ptl;
215 swp_entry_t entry;
216 struct page *page;
217
218 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
219 pte = *ptep;
220 if (!is_swap_pte(pte))
221 goto out;
222
223 entry = pte_to_swp_entry(pte);
224 if (!is_migration_entry(entry))
225 goto out;
226
227 page = migration_entry_to_page(entry);
228
Nick Piggine2867812008-07-25 19:45:30 -0700229 /*
230 * Once radix-tree replacement of page migration started, page_count
231 * *must* be zero. And, we don't want to call wait_on_page_locked()
232 * against a page without get_page().
233 * So, we use get_page_unless_zero(), here. Even failed, page fault
234 * will occur again.
235 */
236 if (!get_page_unless_zero(page))
237 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700238 pte_unmap_unlock(ptep, ptl);
239 wait_on_page_locked(page);
240 put_page(page);
241 return;
242out:
243 pte_unmap_unlock(ptep, ptl);
244}
245
Christoph Lameterb20a3502006-03-22 00:09:12 -0800246/*
Christoph Lameterc3fcf8a2006-06-23 02:03:32 -0700247 * Replace the page in the mapping.
Christoph Lameter5b5c7122006-06-23 02:03:29 -0700248 *
249 * The number of remaining references must be:
250 * 1 for anonymous pages without a mapping
251 * 2 for pages with a mapping
David Howells266cf652009-04-03 16:42:36 +0100252 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800253 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700254static int migrate_page_move_mapping(struct address_space *mapping,
255 struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800256{
Nick Piggine2867812008-07-25 19:45:30 -0700257 int expected_count;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800258 void **pslot;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800259
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700260 if (!mapping) {
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700261 /* Anonymous page without mapping */
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700262 if (page_count(page) != 1)
263 return -EAGAIN;
264 return 0;
265 }
266
Nick Piggin19fd6232008-07-25 19:45:32 -0700267 spin_lock_irq(&mapping->tree_lock);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800268
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800269 pslot = radix_tree_lookup_slot(&mapping->page_tree,
270 page_index(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800271
Johannes Weineredcf4742009-09-21 17:02:59 -0700272 expected_count = 2 + page_has_private(page);
Nick Piggine2867812008-07-25 19:45:30 -0700273 if (page_count(page) != expected_count ||
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800274 (struct page *)radix_tree_deref_slot(pslot) != page) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700275 spin_unlock_irq(&mapping->tree_lock);
Christoph Lametere23ca002006-04-10 22:52:57 -0700276 return -EAGAIN;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800277 }
278
Nick Piggine2867812008-07-25 19:45:30 -0700279 if (!page_freeze_refs(page, expected_count)) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700280 spin_unlock_irq(&mapping->tree_lock);
Nick Piggine2867812008-07-25 19:45:30 -0700281 return -EAGAIN;
282 }
283
Christoph Lameterb20a3502006-03-22 00:09:12 -0800284 /*
285 * Now we know that no one else is looking at the page.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800286 */
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800287 get_page(newpage); /* add cache reference */
Christoph Lameterb20a3502006-03-22 00:09:12 -0800288 if (PageSwapCache(page)) {
289 SetPageSwapCache(newpage);
290 set_page_private(newpage, page_private(page));
291 }
292
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800293 radix_tree_replace_slot(pslot, newpage);
294
Nick Piggine2867812008-07-25 19:45:30 -0700295 page_unfreeze_refs(page, expected_count);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800296 /*
297 * Drop cache reference from old page.
298 * We know this isn't the last reference.
299 */
Christoph Lameterb20a3502006-03-22 00:09:12 -0800300 __put_page(page);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800301
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700302 /*
303 * If moved to a different zone then also account
304 * the page for that zone. Other VM counters will be
305 * taken care of when we establish references to the
306 * new page and drop references to the old page.
307 *
308 * Note that anonymous pages are accounted for
309 * via NR_FILE_PAGES and NR_ANON_PAGES if they
310 * are mapped to swap space.
311 */
312 __dec_zone_page_state(page, NR_FILE_PAGES);
313 __inc_zone_page_state(newpage, NR_FILE_PAGES);
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700314 if (PageSwapBacked(page)) {
315 __dec_zone_page_state(page, NR_SHMEM);
316 __inc_zone_page_state(newpage, NR_SHMEM);
317 }
Nick Piggin19fd6232008-07-25 19:45:32 -0700318 spin_unlock_irq(&mapping->tree_lock);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800319
320 return 0;
321}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800322
323/*
324 * Copy the page to its new location
325 */
Christoph Lametere7340f72006-06-23 02:03:29 -0700326static void migrate_page_copy(struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800327{
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -0700328 int anon;
329
Christoph Lameterb20a3502006-03-22 00:09:12 -0800330 copy_highpage(newpage, page);
331
332 if (PageError(page))
333 SetPageError(newpage);
334 if (PageReferenced(page))
335 SetPageReferenced(newpage);
336 if (PageUptodate(page))
337 SetPageUptodate(newpage);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700338 if (TestClearPageActive(page)) {
339 VM_BUG_ON(PageUnevictable(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800340 SetPageActive(newpage);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700341 } else
342 unevictable_migrate_page(newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800343 if (PageChecked(page))
344 SetPageChecked(newpage);
345 if (PageMappedToDisk(page))
346 SetPageMappedToDisk(newpage);
347
348 if (PageDirty(page)) {
349 clear_page_dirty_for_io(page);
Nick Piggin3a902c52008-04-30 00:55:16 -0700350 /*
351 * Want to mark the page and the radix tree as dirty, and
352 * redo the accounting that clear_page_dirty_for_io undid,
353 * but we can't use set_page_dirty because that function
354 * is actually a signal that all of the page has become dirty.
355 * Wheras only part of our page may be dirty.
356 */
357 __set_page_dirty_nobuffers(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800358 }
359
Nick Pigginb291f002008-10-18 20:26:44 -0700360 mlock_migrate_page(newpage, page);
361
Christoph Lameterb20a3502006-03-22 00:09:12 -0800362 ClearPageSwapCache(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800363 ClearPagePrivate(page);
364 set_page_private(page, 0);
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -0700365 /* page->mapping contains a flag for PageAnon() */
366 anon = PageAnon(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800367 page->mapping = NULL;
368
369 /*
370 * If any waiters have accumulated on the new page then
371 * wake them up.
372 */
373 if (PageWriteback(newpage))
374 end_page_writeback(newpage);
375}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800376
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700377/************************************************************
378 * Migration functions
379 ***********************************************************/
380
381/* Always fail migration. Used for mappings that are not movable */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700382int fail_migrate_page(struct address_space *mapping,
383 struct page *newpage, struct page *page)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700384{
385 return -EIO;
386}
387EXPORT_SYMBOL(fail_migrate_page);
388
Christoph Lameterb20a3502006-03-22 00:09:12 -0800389/*
390 * Common logic to directly migrate a single page suitable for
David Howells266cf652009-04-03 16:42:36 +0100391 * pages that do not use PagePrivate/PagePrivate2.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800392 *
393 * Pages are locked upon entry and exit.
394 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700395int migrate_page(struct address_space *mapping,
396 struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800397{
398 int rc;
399
400 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
401
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700402 rc = migrate_page_move_mapping(mapping, newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800403
404 if (rc)
405 return rc;
406
407 migrate_page_copy(newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800408 return 0;
409}
410EXPORT_SYMBOL(migrate_page);
411
David Howells93614012006-09-30 20:45:40 +0200412#ifdef CONFIG_BLOCK
Christoph Lameterb20a3502006-03-22 00:09:12 -0800413/*
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700414 * Migration function for pages with buffers. This function can only be used
415 * if the underlying filesystem guarantees that no other references to "page"
416 * exist.
417 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700418int buffer_migrate_page(struct address_space *mapping,
419 struct page *newpage, struct page *page)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700420{
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700421 struct buffer_head *bh, *head;
422 int rc;
423
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700424 if (!page_has_buffers(page))
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700425 return migrate_page(mapping, newpage, page);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700426
427 head = page_buffers(page);
428
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700429 rc = migrate_page_move_mapping(mapping, newpage, page);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700430
431 if (rc)
432 return rc;
433
434 bh = head;
435 do {
436 get_bh(bh);
437 lock_buffer(bh);
438 bh = bh->b_this_page;
439
440 } while (bh != head);
441
442 ClearPagePrivate(page);
443 set_page_private(newpage, page_private(page));
444 set_page_private(page, 0);
445 put_page(page);
446 get_page(newpage);
447
448 bh = head;
449 do {
450 set_bh_page(bh, newpage, bh_offset(bh));
451 bh = bh->b_this_page;
452
453 } while (bh != head);
454
455 SetPagePrivate(newpage);
456
457 migrate_page_copy(newpage, page);
458
459 bh = head;
460 do {
461 unlock_buffer(bh);
462 put_bh(bh);
463 bh = bh->b_this_page;
464
465 } while (bh != head);
466
467 return 0;
468}
469EXPORT_SYMBOL(buffer_migrate_page);
David Howells93614012006-09-30 20:45:40 +0200470#endif
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700471
Christoph Lameter04e62a22006-06-23 02:03:38 -0700472/*
473 * Writeback a page to clean the dirty state
474 */
475static int writeout(struct address_space *mapping, struct page *page)
476{
477 struct writeback_control wbc = {
478 .sync_mode = WB_SYNC_NONE,
479 .nr_to_write = 1,
480 .range_start = 0,
481 .range_end = LLONG_MAX,
482 .nonblocking = 1,
483 .for_reclaim = 1
484 };
485 int rc;
486
487 if (!mapping->a_ops->writepage)
488 /* No write method for the address space */
489 return -EINVAL;
490
491 if (!clear_page_dirty_for_io(page))
492 /* Someone else already triggered a write */
493 return -EAGAIN;
494
495 /*
496 * A dirty page may imply that the underlying filesystem has
497 * the page on some queue. So the page must be clean for
498 * migration. Writeout may mean we loose the lock and the
499 * page state is no longer what we checked for earlier.
500 * At this point we know that the migration attempt cannot
501 * be successful.
502 */
503 remove_migration_ptes(page, page);
504
505 rc = mapping->a_ops->writepage(page, &wbc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700506
507 if (rc != AOP_WRITEPAGE_ACTIVATE)
508 /* unlocked. Relock */
509 lock_page(page);
510
Hugh Dickinsbda85502008-11-19 15:36:36 -0800511 return (rc < 0) ? -EIO : -EAGAIN;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700512}
513
514/*
515 * Default handling if a filesystem does not provide a migration function.
516 */
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700517static int fallback_migrate_page(struct address_space *mapping,
518 struct page *newpage, struct page *page)
519{
Christoph Lameter04e62a22006-06-23 02:03:38 -0700520 if (PageDirty(page))
521 return writeout(mapping, page);
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700522
523 /*
524 * Buffers may be managed in a filesystem specific way.
525 * We must have no buffers or drop them.
526 */
David Howells266cf652009-04-03 16:42:36 +0100527 if (page_has_private(page) &&
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700528 !try_to_release_page(page, GFP_KERNEL))
529 return -EAGAIN;
530
531 return migrate_page(mapping, newpage, page);
532}
533
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700534/*
Christoph Lametere24f0b82006-06-23 02:03:51 -0700535 * Move a page to a newly allocated page
536 * The page is locked and all ptes have been successfully removed.
537 *
538 * The new page will have replaced the old page if this function
539 * is successful.
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700540 *
541 * Return value:
542 * < 0 - error code
543 * == 0 - success
Christoph Lametere24f0b82006-06-23 02:03:51 -0700544 */
545static int move_to_new_page(struct page *newpage, struct page *page)
546{
547 struct address_space *mapping;
548 int rc;
549
550 /*
551 * Block others from accessing the page when we get around to
552 * establishing additional references. We are the only one
553 * holding a reference to the new page at this point.
554 */
Nick Piggin529ae9a2008-08-02 12:01:03 +0200555 if (!trylock_page(newpage))
Christoph Lametere24f0b82006-06-23 02:03:51 -0700556 BUG();
557
558 /* Prepare mapping for the new page.*/
559 newpage->index = page->index;
560 newpage->mapping = page->mapping;
Rik van Rielb2e18532008-10-18 20:26:30 -0700561 if (PageSwapBacked(page))
562 SetPageSwapBacked(newpage);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700563
564 mapping = page_mapping(page);
565 if (!mapping)
566 rc = migrate_page(mapping, newpage, page);
567 else if (mapping->a_ops->migratepage)
568 /*
569 * Most pages have a mapping and most filesystems
570 * should provide a migration function. Anonymous
571 * pages are part of swap space which also has its
572 * own migration function. This is the most common
573 * path for page migration.
574 */
575 rc = mapping->a_ops->migratepage(mapping,
576 newpage, page);
577 else
578 rc = fallback_migrate_page(mapping, newpage, page);
579
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800580 if (!rc) {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700581 remove_migration_ptes(page, newpage);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800582 } else
Christoph Lametere24f0b82006-06-23 02:03:51 -0700583 newpage->mapping = NULL;
584
585 unlock_page(newpage);
586
587 return rc;
588}
589
590/*
591 * Obtain the lock on page, remove all ptes and migrate the page
592 * to the newly allocated page in newpage.
593 */
Christoph Lameter95a402c2006-06-23 02:03:53 -0700594static int unmap_and_move(new_page_t get_new_page, unsigned long private,
595 struct page *page, int force)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700596{
597 int rc = 0;
Christoph Lameter742755a2006-06-23 02:03:55 -0700598 int *result = NULL;
599 struct page *newpage = get_new_page(page, private, &result);
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700600 int rcu_locked = 0;
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800601 int charge = 0;
KAMEZAWA Hiroyukie00e4312009-11-11 14:26:26 -0800602 struct mem_cgroup *mem = NULL;
Christoph Lameter95a402c2006-06-23 02:03:53 -0700603
604 if (!newpage)
605 return -ENOMEM;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700606
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700607 if (page_count(page) == 1) {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700608 /* page was freed from under us. So we are done. */
Christoph Lameter95a402c2006-06-23 02:03:53 -0700609 goto move_newpage;
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700610 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700611
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700612 /* prepare cgroup just returns 0 or -ENOMEM */
Christoph Lametere24f0b82006-06-23 02:03:51 -0700613 rc = -EAGAIN;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800614
Nick Piggin529ae9a2008-08-02 12:01:03 +0200615 if (!trylock_page(page)) {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700616 if (!force)
Christoph Lameter95a402c2006-06-23 02:03:53 -0700617 goto move_newpage;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700618 lock_page(page);
619 }
620
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800621 /* charge against new page */
622 charge = mem_cgroup_prepare_migration(page, &mem);
623 if (charge == -ENOMEM) {
624 rc = -ENOMEM;
625 goto unlock;
626 }
627 BUG_ON(charge);
628
Christoph Lametere24f0b82006-06-23 02:03:51 -0700629 if (PageWriteback(page)) {
630 if (!force)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800631 goto uncharge;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700632 wait_on_page_writeback(page);
633 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700634 /*
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700635 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
636 * we cannot notice that anon_vma is freed while we migrates a page.
637 * This rcu_read_lock() delays freeing anon_vma pointer until the end
638 * of migration. File cache pages are no problem because of page_lock()
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700639 * File Caches may use write_page() or lock_page() in migration, then,
640 * just care Anon page here.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700641 */
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700642 if (PageAnon(page)) {
643 rcu_read_lock();
644 rcu_locked = 1;
645 }
Shaohua Li62e1c552008-02-04 22:29:33 -0800646
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700647 /*
Shaohua Li62e1c552008-02-04 22:29:33 -0800648 * Corner case handling:
649 * 1. When a new swap-cache page is read into, it is added to the LRU
650 * and treated as swapcache but it has no rmap yet.
651 * Calling try_to_unmap() against a page->mapping==NULL page will
652 * trigger a BUG. So handle it here.
653 * 2. An orphaned page (see truncate_complete_page) might have
654 * fs-private metadata. The page can be picked up due to memory
655 * offlining. Everywhere else except page reclaim, the page is
656 * invisible to the vm, so the page can not be migrated. So try to
657 * free the metadata, so the page can be freed.
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700658 */
Shaohua Li62e1c552008-02-04 22:29:33 -0800659 if (!page->mapping) {
David Howells266cf652009-04-03 16:42:36 +0100660 if (!PageAnon(page) && page_has_private(page)) {
Shaohua Li62e1c552008-02-04 22:29:33 -0800661 /*
662 * Go direct to try_to_free_buffers() here because
663 * a) that's what try_to_release_page() would do anyway
664 * b) we may be under rcu_read_lock() here, so we can't
665 * use GFP_KERNEL which is what try_to_release_page()
666 * needs to be effective.
667 */
668 try_to_free_buffers(page);
Shaohua Liabfc3482009-09-21 17:01:19 -0700669 goto rcu_unlock;
Shaohua Li62e1c552008-02-04 22:29:33 -0800670 }
Shaohua Liabfc3482009-09-21 17:01:19 -0700671 goto skip_unmap;
Shaohua Li62e1c552008-02-04 22:29:33 -0800672 }
673
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700674 /* Establish migration ptes or remove ptes */
Andi Kleen14fa31b2009-09-16 11:50:10 +0200675 try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700676
Shaohua Liabfc3482009-09-21 17:01:19 -0700677skip_unmap:
Christoph Lametere6a15302006-06-25 05:46:49 -0700678 if (!page_mapped(page))
679 rc = move_to_new_page(newpage, page);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700680
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700681 if (rc)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700682 remove_migration_ptes(page, page);
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700683rcu_unlock:
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700684 if (rcu_locked)
685 rcu_read_unlock();
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800686uncharge:
687 if (!charge)
688 mem_cgroup_end_migration(mem, page, newpage);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700689unlock:
690 unlock_page(page);
Christoph Lameter95a402c2006-06-23 02:03:53 -0700691
Christoph Lametere24f0b82006-06-23 02:03:51 -0700692 if (rc != -EAGAIN) {
Christoph Lameteraaa994b2006-06-23 02:03:52 -0700693 /*
694 * A page that has been migrated has all references
695 * removed and will be freed. A page that has not been
696 * migrated will have kepts its references and be
697 * restored.
698 */
699 list_del(&page->lru);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -0700700 dec_zone_page_state(page, NR_ISOLATED_ANON +
Johannes Weiner6c0b1352009-09-21 17:02:59 -0700701 page_is_file_cache(page));
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700702 putback_lru_page(page);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700703 }
Christoph Lameter95a402c2006-06-23 02:03:53 -0700704
705move_newpage:
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700706
Christoph Lameter95a402c2006-06-23 02:03:53 -0700707 /*
708 * Move the new page to the LRU. If migration was not successful
709 * then this will free the page.
710 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700711 putback_lru_page(newpage);
712
Christoph Lameter742755a2006-06-23 02:03:55 -0700713 if (result) {
714 if (rc)
715 *result = rc;
716 else
717 *result = page_to_nid(newpage);
718 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700719 return rc;
720}
721
722/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800723 * migrate_pages
724 *
Christoph Lameter95a402c2006-06-23 02:03:53 -0700725 * The function takes one list of pages to migrate and a function
726 * that determines from the page to be migrated and the private data
727 * the target of the move and allocates the page.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800728 *
729 * The function returns after 10 attempts or if no pages
730 * are movable anymore because to has become empty
Christoph Lameteraaa994b2006-06-23 02:03:52 -0700731 * or no retryable pages exist anymore. All pages will be
Gabriel Craciunescue9534b32007-10-20 02:13:26 +0200732 * returned to the LRU or freed.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800733 *
Christoph Lameter95a402c2006-06-23 02:03:53 -0700734 * Return: Number of pages not migrated or error code.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800735 */
Christoph Lameter95a402c2006-06-23 02:03:53 -0700736int migrate_pages(struct list_head *from,
737 new_page_t get_new_page, unsigned long private)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800738{
Christoph Lametere24f0b82006-06-23 02:03:51 -0700739 int retry = 1;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800740 int nr_failed = 0;
741 int pass = 0;
742 struct page *page;
743 struct page *page2;
744 int swapwrite = current->flags & PF_SWAPWRITE;
745 int rc;
746
747 if (!swapwrite)
748 current->flags |= PF_SWAPWRITE;
749
Christoph Lametere24f0b82006-06-23 02:03:51 -0700750 for(pass = 0; pass < 10 && retry; pass++) {
751 retry = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800752
Christoph Lametere24f0b82006-06-23 02:03:51 -0700753 list_for_each_entry_safe(page, page2, from, lru) {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700754 cond_resched();
Christoph Lameterb20a3502006-03-22 00:09:12 -0800755
Christoph Lameter95a402c2006-06-23 02:03:53 -0700756 rc = unmap_and_move(get_new_page, private,
757 page, pass > 2);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800758
Christoph Lametere24f0b82006-06-23 02:03:51 -0700759 switch(rc) {
Christoph Lameter95a402c2006-06-23 02:03:53 -0700760 case -ENOMEM:
761 goto out;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700762 case -EAGAIN:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700763 retry++;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700764 break;
765 case 0:
Christoph Lametere24f0b82006-06-23 02:03:51 -0700766 break;
767 default:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700768 /* Permanent failure */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700769 nr_failed++;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700770 break;
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700771 }
Christoph Lameterb20a3502006-03-22 00:09:12 -0800772 }
773 }
Christoph Lameter95a402c2006-06-23 02:03:53 -0700774 rc = 0;
775out:
Christoph Lameterb20a3502006-03-22 00:09:12 -0800776 if (!swapwrite)
777 current->flags &= ~PF_SWAPWRITE;
778
Christoph Lameteraaa994b2006-06-23 02:03:52 -0700779 putback_lru_pages(from);
Christoph Lameter95a402c2006-06-23 02:03:53 -0700780
781 if (rc)
782 return rc;
783
Christoph Lameterb20a3502006-03-22 00:09:12 -0800784 return nr_failed + retry;
785}
786
Christoph Lameter742755a2006-06-23 02:03:55 -0700787#ifdef CONFIG_NUMA
788/*
789 * Move a list of individual pages
790 */
791struct page_to_node {
792 unsigned long addr;
793 struct page *page;
794 int node;
795 int status;
796};
797
798static struct page *new_page_node(struct page *p, unsigned long private,
799 int **result)
800{
801 struct page_to_node *pm = (struct page_to_node *)private;
802
803 while (pm->node != MAX_NUMNODES && pm->page != p)
804 pm++;
805
806 if (pm->node == MAX_NUMNODES)
807 return NULL;
808
809 *result = &pm->status;
810
Mel Gorman6484eb32009-06-16 15:31:54 -0700811 return alloc_pages_exact_node(pm->node,
Mel Gorman769848c2007-07-17 04:03:05 -0700812 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
Christoph Lameter742755a2006-06-23 02:03:55 -0700813}
814
815/*
816 * Move a set of pages as indicated in the pm array. The addr
817 * field must be set to the virtual address of the page to be moved
818 * and the node number must contain a valid target node.
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700819 * The pm array ends with node = MAX_NUMNODES.
Christoph Lameter742755a2006-06-23 02:03:55 -0700820 */
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700821static int do_move_page_to_node_array(struct mm_struct *mm,
822 struct page_to_node *pm,
823 int migrate_all)
Christoph Lameter742755a2006-06-23 02:03:55 -0700824{
825 int err;
826 struct page_to_node *pp;
827 LIST_HEAD(pagelist);
828
829 down_read(&mm->mmap_sem);
830
831 /*
832 * Build a list of pages to migrate
833 */
Christoph Lameter742755a2006-06-23 02:03:55 -0700834 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
835 struct vm_area_struct *vma;
836 struct page *page;
837
Christoph Lameter742755a2006-06-23 02:03:55 -0700838 err = -EFAULT;
839 vma = find_vma(mm, pp->addr);
Christoph Lameter0dc952d2007-03-05 00:30:33 -0800840 if (!vma || !vma_migratable(vma))
Christoph Lameter742755a2006-06-23 02:03:55 -0700841 goto set_status;
842
843 page = follow_page(vma, pp->addr, FOLL_GET);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -0700844
845 err = PTR_ERR(page);
846 if (IS_ERR(page))
847 goto set_status;
848
Christoph Lameter742755a2006-06-23 02:03:55 -0700849 err = -ENOENT;
850 if (!page)
851 goto set_status;
852
853 if (PageReserved(page)) /* Check for zero page */
854 goto put_and_set;
855
856 pp->page = page;
857 err = page_to_nid(page);
858
859 if (err == pp->node)
860 /*
861 * Node already in the right place
862 */
863 goto put_and_set;
864
865 err = -EACCES;
866 if (page_mapcount(page) > 1 &&
867 !migrate_all)
868 goto put_and_set;
869
Nick Piggin62695a82008-10-18 20:26:09 -0700870 err = isolate_lru_page(page);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -0800871 if (!err) {
Nick Piggin62695a82008-10-18 20:26:09 -0700872 list_add_tail(&page->lru, &pagelist);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -0800873 inc_zone_page_state(page, NR_ISOLATED_ANON +
874 page_is_file_cache(page));
875 }
Christoph Lameter742755a2006-06-23 02:03:55 -0700876put_and_set:
877 /*
878 * Either remove the duplicate refcount from
879 * isolate_lru_page() or drop the page ref if it was
880 * not isolated.
881 */
882 put_page(page);
883set_status:
884 pp->status = err;
885 }
886
Brice Gogline78bbfa2008-10-18 20:27:15 -0700887 err = 0;
Christoph Lameter742755a2006-06-23 02:03:55 -0700888 if (!list_empty(&pagelist))
889 err = migrate_pages(&pagelist, new_page_node,
890 (unsigned long)pm);
Christoph Lameter742755a2006-06-23 02:03:55 -0700891
892 up_read(&mm->mmap_sem);
893 return err;
894}
895
896/*
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700897 * Migrate an array of page address onto an array of nodes and fill
898 * the corresponding array of status.
899 */
900static int do_pages_move(struct mm_struct *mm, struct task_struct *task,
901 unsigned long nr_pages,
902 const void __user * __user *pages,
903 const int __user *nodes,
904 int __user *status, int flags)
905{
Brice Goglin3140a222009-01-06 14:38:57 -0800906 struct page_to_node *pm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700907 nodemask_t task_nodes;
Brice Goglin3140a222009-01-06 14:38:57 -0800908 unsigned long chunk_nr_pages;
909 unsigned long chunk_start;
910 int err;
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700911
912 task_nodes = cpuset_mems_allowed(task);
913
Brice Goglin3140a222009-01-06 14:38:57 -0800914 err = -ENOMEM;
915 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
916 if (!pm)
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700917 goto out;
Brice Goglin35282a22009-06-16 15:32:43 -0700918
919 migrate_prep();
920
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700921 /*
Brice Goglin3140a222009-01-06 14:38:57 -0800922 * Store a chunk of page_to_node array in a page,
923 * but keep the last one as a marker
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700924 */
Brice Goglin3140a222009-01-06 14:38:57 -0800925 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700926
Brice Goglin3140a222009-01-06 14:38:57 -0800927 for (chunk_start = 0;
928 chunk_start < nr_pages;
929 chunk_start += chunk_nr_pages) {
930 int j;
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700931
Brice Goglin3140a222009-01-06 14:38:57 -0800932 if (chunk_start + chunk_nr_pages > nr_pages)
933 chunk_nr_pages = nr_pages - chunk_start;
934
935 /* fill the chunk pm with addrs and nodes from user-space */
936 for (j = 0; j < chunk_nr_pages; j++) {
937 const void __user *p;
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700938 int node;
939
Brice Goglin3140a222009-01-06 14:38:57 -0800940 err = -EFAULT;
941 if (get_user(p, pages + j + chunk_start))
942 goto out_pm;
943 pm[j].addr = (unsigned long) p;
944
945 if (get_user(node, nodes + j + chunk_start))
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700946 goto out_pm;
947
948 err = -ENODEV;
949 if (!node_state(node, N_HIGH_MEMORY))
950 goto out_pm;
951
952 err = -EACCES;
953 if (!node_isset(node, task_nodes))
954 goto out_pm;
955
Brice Goglin3140a222009-01-06 14:38:57 -0800956 pm[j].node = node;
957 }
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700958
Brice Goglin3140a222009-01-06 14:38:57 -0800959 /* End marker for this chunk */
960 pm[chunk_nr_pages].node = MAX_NUMNODES;
961
962 /* Migrate this chunk */
963 err = do_move_page_to_node_array(mm, pm,
964 flags & MPOL_MF_MOVE_ALL);
965 if (err < 0)
966 goto out_pm;
967
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700968 /* Return status information */
Brice Goglin3140a222009-01-06 14:38:57 -0800969 for (j = 0; j < chunk_nr_pages; j++)
970 if (put_user(pm[j].status, status + j + chunk_start)) {
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700971 err = -EFAULT;
Brice Goglin3140a222009-01-06 14:38:57 -0800972 goto out_pm;
973 }
974 }
975 err = 0;
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700976
977out_pm:
Brice Goglin3140a222009-01-06 14:38:57 -0800978 free_page((unsigned long)pm);
Brice Goglin5e9a0f02008-10-18 20:27:17 -0700979out:
980 return err;
981}
982
983/*
Brice Goglin2f007e72008-10-18 20:27:16 -0700984 * Determine the nodes of an array of pages and store it in an array of status.
Christoph Lameter742755a2006-06-23 02:03:55 -0700985 */
Brice Goglin80bba122008-12-09 13:14:23 -0800986static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
987 const void __user **pages, int *status)
Christoph Lameter742755a2006-06-23 02:03:55 -0700988{
Brice Goglin2f007e72008-10-18 20:27:16 -0700989 unsigned long i;
Brice Goglin2f007e72008-10-18 20:27:16 -0700990
Christoph Lameter742755a2006-06-23 02:03:55 -0700991 down_read(&mm->mmap_sem);
992
Brice Goglin2f007e72008-10-18 20:27:16 -0700993 for (i = 0; i < nr_pages; i++) {
Brice Goglin80bba122008-12-09 13:14:23 -0800994 unsigned long addr = (unsigned long)(*pages);
Christoph Lameter742755a2006-06-23 02:03:55 -0700995 struct vm_area_struct *vma;
996 struct page *page;
KOSAKI Motohiroc095adb2008-12-16 16:06:43 +0900997 int err = -EFAULT;
Brice Goglin2f007e72008-10-18 20:27:16 -0700998
999 vma = find_vma(mm, addr);
Christoph Lameter742755a2006-06-23 02:03:55 -07001000 if (!vma)
1001 goto set_status;
1002
Brice Goglin2f007e72008-10-18 20:27:16 -07001003 page = follow_page(vma, addr, 0);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001004
1005 err = PTR_ERR(page);
1006 if (IS_ERR(page))
1007 goto set_status;
1008
Christoph Lameter742755a2006-06-23 02:03:55 -07001009 err = -ENOENT;
1010 /* Use PageReserved to check for zero page */
1011 if (!page || PageReserved(page))
1012 goto set_status;
1013
1014 err = page_to_nid(page);
1015set_status:
Brice Goglin80bba122008-12-09 13:14:23 -08001016 *status = err;
1017
1018 pages++;
1019 status++;
1020 }
1021
1022 up_read(&mm->mmap_sem);
1023}
1024
1025/*
1026 * Determine the nodes of a user array of pages and store it in
1027 * a user array of status.
1028 */
1029static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1030 const void __user * __user *pages,
1031 int __user *status)
1032{
1033#define DO_PAGES_STAT_CHUNK_NR 16
1034 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1035 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
1036 unsigned long i, chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1037 int err;
1038
1039 for (i = 0; i < nr_pages; i += chunk_nr) {
H. Peter Anvinb92558502009-12-08 14:01:32 -08001040 if (chunk_nr > nr_pages - i)
Brice Goglin80bba122008-12-09 13:14:23 -08001041 chunk_nr = nr_pages - i;
1042
1043 err = copy_from_user(chunk_pages, &pages[i],
1044 chunk_nr * sizeof(*chunk_pages));
1045 if (err) {
1046 err = -EFAULT;
1047 goto out;
1048 }
1049
1050 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1051
1052 err = copy_to_user(&status[i], chunk_status,
1053 chunk_nr * sizeof(*chunk_status));
1054 if (err) {
1055 err = -EFAULT;
1056 goto out;
1057 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001058 }
Brice Goglin2f007e72008-10-18 20:27:16 -07001059 err = 0;
Christoph Lameter742755a2006-06-23 02:03:55 -07001060
Brice Goglin2f007e72008-10-18 20:27:16 -07001061out:
Brice Goglin2f007e72008-10-18 20:27:16 -07001062 return err;
Christoph Lameter742755a2006-06-23 02:03:55 -07001063}
1064
1065/*
1066 * Move a list of pages in the address space of the currently executing
1067 * process.
1068 */
Heiko Carstens938bb9f2009-01-14 14:14:30 +01001069SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1070 const void __user * __user *, pages,
1071 const int __user *, nodes,
1072 int __user *, status, int, flags)
Christoph Lameter742755a2006-06-23 02:03:55 -07001073{
David Howellsc69e8d92008-11-14 10:39:19 +11001074 const struct cred *cred = current_cred(), *tcred;
Christoph Lameter742755a2006-06-23 02:03:55 -07001075 struct task_struct *task;
Christoph Lameter742755a2006-06-23 02:03:55 -07001076 struct mm_struct *mm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001077 int err;
Christoph Lameter742755a2006-06-23 02:03:55 -07001078
1079 /* Check flags */
1080 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1081 return -EINVAL;
1082
1083 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1084 return -EPERM;
1085
1086 /* Find the mm_struct */
1087 read_lock(&tasklist_lock);
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07001088 task = pid ? find_task_by_vpid(pid) : current;
Christoph Lameter742755a2006-06-23 02:03:55 -07001089 if (!task) {
1090 read_unlock(&tasklist_lock);
1091 return -ESRCH;
1092 }
1093 mm = get_task_mm(task);
1094 read_unlock(&tasklist_lock);
1095
1096 if (!mm)
1097 return -EINVAL;
1098
1099 /*
1100 * Check if this process has the right to modify the specified
1101 * process. The right exists if the process has administrative
1102 * capabilities, superuser privileges or the same
1103 * userid as the target process.
1104 */
David Howellsc69e8d92008-11-14 10:39:19 +11001105 rcu_read_lock();
1106 tcred = __task_cred(task);
David Howellsb6dff3e2008-11-14 10:39:16 +11001107 if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
1108 cred->uid != tcred->suid && cred->uid != tcred->uid &&
Christoph Lameter742755a2006-06-23 02:03:55 -07001109 !capable(CAP_SYS_NICE)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001110 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001111 err = -EPERM;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001112 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001113 }
David Howellsc69e8d92008-11-14 10:39:19 +11001114 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001115
David Quigley86c3a762006-06-23 02:04:02 -07001116 err = security_task_movememory(task);
1117 if (err)
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001118 goto out;
David Quigley86c3a762006-06-23 02:04:02 -07001119
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001120 if (nodes) {
1121 err = do_pages_move(mm, task, nr_pages, pages, nodes, status,
1122 flags);
1123 } else {
Brice Goglin2f007e72008-10-18 20:27:16 -07001124 err = do_pages_stat(mm, nr_pages, pages, status);
Brice Goglin2f007e72008-10-18 20:27:16 -07001125 }
David Quigley86c3a762006-06-23 02:04:02 -07001126
Christoph Lameter742755a2006-06-23 02:03:55 -07001127out:
Christoph Lameter742755a2006-06-23 02:03:55 -07001128 mmput(mm);
1129 return err;
1130}
Christoph Lameter742755a2006-06-23 02:03:55 -07001131
Christoph Lameter7b2259b2006-06-25 05:46:48 -07001132/*
1133 * Call migration functions in the vma_ops that may prepare
1134 * memory in a vm for migration. migration functions may perform
1135 * the migration for vmas that do not have an underlying page struct.
1136 */
1137int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1138 const nodemask_t *from, unsigned long flags)
1139{
1140 struct vm_area_struct *vma;
1141 int err = 0;
1142
Daisuke Nishimura1001c9f2009-02-11 13:04:18 -08001143 for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
Christoph Lameter7b2259b2006-06-25 05:46:48 -07001144 if (vma->vm_ops && vma->vm_ops->migrate) {
1145 err = vma->vm_ops->migrate(vma, to, from, flags);
1146 if (err)
1147 break;
1148 }
1149 }
1150 return err;
1151}
Gerald Schaefer83d16742008-07-23 21:28:22 -07001152#endif