bugfix for memory controller: add helper function for assigning cgroup to page
[linux-2.6-block.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>
12 * Christoph Lameter <clameter@sgi.com>
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>
b20a3503
CL
33
34#include "internal.h"
35
b20a3503
CL
36#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
37
38/*
39 * Isolate one page from the LRU lists. If successful put it onto
40 * the indicated list with elevated page count.
41 *
42 * Result:
43 * -EBUSY: page not on LRU list
44 * 0: page removed from LRU list and added to the specified list.
45 */
46int isolate_lru_page(struct page *page, struct list_head *pagelist)
47{
48 int ret = -EBUSY;
49
50 if (PageLRU(page)) {
51 struct zone *zone = page_zone(page);
52
53 spin_lock_irq(&zone->lru_lock);
3dd9fe8c 54 if (PageLRU(page) && get_page_unless_zero(page)) {
b20a3503 55 ret = 0;
b20a3503
CL
56 ClearPageLRU(page);
57 if (PageActive(page))
58 del_page_from_active_list(zone, page);
59 else
60 del_page_from_inactive_list(zone, page);
61 list_add_tail(&page->lru, pagelist);
62 }
63 spin_unlock_irq(&zone->lru_lock);
64 }
65 return ret;
66}
67
68/*
742755a1
CL
69 * migrate_prep() needs to be called before we start compiling a list of pages
70 * to be migrated using isolate_lru_page().
b20a3503
CL
71 */
72int migrate_prep(void)
73{
b20a3503
CL
74 /*
75 * Clear the LRU lists so pages can be isolated.
76 * Note that pages may be moved off the LRU after we have
77 * drained them. Those pages will fail to migrate like other
78 * pages that may be busy.
79 */
80 lru_add_drain_all();
81
82 return 0;
83}
84
85static inline void move_to_lru(struct page *page)
86{
b20a3503
CL
87 if (PageActive(page)) {
88 /*
89 * lru_cache_add_active checks that
90 * the PG_active bit is off.
91 */
92 ClearPageActive(page);
93 lru_cache_add_active(page);
94 } else {
95 lru_cache_add(page);
96 }
97 put_page(page);
98}
99
100/*
101 * Add isolated pages on the list back to the LRU.
102 *
103 * returns the number of pages put back.
104 */
105int putback_lru_pages(struct list_head *l)
106{
107 struct page *page;
108 struct page *page2;
109 int count = 0;
110
111 list_for_each_entry_safe(page, page2, l, lru) {
e24f0b8f 112 list_del(&page->lru);
b20a3503
CL
113 move_to_lru(page);
114 count++;
115 }
116 return count;
117}
118
0697212a
CL
119/*
120 * Restore a potential migration pte to a working pte entry
121 */
04e62a29 122static void remove_migration_pte(struct vm_area_struct *vma,
0697212a
CL
123 struct page *old, struct page *new)
124{
125 struct mm_struct *mm = vma->vm_mm;
126 swp_entry_t entry;
127 pgd_t *pgd;
128 pud_t *pud;
129 pmd_t *pmd;
130 pte_t *ptep, pte;
131 spinlock_t *ptl;
04e62a29
CL
132 unsigned long addr = page_address_in_vma(new, vma);
133
134 if (addr == -EFAULT)
135 return;
0697212a
CL
136
137 pgd = pgd_offset(mm, addr);
138 if (!pgd_present(*pgd))
139 return;
140
141 pud = pud_offset(pgd, addr);
142 if (!pud_present(*pud))
143 return;
144
145 pmd = pmd_offset(pud, addr);
146 if (!pmd_present(*pmd))
147 return;
148
149 ptep = pte_offset_map(pmd, addr);
150
151 if (!is_swap_pte(*ptep)) {
152 pte_unmap(ptep);
153 return;
154 }
155
e1a1cd59 156 if (mem_cgroup_charge(new, mm, GFP_KERNEL)) {
8a9f3ccd
BS
157 pte_unmap(ptep);
158 return;
159 }
160
0697212a
CL
161 ptl = pte_lockptr(mm, pmd);
162 spin_lock(ptl);
163 pte = *ptep;
164 if (!is_swap_pte(pte))
165 goto out;
166
167 entry = pte_to_swp_entry(pte);
168
169 if (!is_migration_entry(entry) || migration_entry_to_page(entry) != old)
170 goto out;
171
0697212a
CL
172 get_page(new);
173 pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
174 if (is_write_migration_entry(entry))
175 pte = pte_mkwrite(pte);
97ee0524 176 flush_cache_page(vma, addr, pte_pfn(pte));
0697212a 177 set_pte_at(mm, addr, ptep, pte);
04e62a29
CL
178
179 if (PageAnon(new))
180 page_add_anon_rmap(new, vma, addr);
181 else
182 page_add_file_rmap(new);
183
184 /* No need to invalidate - it was non-present before */
185 update_mmu_cache(vma, addr, pte);
04e62a29 186
0697212a
CL
187out:
188 pte_unmap_unlock(ptep, ptl);
189}
190
191/*
04e62a29
CL
192 * Note that remove_file_migration_ptes will only work on regular mappings,
193 * Nonlinear mappings do not use migration entries.
194 */
195static void remove_file_migration_ptes(struct page *old, struct page *new)
196{
197 struct vm_area_struct *vma;
198 struct address_space *mapping = page_mapping(new);
199 struct prio_tree_iter iter;
200 pgoff_t pgoff = new->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
201
202 if (!mapping)
203 return;
204
205 spin_lock(&mapping->i_mmap_lock);
206
207 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff)
208 remove_migration_pte(vma, old, new);
209
210 spin_unlock(&mapping->i_mmap_lock);
211}
212
213/*
0697212a
CL
214 * Must hold mmap_sem lock on at least one of the vmas containing
215 * the page so that the anon_vma cannot vanish.
216 */
04e62a29 217static void remove_anon_migration_ptes(struct page *old, struct page *new)
0697212a
CL
218{
219 struct anon_vma *anon_vma;
220 struct vm_area_struct *vma;
221 unsigned long mapping;
222
223 mapping = (unsigned long)new->mapping;
224
225 if (!mapping || (mapping & PAGE_MAPPING_ANON) == 0)
226 return;
227
228 /*
229 * We hold the mmap_sem lock. So no need to call page_lock_anon_vma.
230 */
231 anon_vma = (struct anon_vma *) (mapping - PAGE_MAPPING_ANON);
232 spin_lock(&anon_vma->lock);
233
234 list_for_each_entry(vma, &anon_vma->head, anon_vma_node)
04e62a29 235 remove_migration_pte(vma, old, new);
0697212a
CL
236
237 spin_unlock(&anon_vma->lock);
238}
239
04e62a29
CL
240/*
241 * Get rid of all migration entries and replace them by
242 * references to the indicated page.
243 */
244static void remove_migration_ptes(struct page *old, struct page *new)
245{
246 if (PageAnon(new))
247 remove_anon_migration_ptes(old, new);
248 else
249 remove_file_migration_ptes(old, new);
250}
251
0697212a
CL
252/*
253 * Something used the pte of a page under migration. We need to
254 * get to the page and wait until migration is finished.
255 * When we return from this function the fault will be retried.
256 *
257 * This function is called from do_swap_page().
258 */
259void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
260 unsigned long address)
261{
262 pte_t *ptep, pte;
263 spinlock_t *ptl;
264 swp_entry_t entry;
265 struct page *page;
266
267 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
268 pte = *ptep;
269 if (!is_swap_pte(pte))
270 goto out;
271
272 entry = pte_to_swp_entry(pte);
273 if (!is_migration_entry(entry))
274 goto out;
275
276 page = migration_entry_to_page(entry);
277
278 get_page(page);
279 pte_unmap_unlock(ptep, ptl);
280 wait_on_page_locked(page);
281 put_page(page);
282 return;
283out:
284 pte_unmap_unlock(ptep, ptl);
285}
286
b20a3503 287/*
c3fcf8a5 288 * Replace the page in the mapping.
5b5c7120
CL
289 *
290 * The number of remaining references must be:
291 * 1 for anonymous pages without a mapping
292 * 2 for pages with a mapping
293 * 3 for pages with a mapping and PagePrivate set.
b20a3503 294 */
2d1db3b1
CL
295static int migrate_page_move_mapping(struct address_space *mapping,
296 struct page *newpage, struct page *page)
b20a3503 297{
7cf9c2c7 298 void **pslot;
b20a3503 299
6c5240ae 300 if (!mapping) {
0e8c7d0f 301 /* Anonymous page without mapping */
6c5240ae
CL
302 if (page_count(page) != 1)
303 return -EAGAIN;
304 return 0;
305 }
306
b20a3503
CL
307 write_lock_irq(&mapping->tree_lock);
308
7cf9c2c7
NP
309 pslot = radix_tree_lookup_slot(&mapping->page_tree,
310 page_index(page));
b20a3503 311
6c5240ae 312 if (page_count(page) != 2 + !!PagePrivate(page) ||
7cf9c2c7 313 (struct page *)radix_tree_deref_slot(pslot) != page) {
b20a3503 314 write_unlock_irq(&mapping->tree_lock);
e23ca00b 315 return -EAGAIN;
b20a3503
CL
316 }
317
318 /*
319 * Now we know that no one else is looking at the page.
b20a3503 320 */
7cf9c2c7 321 get_page(newpage); /* add cache reference */
6c5240ae 322#ifdef CONFIG_SWAP
b20a3503
CL
323 if (PageSwapCache(page)) {
324 SetPageSwapCache(newpage);
325 set_page_private(newpage, page_private(page));
326 }
6c5240ae 327#endif
b20a3503 328
7cf9c2c7
NP
329 radix_tree_replace_slot(pslot, newpage);
330
331 /*
332 * Drop cache reference from old page.
333 * We know this isn't the last reference.
334 */
b20a3503 335 __put_page(page);
7cf9c2c7 336
0e8c7d0f
CL
337 /*
338 * If moved to a different zone then also account
339 * the page for that zone. Other VM counters will be
340 * taken care of when we establish references to the
341 * new page and drop references to the old page.
342 *
343 * Note that anonymous pages are accounted for
344 * via NR_FILE_PAGES and NR_ANON_PAGES if they
345 * are mapped to swap space.
346 */
347 __dec_zone_page_state(page, NR_FILE_PAGES);
348 __inc_zone_page_state(newpage, NR_FILE_PAGES);
349
b20a3503
CL
350 write_unlock_irq(&mapping->tree_lock);
351
352 return 0;
353}
b20a3503
CL
354
355/*
356 * Copy the page to its new location
357 */
e7340f73 358static void migrate_page_copy(struct page *newpage, struct page *page)
b20a3503
CL
359{
360 copy_highpage(newpage, page);
361
362 if (PageError(page))
363 SetPageError(newpage);
364 if (PageReferenced(page))
365 SetPageReferenced(newpage);
366 if (PageUptodate(page))
367 SetPageUptodate(newpage);
368 if (PageActive(page))
369 SetPageActive(newpage);
370 if (PageChecked(page))
371 SetPageChecked(newpage);
372 if (PageMappedToDisk(page))
373 SetPageMappedToDisk(newpage);
374
375 if (PageDirty(page)) {
376 clear_page_dirty_for_io(page);
377 set_page_dirty(newpage);
378 }
379
6c5240ae 380#ifdef CONFIG_SWAP
b20a3503 381 ClearPageSwapCache(page);
6c5240ae 382#endif
b20a3503
CL
383 ClearPageActive(page);
384 ClearPagePrivate(page);
385 set_page_private(page, 0);
386 page->mapping = NULL;
387
388 /*
389 * If any waiters have accumulated on the new page then
390 * wake them up.
391 */
392 if (PageWriteback(newpage))
393 end_page_writeback(newpage);
394}
b20a3503 395
1d8b85cc
CL
396/************************************************************
397 * Migration functions
398 ***********************************************************/
399
400/* Always fail migration. Used for mappings that are not movable */
2d1db3b1
CL
401int fail_migrate_page(struct address_space *mapping,
402 struct page *newpage, struct page *page)
1d8b85cc
CL
403{
404 return -EIO;
405}
406EXPORT_SYMBOL(fail_migrate_page);
407
b20a3503
CL
408/*
409 * Common logic to directly migrate a single page suitable for
410 * pages that do not use PagePrivate.
411 *
412 * Pages are locked upon entry and exit.
413 */
2d1db3b1
CL
414int migrate_page(struct address_space *mapping,
415 struct page *newpage, struct page *page)
b20a3503
CL
416{
417 int rc;
418
419 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
420
2d1db3b1 421 rc = migrate_page_move_mapping(mapping, newpage, page);
b20a3503
CL
422
423 if (rc)
424 return rc;
425
426 migrate_page_copy(newpage, page);
b20a3503
CL
427 return 0;
428}
429EXPORT_SYMBOL(migrate_page);
430
9361401e 431#ifdef CONFIG_BLOCK
1d8b85cc
CL
432/*
433 * Migration function for pages with buffers. This function can only be used
434 * if the underlying filesystem guarantees that no other references to "page"
435 * exist.
436 */
2d1db3b1
CL
437int buffer_migrate_page(struct address_space *mapping,
438 struct page *newpage, struct page *page)
1d8b85cc 439{
1d8b85cc
CL
440 struct buffer_head *bh, *head;
441 int rc;
442
1d8b85cc 443 if (!page_has_buffers(page))
2d1db3b1 444 return migrate_page(mapping, newpage, page);
1d8b85cc
CL
445
446 head = page_buffers(page);
447
2d1db3b1 448 rc = migrate_page_move_mapping(mapping, newpage, page);
1d8b85cc
CL
449
450 if (rc)
451 return rc;
452
453 bh = head;
454 do {
455 get_bh(bh);
456 lock_buffer(bh);
457 bh = bh->b_this_page;
458
459 } while (bh != head);
460
461 ClearPagePrivate(page);
462 set_page_private(newpage, page_private(page));
463 set_page_private(page, 0);
464 put_page(page);
465 get_page(newpage);
466
467 bh = head;
468 do {
469 set_bh_page(bh, newpage, bh_offset(bh));
470 bh = bh->b_this_page;
471
472 } while (bh != head);
473
474 SetPagePrivate(newpage);
475
476 migrate_page_copy(newpage, page);
477
478 bh = head;
479 do {
480 unlock_buffer(bh);
481 put_bh(bh);
482 bh = bh->b_this_page;
483
484 } while (bh != head);
485
486 return 0;
487}
488EXPORT_SYMBOL(buffer_migrate_page);
9361401e 489#endif
1d8b85cc 490
04e62a29
CL
491/*
492 * Writeback a page to clean the dirty state
493 */
494static int writeout(struct address_space *mapping, struct page *page)
8351a6e4 495{
04e62a29
CL
496 struct writeback_control wbc = {
497 .sync_mode = WB_SYNC_NONE,
498 .nr_to_write = 1,
499 .range_start = 0,
500 .range_end = LLONG_MAX,
501 .nonblocking = 1,
502 .for_reclaim = 1
503 };
504 int rc;
505
506 if (!mapping->a_ops->writepage)
507 /* No write method for the address space */
508 return -EINVAL;
509
510 if (!clear_page_dirty_for_io(page))
511 /* Someone else already triggered a write */
512 return -EAGAIN;
513
8351a6e4 514 /*
04e62a29
CL
515 * A dirty page may imply that the underlying filesystem has
516 * the page on some queue. So the page must be clean for
517 * migration. Writeout may mean we loose the lock and the
518 * page state is no longer what we checked for earlier.
519 * At this point we know that the migration attempt cannot
520 * be successful.
8351a6e4 521 */
04e62a29 522 remove_migration_ptes(page, page);
8351a6e4 523
04e62a29
CL
524 rc = mapping->a_ops->writepage(page, &wbc);
525 if (rc < 0)
526 /* I/O Error writing */
527 return -EIO;
8351a6e4 528
04e62a29
CL
529 if (rc != AOP_WRITEPAGE_ACTIVATE)
530 /* unlocked. Relock */
531 lock_page(page);
532
533 return -EAGAIN;
534}
535
536/*
537 * Default handling if a filesystem does not provide a migration function.
538 */
539static int fallback_migrate_page(struct address_space *mapping,
540 struct page *newpage, struct page *page)
541{
542 if (PageDirty(page))
543 return writeout(mapping, page);
8351a6e4
CL
544
545 /*
546 * Buffers may be managed in a filesystem specific way.
547 * We must have no buffers or drop them.
548 */
b398f6bf 549 if (PagePrivate(page) &&
8351a6e4
CL
550 !try_to_release_page(page, GFP_KERNEL))
551 return -EAGAIN;
552
553 return migrate_page(mapping, newpage, page);
554}
555
e24f0b8f
CL
556/*
557 * Move a page to a newly allocated page
558 * The page is locked and all ptes have been successfully removed.
559 *
560 * The new page will have replaced the old page if this function
561 * is successful.
562 */
563static int move_to_new_page(struct page *newpage, struct page *page)
564{
565 struct address_space *mapping;
566 int rc;
567
568 /*
569 * Block others from accessing the page when we get around to
570 * establishing additional references. We are the only one
571 * holding a reference to the new page at this point.
572 */
573 if (TestSetPageLocked(newpage))
574 BUG();
575
576 /* Prepare mapping for the new page.*/
577 newpage->index = page->index;
578 newpage->mapping = page->mapping;
579
580 mapping = page_mapping(page);
581 if (!mapping)
582 rc = migrate_page(mapping, newpage, page);
583 else if (mapping->a_ops->migratepage)
584 /*
585 * Most pages have a mapping and most filesystems
586 * should provide a migration function. Anonymous
587 * pages are part of swap space which also has its
588 * own migration function. This is the most common
589 * path for page migration.
590 */
591 rc = mapping->a_ops->migratepage(mapping,
592 newpage, page);
593 else
594 rc = fallback_migrate_page(mapping, newpage, page);
595
596 if (!rc)
597 remove_migration_ptes(page, newpage);
598 else
599 newpage->mapping = NULL;
600
601 unlock_page(newpage);
602
603 return rc;
604}
605
606/*
607 * Obtain the lock on page, remove all ptes and migrate the page
608 * to the newly allocated page in newpage.
609 */
95a402c3
CL
610static int unmap_and_move(new_page_t get_new_page, unsigned long private,
611 struct page *page, int force)
e24f0b8f
CL
612{
613 int rc = 0;
742755a1
CL
614 int *result = NULL;
615 struct page *newpage = get_new_page(page, private, &result);
989f89c5 616 int rcu_locked = 0;
95a402c3
CL
617
618 if (!newpage)
619 return -ENOMEM;
e24f0b8f
CL
620
621 if (page_count(page) == 1)
622 /* page was freed from under us. So we are done. */
95a402c3 623 goto move_newpage;
e24f0b8f
CL
624
625 rc = -EAGAIN;
626 if (TestSetPageLocked(page)) {
627 if (!force)
95a402c3 628 goto move_newpage;
e24f0b8f
CL
629 lock_page(page);
630 }
631
632 if (PageWriteback(page)) {
633 if (!force)
634 goto unlock;
635 wait_on_page_writeback(page);
636 }
e24f0b8f 637 /*
dc386d4d
KH
638 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
639 * we cannot notice that anon_vma is freed while we migrates a page.
640 * This rcu_read_lock() delays freeing anon_vma pointer until the end
641 * of migration. File cache pages are no problem because of page_lock()
989f89c5
KH
642 * File Caches may use write_page() or lock_page() in migration, then,
643 * just care Anon page here.
dc386d4d 644 */
989f89c5
KH
645 if (PageAnon(page)) {
646 rcu_read_lock();
647 rcu_locked = 1;
648 }
62e1c553 649
dc386d4d 650 /*
62e1c553
SL
651 * Corner case handling:
652 * 1. When a new swap-cache page is read into, it is added to the LRU
653 * and treated as swapcache but it has no rmap yet.
654 * Calling try_to_unmap() against a page->mapping==NULL page will
655 * trigger a BUG. So handle it here.
656 * 2. An orphaned page (see truncate_complete_page) might have
657 * fs-private metadata. The page can be picked up due to memory
658 * offlining. Everywhere else except page reclaim, the page is
659 * invisible to the vm, so the page can not be migrated. So try to
660 * free the metadata, so the page can be freed.
e24f0b8f 661 */
62e1c553
SL
662 if (!page->mapping) {
663 if (!PageAnon(page) && PagePrivate(page)) {
664 /*
665 * Go direct to try_to_free_buffers() here because
666 * a) that's what try_to_release_page() would do anyway
667 * b) we may be under rcu_read_lock() here, so we can't
668 * use GFP_KERNEL which is what try_to_release_page()
669 * needs to be effective.
670 */
671 try_to_free_buffers(page);
672 }
dc386d4d 673 goto rcu_unlock;
62e1c553
SL
674 }
675
dc386d4d 676 /* Establish migration ptes or remove ptes */
e6a1530d 677 try_to_unmap(page, 1);
dc386d4d 678
e6a1530d
CL
679 if (!page_mapped(page))
680 rc = move_to_new_page(newpage, page);
e24f0b8f
CL
681
682 if (rc)
683 remove_migration_ptes(page, page);
dc386d4d 684rcu_unlock:
989f89c5
KH
685 if (rcu_locked)
686 rcu_read_unlock();
e6a1530d 687
e24f0b8f 688unlock:
dc386d4d 689
e24f0b8f 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);
700 move_to_lru(page);
e24f0b8f 701 }
95a402c3
CL
702
703move_newpage:
704 /*
705 * Move the new page to the LRU. If migration was not successful
706 * then this will free the page.
707 */
708 move_to_lru(newpage);
742755a1
CL
709 if (result) {
710 if (rc)
711 *result = rc;
712 else
713 *result = page_to_nid(newpage);
714 }
e24f0b8f
CL
715 return rc;
716}
717
b20a3503
CL
718/*
719 * migrate_pages
720 *
95a402c3
CL
721 * The function takes one list of pages to migrate and a function
722 * that determines from the page to be migrated and the private data
723 * the target of the move and allocates the page.
b20a3503
CL
724 *
725 * The function returns after 10 attempts or if no pages
726 * are movable anymore because to has become empty
aaa994b3 727 * or no retryable pages exist anymore. All pages will be
e9534b3f 728 * returned to the LRU or freed.
b20a3503 729 *
95a402c3 730 * Return: Number of pages not migrated or error code.
b20a3503 731 */
95a402c3
CL
732int migrate_pages(struct list_head *from,
733 new_page_t get_new_page, unsigned long private)
b20a3503 734{
e24f0b8f 735 int retry = 1;
b20a3503
CL
736 int nr_failed = 0;
737 int pass = 0;
738 struct page *page;
739 struct page *page2;
740 int swapwrite = current->flags & PF_SWAPWRITE;
741 int rc;
742
743 if (!swapwrite)
744 current->flags |= PF_SWAPWRITE;
745
e24f0b8f
CL
746 for(pass = 0; pass < 10 && retry; pass++) {
747 retry = 0;
b20a3503 748
e24f0b8f 749 list_for_each_entry_safe(page, page2, from, lru) {
e24f0b8f 750 cond_resched();
2d1db3b1 751
95a402c3
CL
752 rc = unmap_and_move(get_new_page, private,
753 page, pass > 2);
2d1db3b1 754
e24f0b8f 755 switch(rc) {
95a402c3
CL
756 case -ENOMEM:
757 goto out;
e24f0b8f 758 case -EAGAIN:
2d1db3b1 759 retry++;
e24f0b8f
CL
760 break;
761 case 0:
e24f0b8f
CL
762 break;
763 default:
2d1db3b1 764 /* Permanent failure */
2d1db3b1 765 nr_failed++;
e24f0b8f 766 break;
2d1db3b1 767 }
b20a3503
CL
768 }
769 }
95a402c3
CL
770 rc = 0;
771out:
b20a3503
CL
772 if (!swapwrite)
773 current->flags &= ~PF_SWAPWRITE;
774
aaa994b3 775 putback_lru_pages(from);
b20a3503 776
95a402c3
CL
777 if (rc)
778 return rc;
b20a3503 779
95a402c3 780 return nr_failed + retry;
b20a3503 781}
95a402c3 782
742755a1
CL
783#ifdef CONFIG_NUMA
784/*
785 * Move a list of individual pages
786 */
787struct page_to_node {
788 unsigned long addr;
789 struct page *page;
790 int node;
791 int status;
792};
793
794static struct page *new_page_node(struct page *p, unsigned long private,
795 int **result)
796{
797 struct page_to_node *pm = (struct page_to_node *)private;
798
799 while (pm->node != MAX_NUMNODES && pm->page != p)
800 pm++;
801
802 if (pm->node == MAX_NUMNODES)
803 return NULL;
804
805 *result = &pm->status;
806
769848c0
MG
807 return alloc_pages_node(pm->node,
808 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
742755a1
CL
809}
810
811/*
812 * Move a set of pages as indicated in the pm array. The addr
813 * field must be set to the virtual address of the page to be moved
814 * and the node number must contain a valid target node.
815 */
816static int do_move_pages(struct mm_struct *mm, struct page_to_node *pm,
817 int migrate_all)
818{
819 int err;
820 struct page_to_node *pp;
821 LIST_HEAD(pagelist);
822
823 down_read(&mm->mmap_sem);
824
825 /*
826 * Build a list of pages to migrate
827 */
828 migrate_prep();
829 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
830 struct vm_area_struct *vma;
831 struct page *page;
832
833 /*
834 * A valid page pointer that will not match any of the
835 * pages that will be moved.
836 */
837 pp->page = ZERO_PAGE(0);
838
839 err = -EFAULT;
840 vma = find_vma(mm, pp->addr);
0dc952dc 841 if (!vma || !vma_migratable(vma))
742755a1
CL
842 goto set_status;
843
844 page = follow_page(vma, pp->addr, FOLL_GET);
845 err = -ENOENT;
846 if (!page)
847 goto set_status;
848
849 if (PageReserved(page)) /* Check for zero page */
850 goto put_and_set;
851
852 pp->page = page;
853 err = page_to_nid(page);
854
855 if (err == pp->node)
856 /*
857 * Node already in the right place
858 */
859 goto put_and_set;
860
861 err = -EACCES;
862 if (page_mapcount(page) > 1 &&
863 !migrate_all)
864 goto put_and_set;
865
866 err = isolate_lru_page(page, &pagelist);
867put_and_set:
868 /*
869 * Either remove the duplicate refcount from
870 * isolate_lru_page() or drop the page ref if it was
871 * not isolated.
872 */
873 put_page(page);
874set_status:
875 pp->status = err;
876 }
877
878 if (!list_empty(&pagelist))
879 err = migrate_pages(&pagelist, new_page_node,
880 (unsigned long)pm);
881 else
882 err = -ENOENT;
883
884 up_read(&mm->mmap_sem);
885 return err;
886}
887
888/*
889 * Determine the nodes of a list of pages. The addr in the pm array
890 * must have been set to the virtual address of which we want to determine
891 * the node number.
892 */
893static int do_pages_stat(struct mm_struct *mm, struct page_to_node *pm)
894{
895 down_read(&mm->mmap_sem);
896
897 for ( ; pm->node != MAX_NUMNODES; pm++) {
898 struct vm_area_struct *vma;
899 struct page *page;
900 int err;
901
902 err = -EFAULT;
903 vma = find_vma(mm, pm->addr);
904 if (!vma)
905 goto set_status;
906
907 page = follow_page(vma, pm->addr, 0);
908 err = -ENOENT;
909 /* Use PageReserved to check for zero page */
910 if (!page || PageReserved(page))
911 goto set_status;
912
913 err = page_to_nid(page);
914set_status:
915 pm->status = err;
916 }
917
918 up_read(&mm->mmap_sem);
919 return 0;
920}
921
922/*
923 * Move a list of pages in the address space of the currently executing
924 * process.
925 */
926asmlinkage long sys_move_pages(pid_t pid, unsigned long nr_pages,
927 const void __user * __user *pages,
928 const int __user *nodes,
929 int __user *status, int flags)
930{
931 int err = 0;
932 int i;
933 struct task_struct *task;
934 nodemask_t task_nodes;
935 struct mm_struct *mm;
936 struct page_to_node *pm = NULL;
937
938 /* Check flags */
939 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
940 return -EINVAL;
941
942 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
943 return -EPERM;
944
945 /* Find the mm_struct */
946 read_lock(&tasklist_lock);
228ebcbe 947 task = pid ? find_task_by_vpid(pid) : current;
742755a1
CL
948 if (!task) {
949 read_unlock(&tasklist_lock);
950 return -ESRCH;
951 }
952 mm = get_task_mm(task);
953 read_unlock(&tasklist_lock);
954
955 if (!mm)
956 return -EINVAL;
957
958 /*
959 * Check if this process has the right to modify the specified
960 * process. The right exists if the process has administrative
961 * capabilities, superuser privileges or the same
962 * userid as the target process.
963 */
964 if ((current->euid != task->suid) && (current->euid != task->uid) &&
965 (current->uid != task->suid) && (current->uid != task->uid) &&
966 !capable(CAP_SYS_NICE)) {
967 err = -EPERM;
968 goto out2;
969 }
970
86c3a764
DQ
971 err = security_task_movememory(task);
972 if (err)
973 goto out2;
974
975
742755a1
CL
976 task_nodes = cpuset_mems_allowed(task);
977
978 /* Limit nr_pages so that the multiplication may not overflow */
979 if (nr_pages >= ULONG_MAX / sizeof(struct page_to_node) - 1) {
980 err = -E2BIG;
981 goto out2;
982 }
983
984 pm = vmalloc((nr_pages + 1) * sizeof(struct page_to_node));
985 if (!pm) {
986 err = -ENOMEM;
987 goto out2;
988 }
989
990 /*
991 * Get parameters from user space and initialize the pm
992 * array. Return various errors if the user did something wrong.
993 */
994 for (i = 0; i < nr_pages; i++) {
9d966d49 995 const void __user *p;
742755a1
CL
996
997 err = -EFAULT;
998 if (get_user(p, pages + i))
999 goto out;
1000
1001 pm[i].addr = (unsigned long)p;
1002 if (nodes) {
1003 int node;
1004
1005 if (get_user(node, nodes + i))
1006 goto out;
1007
1008 err = -ENODEV;
56bbd65d 1009 if (!node_state(node, N_HIGH_MEMORY))
742755a1
CL
1010 goto out;
1011
1012 err = -EACCES;
1013 if (!node_isset(node, task_nodes))
1014 goto out;
1015
1016 pm[i].node = node;
8ce08464
SR
1017 } else
1018 pm[i].node = 0; /* anything to not match MAX_NUMNODES */
742755a1
CL
1019 }
1020 /* End marker */
1021 pm[nr_pages].node = MAX_NUMNODES;
1022
1023 if (nodes)
1024 err = do_move_pages(mm, pm, flags & MPOL_MF_MOVE_ALL);
1025 else
1026 err = do_pages_stat(mm, pm);
1027
1028 if (err >= 0)
1029 /* Return status information */
1030 for (i = 0; i < nr_pages; i++)
1031 if (put_user(pm[i].status, status + i))
1032 err = -EFAULT;
1033
1034out:
1035 vfree(pm);
1036out2:
1037 mmput(mm);
1038 return err;
1039}
1040#endif
1041
7b2259b3
CL
1042/*
1043 * Call migration functions in the vma_ops that may prepare
1044 * memory in a vm for migration. migration functions may perform
1045 * the migration for vmas that do not have an underlying page struct.
1046 */
1047int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1048 const nodemask_t *from, unsigned long flags)
1049{
1050 struct vm_area_struct *vma;
1051 int err = 0;
1052
1053 for(vma = mm->mmap; vma->vm_next && !err; vma = vma->vm_next) {
1054 if (vma->vm_ops && vma->vm_ops->migrate) {
1055 err = vma->vm_ops->migrate(vma, to, from, flags);
1056 if (err)
1057 break;
1058 }
1059 }
1060 return err;
1061}