]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - mm/migrate.c
ksm: mem cgroup charge swapin copy
[thirdparty/kernel/stable.git] / mm / migrate.c
CommitLineData
b20a3503
CL
1/*
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>
cde53535 12 * Christoph Lameter
b20a3503
CL
13 */
14
15#include <linux/migrate.h>
16#include <linux/module.h>
17#include <linux/swap.h>
0697212a 18#include <linux/swapops.h>
b20a3503 19#include <linux/pagemap.h>
e23ca00b 20#include <linux/buffer_head.h>
b20a3503 21#include <linux/mm_inline.h>
b488893a 22#include <linux/nsproxy.h>
b20a3503
CL
23#include <linux/pagevec.h>
24#include <linux/rmap.h>
25#include <linux/topology.h>
26#include <linux/cpu.h>
27#include <linux/cpuset.h>
04e62a29 28#include <linux/writeback.h>
742755a1
CL
29#include <linux/mempolicy.h>
30#include <linux/vmalloc.h>
86c3a764 31#include <linux/security.h>
8a9f3ccd 32#include <linux/memcontrol.h>
4f5ca265 33#include <linux/syscalls.h>
b20a3503
CL
34
35#include "internal.h"
36
b20a3503
CL
37#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
38
b20a3503 39/*
742755a1
CL
40 * migrate_prep() needs to be called before we start compiling a list of pages
41 * to be migrated using isolate_lru_page().
b20a3503
CL
42 */
43int migrate_prep(void)
44{
b20a3503
CL
45 /*
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
b20a3503 56/*
894bc310
LS
57 * Add isolated pages on the list back to the LRU under page lock
58 * to avoid leaking evictable pages back onto unevictable list.
b20a3503
CL
59 *
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) {
e24f0b8f 69 list_del(&page->lru);
a731286d 70 dec_zone_page_state(page, NR_ISOLATED_ANON +
6c0b1351 71 page_is_file_cache(page));
894bc310 72 putback_lru_page(page);
b20a3503
CL
73 count++;
74 }
75 return count;
76}
77
0697212a
CL
78/*
79 * Restore a potential migration pte to a working pte entry
80 */
04e62a29 81static void remove_migration_pte(struct vm_area_struct *vma,
0697212a
CL
82 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;
04e62a29
CL
91 unsigned long addr = page_address_in_vma(new, vma);
92
93 if (addr == -EFAULT)
94 return;
0697212a
CL
95
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
0697212a
CL
126 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);
97ee0524 130 flush_cache_page(vma, addr, pte_pfn(pte));
0697212a 131 set_pte_at(mm, addr, ptep, pte);
04e62a29
CL
132
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);
04e62a29 140
0697212a
CL
141out:
142 pte_unmap_unlock(ptep, ptl);
143}
144
145/*
04e62a29
CL
146 * 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;
abfc3488 152 struct address_space *mapping = new->mapping;
04e62a29
CL
153 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/*
0697212a
CL
168 * Must hold mmap_sem lock on at least one of the vmas containing
169 * the page so that the anon_vma cannot vanish.
170 */
04e62a29 171static void remove_anon_migration_ptes(struct page *old, struct page *new)
0697212a
CL
172{
173 struct anon_vma *anon_vma;
174 struct vm_area_struct *vma;
0697212a
CL
175
176 /*
177 * We hold the mmap_sem lock. So no need to call page_lock_anon_vma.
178 */
3ca7b3c5
HD
179 anon_vma = page_anon_vma(new);
180 if (!anon_vma)
181 return;
182
0697212a
CL
183 spin_lock(&anon_vma->lock);
184
185 list_for_each_entry(vma, &anon_vma->head, anon_vma_node)
04e62a29 186 remove_migration_pte(vma, old, new);
0697212a
CL
187
188 spin_unlock(&anon_vma->lock);
189}
190
04e62a29
CL
191/*
192 * 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
0697212a
CL
203/*
204 * 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
e286781d
NP
229 /*
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;
0697212a
CL
238 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
b20a3503 246/*
c3fcf8a5 247 * Replace the page in the mapping.
5b5c7120
CL
248 *
249 * The number of remaining references must be:
250 * 1 for anonymous pages without a mapping
251 * 2 for pages with a mapping
266cf658 252 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
b20a3503 253 */
2d1db3b1
CL
254static int migrate_page_move_mapping(struct address_space *mapping,
255 struct page *newpage, struct page *page)
b20a3503 256{
e286781d 257 int expected_count;
7cf9c2c7 258 void **pslot;
b20a3503 259
6c5240ae 260 if (!mapping) {
0e8c7d0f 261 /* Anonymous page without mapping */
6c5240ae
CL
262 if (page_count(page) != 1)
263 return -EAGAIN;
264 return 0;
265 }
266
19fd6231 267 spin_lock_irq(&mapping->tree_lock);
b20a3503 268
7cf9c2c7
NP
269 pslot = radix_tree_lookup_slot(&mapping->page_tree,
270 page_index(page));
b20a3503 271
edcf4748 272 expected_count = 2 + page_has_private(page);
e286781d 273 if (page_count(page) != expected_count ||
7cf9c2c7 274 (struct page *)radix_tree_deref_slot(pslot) != page) {
19fd6231 275 spin_unlock_irq(&mapping->tree_lock);
e23ca00b 276 return -EAGAIN;
b20a3503
CL
277 }
278
e286781d 279 if (!page_freeze_refs(page, expected_count)) {
19fd6231 280 spin_unlock_irq(&mapping->tree_lock);
e286781d
NP
281 return -EAGAIN;
282 }
283
b20a3503
CL
284 /*
285 * Now we know that no one else is looking at the page.
b20a3503 286 */
7cf9c2c7 287 get_page(newpage); /* add cache reference */
b20a3503
CL
288 if (PageSwapCache(page)) {
289 SetPageSwapCache(newpage);
290 set_page_private(newpage, page_private(page));
291 }
292
7cf9c2c7
NP
293 radix_tree_replace_slot(pslot, newpage);
294
e286781d 295 page_unfreeze_refs(page, expected_count);
7cf9c2c7
NP
296 /*
297 * Drop cache reference from old page.
298 * We know this isn't the last reference.
299 */
b20a3503 300 __put_page(page);
7cf9c2c7 301
0e8c7d0f
CL
302 /*
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);
4b02108a
KM
314 if (PageSwapBacked(page)) {
315 __dec_zone_page_state(page, NR_SHMEM);
316 __inc_zone_page_state(newpage, NR_SHMEM);
317 }
19fd6231 318 spin_unlock_irq(&mapping->tree_lock);
b20a3503
CL
319
320 return 0;
321}
b20a3503
CL
322
323/*
324 * Copy the page to its new location
325 */
e7340f73 326static void migrate_page_copy(struct page *newpage, struct page *page)
b20a3503 327{
b7abea96
KH
328 int anon;
329
b20a3503
CL
330 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);
894bc310
LS
338 if (TestClearPageActive(page)) {
339 VM_BUG_ON(PageUnevictable(page));
b20a3503 340 SetPageActive(newpage);
894bc310
LS
341 } else
342 unevictable_migrate_page(newpage, page);
b20a3503
CL
343 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);
3a902c5f
NP
350 /*
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);
b20a3503
CL
358 }
359
b291f000
NP
360 mlock_migrate_page(newpage, page);
361
b20a3503 362 ClearPageSwapCache(page);
b20a3503
CL
363 ClearPagePrivate(page);
364 set_page_private(page, 0);
b7abea96
KH
365 /* page->mapping contains a flag for PageAnon() */
366 anon = PageAnon(page);
b20a3503
CL
367 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}
b20a3503 376
1d8b85cc
CL
377/************************************************************
378 * Migration functions
379 ***********************************************************/
380
381/* Always fail migration. Used for mappings that are not movable */
2d1db3b1
CL
382int fail_migrate_page(struct address_space *mapping,
383 struct page *newpage, struct page *page)
1d8b85cc
CL
384{
385 return -EIO;
386}
387EXPORT_SYMBOL(fail_migrate_page);
388
b20a3503
CL
389/*
390 * Common logic to directly migrate a single page suitable for
266cf658 391 * pages that do not use PagePrivate/PagePrivate2.
b20a3503
CL
392 *
393 * Pages are locked upon entry and exit.
394 */
2d1db3b1
CL
395int migrate_page(struct address_space *mapping,
396 struct page *newpage, struct page *page)
b20a3503
CL
397{
398 int rc;
399
400 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
401
2d1db3b1 402 rc = migrate_page_move_mapping(mapping, newpage, page);
b20a3503
CL
403
404 if (rc)
405 return rc;
406
407 migrate_page_copy(newpage, page);
b20a3503
CL
408 return 0;
409}
410EXPORT_SYMBOL(migrate_page);
411
9361401e 412#ifdef CONFIG_BLOCK
1d8b85cc
CL
413/*
414 * 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 */
2d1db3b1
CL
418int buffer_migrate_page(struct address_space *mapping,
419 struct page *newpage, struct page *page)
1d8b85cc 420{
1d8b85cc
CL
421 struct buffer_head *bh, *head;
422 int rc;
423
1d8b85cc 424 if (!page_has_buffers(page))
2d1db3b1 425 return migrate_page(mapping, newpage, page);
1d8b85cc
CL
426
427 head = page_buffers(page);
428
2d1db3b1 429 rc = migrate_page_move_mapping(mapping, newpage, page);
1d8b85cc
CL
430
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);
9361401e 470#endif
1d8b85cc 471
04e62a29
CL
472/*
473 * Writeback a page to clean the dirty state
474 */
475static int writeout(struct address_space *mapping, struct page *page)
8351a6e4 476{
04e62a29
CL
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
8351a6e4 495 /*
04e62a29
CL
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.
8351a6e4 502 */
04e62a29 503 remove_migration_ptes(page, page);
8351a6e4 504
04e62a29 505 rc = mapping->a_ops->writepage(page, &wbc);
8351a6e4 506
04e62a29
CL
507 if (rc != AOP_WRITEPAGE_ACTIVATE)
508 /* unlocked. Relock */
509 lock_page(page);
510
bda8550d 511 return (rc < 0) ? -EIO : -EAGAIN;
04e62a29
CL
512}
513
514/*
515 * Default handling if a filesystem does not provide a migration function.
516 */
517static int fallback_migrate_page(struct address_space *mapping,
518 struct page *newpage, struct page *page)
519{
520 if (PageDirty(page))
521 return writeout(mapping, page);
8351a6e4
CL
522
523 /*
524 * Buffers may be managed in a filesystem specific way.
525 * We must have no buffers or drop them.
526 */
266cf658 527 if (page_has_private(page) &&
8351a6e4
CL
528 !try_to_release_page(page, GFP_KERNEL))
529 return -EAGAIN;
530
531 return migrate_page(mapping, newpage, page);
532}
533
e24f0b8f
CL
534/*
535 * 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.
894bc310
LS
540 *
541 * Return value:
542 * < 0 - error code
543 * == 0 - success
e24f0b8f
CL
544 */
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 */
529ae9aa 555 if (!trylock_page(newpage))
e24f0b8f
CL
556 BUG();
557
558 /* Prepare mapping for the new page.*/
559 newpage->index = page->index;
560 newpage->mapping = page->mapping;
b2e18538
RR
561 if (PageSwapBacked(page))
562 SetPageSwapBacked(newpage);
e24f0b8f
CL
563
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
ae41be37 580 if (!rc) {
e24f0b8f 581 remove_migration_ptes(page, newpage);
ae41be37 582 } else
e24f0b8f
CL
583 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 */
95a402c3
CL
594static int unmap_and_move(new_page_t get_new_page, unsigned long private,
595 struct page *page, int force)
e24f0b8f
CL
596{
597 int rc = 0;
742755a1
CL
598 int *result = NULL;
599 struct page *newpage = get_new_page(page, private, &result);
989f89c5 600 int rcu_locked = 0;
ae41be37 601 int charge = 0;
e00e4316 602 struct mem_cgroup *mem = NULL;
95a402c3
CL
603
604 if (!newpage)
605 return -ENOMEM;
e24f0b8f 606
894bc310 607 if (page_count(page) == 1) {
e24f0b8f 608 /* page was freed from under us. So we are done. */
95a402c3 609 goto move_newpage;
894bc310 610 }
e24f0b8f 611
e8589cc1 612 /* prepare cgroup just returns 0 or -ENOMEM */
e24f0b8f 613 rc = -EAGAIN;
01b1ae63 614
529ae9aa 615 if (!trylock_page(page)) {
e24f0b8f 616 if (!force)
95a402c3 617 goto move_newpage;
e24f0b8f
CL
618 lock_page(page);
619 }
620
01b1ae63
KH
621 /* 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
e24f0b8f
CL
629 if (PageWriteback(page)) {
630 if (!force)
01b1ae63 631 goto uncharge;
e24f0b8f
CL
632 wait_on_page_writeback(page);
633 }
e24f0b8f 634 /*
dc386d4d
KH
635 * 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()
989f89c5
KH
639 * File Caches may use write_page() or lock_page() in migration, then,
640 * just care Anon page here.
dc386d4d 641 */
989f89c5
KH
642 if (PageAnon(page)) {
643 rcu_read_lock();
644 rcu_locked = 1;
645 }
62e1c553 646
dc386d4d 647 /*
62e1c553
SL
648 * 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.
e24f0b8f 658 */
62e1c553 659 if (!page->mapping) {
266cf658 660 if (!PageAnon(page) && page_has_private(page)) {
62e1c553
SL
661 /*
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);
abfc3488 669 goto rcu_unlock;
62e1c553 670 }
abfc3488 671 goto skip_unmap;
62e1c553
SL
672 }
673
dc386d4d 674 /* Establish migration ptes or remove ptes */
14fa31b8 675 try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
dc386d4d 676
abfc3488 677skip_unmap:
e6a1530d
CL
678 if (!page_mapped(page))
679 rc = move_to_new_page(newpage, page);
e24f0b8f 680
e8589cc1 681 if (rc)
e24f0b8f 682 remove_migration_ptes(page, page);
dc386d4d 683rcu_unlock:
989f89c5
KH
684 if (rcu_locked)
685 rcu_read_unlock();
01b1ae63
KH
686uncharge:
687 if (!charge)
688 mem_cgroup_end_migration(mem, page, newpage);
e24f0b8f
CL
689unlock:
690 unlock_page(page);
95a402c3 691
e24f0b8f 692 if (rc != -EAGAIN) {
aaa994b3
CL
693 /*
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);
a731286d 700 dec_zone_page_state(page, NR_ISOLATED_ANON +
6c0b1351 701 page_is_file_cache(page));
894bc310 702 putback_lru_page(page);
e24f0b8f 703 }
95a402c3
CL
704
705move_newpage:
894bc310 706
95a402c3
CL
707 /*
708 * Move the new page to the LRU. If migration was not successful
709 * then this will free the page.
710 */
894bc310
LS
711 putback_lru_page(newpage);
712
742755a1
CL
713 if (result) {
714 if (rc)
715 *result = rc;
716 else
717 *result = page_to_nid(newpage);
718 }
e24f0b8f
CL
719 return rc;
720}
721
b20a3503
CL
722/*
723 * migrate_pages
724 *
95a402c3
CL
725 * 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.
b20a3503
CL
728 *
729 * The function returns after 10 attempts or if no pages
730 * are movable anymore because to has become empty
aaa994b3 731 * or no retryable pages exist anymore. All pages will be
e9534b3f 732 * returned to the LRU or freed.
b20a3503 733 *
95a402c3 734 * Return: Number of pages not migrated or error code.
b20a3503 735 */
95a402c3
CL
736int migrate_pages(struct list_head *from,
737 new_page_t get_new_page, unsigned long private)
b20a3503 738{
e24f0b8f 739 int retry = 1;
b20a3503
CL
740 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
e24f0b8f
CL
750 for(pass = 0; pass < 10 && retry; pass++) {
751 retry = 0;
b20a3503 752
e24f0b8f 753 list_for_each_entry_safe(page, page2, from, lru) {
e24f0b8f 754 cond_resched();
2d1db3b1 755
95a402c3
CL
756 rc = unmap_and_move(get_new_page, private,
757 page, pass > 2);
2d1db3b1 758
e24f0b8f 759 switch(rc) {
95a402c3
CL
760 case -ENOMEM:
761 goto out;
e24f0b8f 762 case -EAGAIN:
2d1db3b1 763 retry++;
e24f0b8f
CL
764 break;
765 case 0:
e24f0b8f
CL
766 break;
767 default:
2d1db3b1 768 /* Permanent failure */
2d1db3b1 769 nr_failed++;
e24f0b8f 770 break;
2d1db3b1 771 }
b20a3503
CL
772 }
773 }
95a402c3
CL
774 rc = 0;
775out:
b20a3503
CL
776 if (!swapwrite)
777 current->flags &= ~PF_SWAPWRITE;
778
aaa994b3 779 putback_lru_pages(from);
b20a3503 780
95a402c3
CL
781 if (rc)
782 return rc;
b20a3503 783
95a402c3 784 return nr_failed + retry;
b20a3503 785}
95a402c3 786
742755a1
CL
787#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
6484eb3e 811 return alloc_pages_exact_node(pm->node,
769848c0 812 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
742755a1
CL
813}
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.
5e9a0f02 819 * The pm array ends with node = MAX_NUMNODES.
742755a1 820 */
5e9a0f02
BG
821static int do_move_page_to_node_array(struct mm_struct *mm,
822 struct page_to_node *pm,
823 int migrate_all)
742755a1
CL
824{
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 */
742755a1
CL
834 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
835 struct vm_area_struct *vma;
836 struct page *page;
837
742755a1
CL
838 err = -EFAULT;
839 vma = find_vma(mm, pp->addr);
0dc952dc 840 if (!vma || !vma_migratable(vma))
742755a1
CL
841 goto set_status;
842
843 page = follow_page(vma, pp->addr, FOLL_GET);
89f5b7da
LT
844
845 err = PTR_ERR(page);
846 if (IS_ERR(page))
847 goto set_status;
848
742755a1
CL
849 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
62695a84 870 err = isolate_lru_page(page);
6d9c285a 871 if (!err) {
62695a84 872 list_add_tail(&page->lru, &pagelist);
6d9c285a
KM
873 inc_zone_page_state(page, NR_ISOLATED_ANON +
874 page_is_file_cache(page));
875 }
742755a1
CL
876put_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
e78bbfa8 887 err = 0;
742755a1
CL
888 if (!list_empty(&pagelist))
889 err = migrate_pages(&pagelist, new_page_node,
890 (unsigned long)pm);
742755a1
CL
891
892 up_read(&mm->mmap_sem);
893 return err;
894}
895
5e9a0f02
BG
896/*
897 * 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{
3140a227 906 struct page_to_node *pm;
5e9a0f02 907 nodemask_t task_nodes;
3140a227
BG
908 unsigned long chunk_nr_pages;
909 unsigned long chunk_start;
910 int err;
5e9a0f02
BG
911
912 task_nodes = cpuset_mems_allowed(task);
913
3140a227
BG
914 err = -ENOMEM;
915 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
916 if (!pm)
5e9a0f02 917 goto out;
35282a2d
BG
918
919 migrate_prep();
920
5e9a0f02 921 /*
3140a227
BG
922 * Store a chunk of page_to_node array in a page,
923 * but keep the last one as a marker
5e9a0f02 924 */
3140a227 925 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
5e9a0f02 926
3140a227
BG
927 for (chunk_start = 0;
928 chunk_start < nr_pages;
929 chunk_start += chunk_nr_pages) {
930 int j;
5e9a0f02 931
3140a227
BG
932 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;
5e9a0f02
BG
938 int node;
939
3140a227
BG
940 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))
5e9a0f02
BG
946 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
3140a227
BG
956 pm[j].node = node;
957 }
958
959 /* 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;
5e9a0f02 967
5e9a0f02 968 /* Return status information */
3140a227
BG
969 for (j = 0; j < chunk_nr_pages; j++)
970 if (put_user(pm[j].status, status + j + chunk_start)) {
5e9a0f02 971 err = -EFAULT;
3140a227
BG
972 goto out_pm;
973 }
974 }
975 err = 0;
5e9a0f02
BG
976
977out_pm:
3140a227 978 free_page((unsigned long)pm);
5e9a0f02
BG
979out:
980 return err;
981}
982
742755a1 983/*
2f007e74 984 * Determine the nodes of an array of pages and store it in an array of status.
742755a1 985 */
80bba129
BG
986static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
987 const void __user **pages, int *status)
742755a1 988{
2f007e74 989 unsigned long i;
2f007e74 990
742755a1
CL
991 down_read(&mm->mmap_sem);
992
2f007e74 993 for (i = 0; i < nr_pages; i++) {
80bba129 994 unsigned long addr = (unsigned long)(*pages);
742755a1
CL
995 struct vm_area_struct *vma;
996 struct page *page;
c095adbc 997 int err = -EFAULT;
2f007e74
BG
998
999 vma = find_vma(mm, addr);
742755a1
CL
1000 if (!vma)
1001 goto set_status;
1002
2f007e74 1003 page = follow_page(vma, addr, 0);
89f5b7da
LT
1004
1005 err = PTR_ERR(page);
1006 if (IS_ERR(page))
1007 goto set_status;
1008
742755a1
CL
1009 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:
80bba129
BG
1016 *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) {
b9255850 1040 if (chunk_nr > nr_pages - i)
80bba129
BG
1041 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 }
742755a1 1058 }
2f007e74 1059 err = 0;
742755a1 1060
2f007e74 1061out:
2f007e74 1062 return err;
742755a1
CL
1063}
1064
1065/*
1066 * Move a list of pages in the address space of the currently executing
1067 * process.
1068 */
938bb9f5
HC
1069SYSCALL_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)
742755a1 1073{
c69e8d9c 1074 const struct cred *cred = current_cred(), *tcred;
742755a1 1075 struct task_struct *task;
742755a1 1076 struct mm_struct *mm;
5e9a0f02 1077 int err;
742755a1
CL
1078
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);
228ebcbe 1088 task = pid ? find_task_by_vpid(pid) : current;
742755a1
CL
1089 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 */
c69e8d9c
DH
1105 rcu_read_lock();
1106 tcred = __task_cred(task);
b6dff3ec
DH
1107 if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
1108 cred->uid != tcred->suid && cred->uid != tcred->uid &&
742755a1 1109 !capable(CAP_SYS_NICE)) {
c69e8d9c 1110 rcu_read_unlock();
742755a1 1111 err = -EPERM;
5e9a0f02 1112 goto out;
742755a1 1113 }
c69e8d9c 1114 rcu_read_unlock();
742755a1 1115
86c3a764
DQ
1116 err = security_task_movememory(task);
1117 if (err)
5e9a0f02 1118 goto out;
86c3a764 1119
5e9a0f02
BG
1120 if (nodes) {
1121 err = do_pages_move(mm, task, nr_pages, pages, nodes, status,
1122 flags);
1123 } else {
2f007e74 1124 err = do_pages_stat(mm, nr_pages, pages, status);
742755a1
CL
1125 }
1126
742755a1 1127out:
742755a1
CL
1128 mmput(mm);
1129 return err;
1130}
742755a1 1131
7b2259b3
CL
1132/*
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
1001c9fb 1143 for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
7b2259b3
CL
1144 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}
83d1674a 1152#endif