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