mm: page allocator: do not call direct reclaim for THP allocations while compaction...
[deliverable/linux.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>
b95f1b31 16#include <linux/export.h>
b20a3503 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>
290408d4 35#include <linux/hugetlb.h>
5a0e3ad6 36#include <linux/gfp.h>
b20a3503 37
0d1836c3
MN
38#include <asm/tlbflush.h>
39
b20a3503
CL
40#include "internal.h"
41
b20a3503 42/*
742755a1 43 * migrate_prep() needs to be called before we start compiling a list of pages
748446bb
MG
44 * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is
45 * undesirable, use migrate_prep_local()
b20a3503
CL
46 */
47int migrate_prep(void)
48{
b20a3503
CL
49 /*
50 * Clear the LRU lists so pages can be isolated.
51 * Note that pages may be moved off the LRU after we have
52 * drained them. Those pages will fail to migrate like other
53 * pages that may be busy.
54 */
55 lru_add_drain_all();
56
57 return 0;
58}
59
748446bb
MG
60/* Do the necessary work of migrate_prep but not if it involves other CPUs */
61int migrate_prep_local(void)
62{
63 lru_add_drain();
64
65 return 0;
66}
67
b20a3503 68/*
894bc310
LS
69 * Add isolated pages on the list back to the LRU under page lock
70 * to avoid leaking evictable pages back onto unevictable list.
b20a3503 71 */
e13861d8 72void putback_lru_pages(struct list_head *l)
b20a3503
CL
73{
74 struct page *page;
75 struct page *page2;
b20a3503
CL
76
77 list_for_each_entry_safe(page, page2, l, lru) {
e24f0b8f 78 list_del(&page->lru);
a731286d 79 dec_zone_page_state(page, NR_ISOLATED_ANON +
6c0b1351 80 page_is_file_cache(page));
894bc310 81 putback_lru_page(page);
b20a3503 82 }
b20a3503
CL
83}
84
0697212a
CL
85/*
86 * Restore a potential migration pte to a working pte entry
87 */
e9995ef9
HD
88static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,
89 unsigned long addr, void *old)
0697212a
CL
90{
91 struct mm_struct *mm = vma->vm_mm;
92 swp_entry_t entry;
93 pgd_t *pgd;
94 pud_t *pud;
95 pmd_t *pmd;
96 pte_t *ptep, pte;
97 spinlock_t *ptl;
98
290408d4
NH
99 if (unlikely(PageHuge(new))) {
100 ptep = huge_pte_offset(mm, addr);
101 if (!ptep)
102 goto out;
103 ptl = &mm->page_table_lock;
104 } else {
105 pgd = pgd_offset(mm, addr);
106 if (!pgd_present(*pgd))
107 goto out;
0697212a 108
290408d4
NH
109 pud = pud_offset(pgd, addr);
110 if (!pud_present(*pud))
111 goto out;
0697212a 112
290408d4 113 pmd = pmd_offset(pud, addr);
500d65d4
AA
114 if (pmd_trans_huge(*pmd))
115 goto out;
290408d4
NH
116 if (!pmd_present(*pmd))
117 goto out;
0697212a 118
290408d4 119 ptep = pte_offset_map(pmd, addr);
0697212a 120
486cf46f
HD
121 /*
122 * Peek to check is_swap_pte() before taking ptlock? No, we
123 * can race mremap's move_ptes(), which skips anon_vma lock.
124 */
290408d4
NH
125
126 ptl = pte_lockptr(mm, pmd);
127 }
0697212a 128
0697212a
CL
129 spin_lock(ptl);
130 pte = *ptep;
131 if (!is_swap_pte(pte))
e9995ef9 132 goto unlock;
0697212a
CL
133
134 entry = pte_to_swp_entry(pte);
135
e9995ef9
HD
136 if (!is_migration_entry(entry) ||
137 migration_entry_to_page(entry) != old)
138 goto unlock;
0697212a 139
0697212a
CL
140 get_page(new);
141 pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
142 if (is_write_migration_entry(entry))
143 pte = pte_mkwrite(pte);
3ef8fd7f 144#ifdef CONFIG_HUGETLB_PAGE
290408d4
NH
145 if (PageHuge(new))
146 pte = pte_mkhuge(pte);
3ef8fd7f 147#endif
97ee0524 148 flush_cache_page(vma, addr, pte_pfn(pte));
0697212a 149 set_pte_at(mm, addr, ptep, pte);
04e62a29 150
290408d4
NH
151 if (PageHuge(new)) {
152 if (PageAnon(new))
153 hugepage_add_anon_rmap(new, vma, addr);
154 else
155 page_dup_rmap(new);
156 } else if (PageAnon(new))
04e62a29
CL
157 page_add_anon_rmap(new, vma, addr);
158 else
159 page_add_file_rmap(new);
160
161 /* No need to invalidate - it was non-present before */
4b3073e1 162 update_mmu_cache(vma, addr, ptep);
e9995ef9 163unlock:
0697212a 164 pte_unmap_unlock(ptep, ptl);
e9995ef9
HD
165out:
166 return SWAP_AGAIN;
0697212a
CL
167}
168
04e62a29
CL
169/*
170 * Get rid of all migration entries and replace them by
171 * references to the indicated page.
172 */
173static void remove_migration_ptes(struct page *old, struct page *new)
174{
e9995ef9 175 rmap_walk(new, remove_migration_pte, old);
04e62a29
CL
176}
177
0697212a
CL
178/*
179 * Something used the pte of a page under migration. We need to
180 * get to the page and wait until migration is finished.
181 * When we return from this function the fault will be retried.
0697212a
CL
182 */
183void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
184 unsigned long address)
185{
186 pte_t *ptep, pte;
187 spinlock_t *ptl;
188 swp_entry_t entry;
189 struct page *page;
190
191 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
192 pte = *ptep;
193 if (!is_swap_pte(pte))
194 goto out;
195
196 entry = pte_to_swp_entry(pte);
197 if (!is_migration_entry(entry))
198 goto out;
199
200 page = migration_entry_to_page(entry);
201
e286781d
NP
202 /*
203 * Once radix-tree replacement of page migration started, page_count
204 * *must* be zero. And, we don't want to call wait_on_page_locked()
205 * against a page without get_page().
206 * So, we use get_page_unless_zero(), here. Even failed, page fault
207 * will occur again.
208 */
209 if (!get_page_unless_zero(page))
210 goto out;
0697212a
CL
211 pte_unmap_unlock(ptep, ptl);
212 wait_on_page_locked(page);
213 put_page(page);
214 return;
215out:
216 pte_unmap_unlock(ptep, ptl);
217}
218
b969c4ab
MG
219#ifdef CONFIG_BLOCK
220/* Returns true if all buffers are successfully locked */
221static bool buffer_migrate_lock_buffers(struct buffer_head *head, bool sync)
222{
223 struct buffer_head *bh = head;
224
225 /* Simple case, sync compaction */
226 if (sync) {
227 do {
228 get_bh(bh);
229 lock_buffer(bh);
230 bh = bh->b_this_page;
231
232 } while (bh != head);
233
234 return true;
235 }
236
237 /* async case, we cannot block on lock_buffer so use trylock_buffer */
238 do {
239 get_bh(bh);
240 if (!trylock_buffer(bh)) {
241 /*
242 * We failed to lock the buffer and cannot stall in
243 * async migration. Release the taken locks
244 */
245 struct buffer_head *failed_bh = bh;
246 put_bh(failed_bh);
247 bh = head;
248 while (bh != failed_bh) {
249 unlock_buffer(bh);
250 put_bh(bh);
251 bh = bh->b_this_page;
252 }
253 return false;
254 }
255
256 bh = bh->b_this_page;
257 } while (bh != head);
258 return true;
259}
260#else
261static inline bool buffer_migrate_lock_buffers(struct buffer_head *head,
262 bool sync)
263{
264 return true;
265}
266#endif /* CONFIG_BLOCK */
267
b20a3503 268/*
c3fcf8a5 269 * Replace the page in the mapping.
5b5c7120
CL
270 *
271 * The number of remaining references must be:
272 * 1 for anonymous pages without a mapping
273 * 2 for pages with a mapping
266cf658 274 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
b20a3503 275 */
2d1db3b1 276static int migrate_page_move_mapping(struct address_space *mapping,
b969c4ab
MG
277 struct page *newpage, struct page *page,
278 struct buffer_head *head, bool sync)
b20a3503 279{
e286781d 280 int expected_count;
7cf9c2c7 281 void **pslot;
b20a3503 282
6c5240ae 283 if (!mapping) {
0e8c7d0f 284 /* Anonymous page without mapping */
6c5240ae
CL
285 if (page_count(page) != 1)
286 return -EAGAIN;
287 return 0;
288 }
289
19fd6231 290 spin_lock_irq(&mapping->tree_lock);
b20a3503 291
7cf9c2c7
NP
292 pslot = radix_tree_lookup_slot(&mapping->page_tree,
293 page_index(page));
b20a3503 294
edcf4748 295 expected_count = 2 + page_has_private(page);
e286781d 296 if (page_count(page) != expected_count ||
29c1f677 297 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
19fd6231 298 spin_unlock_irq(&mapping->tree_lock);
e23ca00b 299 return -EAGAIN;
b20a3503
CL
300 }
301
e286781d 302 if (!page_freeze_refs(page, expected_count)) {
19fd6231 303 spin_unlock_irq(&mapping->tree_lock);
e286781d
NP
304 return -EAGAIN;
305 }
306
b969c4ab
MG
307 /*
308 * In the async migration case of moving a page with buffers, lock the
309 * buffers using trylock before the mapping is moved. If the mapping
310 * was moved, we later failed to lock the buffers and could not move
311 * the mapping back due to an elevated page count, we would have to
312 * block waiting on other references to be dropped.
313 */
314 if (!sync && head && !buffer_migrate_lock_buffers(head, sync)) {
315 page_unfreeze_refs(page, expected_count);
316 spin_unlock_irq(&mapping->tree_lock);
317 return -EAGAIN;
318 }
319
b20a3503
CL
320 /*
321 * Now we know that no one else is looking at the page.
b20a3503 322 */
7cf9c2c7 323 get_page(newpage); /* add cache reference */
b20a3503
CL
324 if (PageSwapCache(page)) {
325 SetPageSwapCache(newpage);
326 set_page_private(newpage, page_private(page));
327 }
328
7cf9c2c7
NP
329 radix_tree_replace_slot(pslot, newpage);
330
331 /*
937a94c9
JG
332 * Drop cache reference from old page by unfreezing
333 * to one less reference.
7cf9c2c7
NP
334 * We know this isn't the last reference.
335 */
937a94c9 336 page_unfreeze_refs(page, expected_count - 1);
7cf9c2c7 337
0e8c7d0f
CL
338 /*
339 * If moved to a different zone then also account
340 * the page for that zone. Other VM counters will be
341 * taken care of when we establish references to the
342 * new page and drop references to the old page.
343 *
344 * Note that anonymous pages are accounted for
345 * via NR_FILE_PAGES and NR_ANON_PAGES if they
346 * are mapped to swap space.
347 */
348 __dec_zone_page_state(page, NR_FILE_PAGES);
349 __inc_zone_page_state(newpage, NR_FILE_PAGES);
99a15e21 350 if (!PageSwapCache(page) && PageSwapBacked(page)) {
4b02108a
KM
351 __dec_zone_page_state(page, NR_SHMEM);
352 __inc_zone_page_state(newpage, NR_SHMEM);
353 }
19fd6231 354 spin_unlock_irq(&mapping->tree_lock);
b20a3503
CL
355
356 return 0;
357}
b20a3503 358
290408d4
NH
359/*
360 * The expected number of remaining references is the same as that
361 * of migrate_page_move_mapping().
362 */
363int migrate_huge_page_move_mapping(struct address_space *mapping,
364 struct page *newpage, struct page *page)
365{
366 int expected_count;
367 void **pslot;
368
369 if (!mapping) {
370 if (page_count(page) != 1)
371 return -EAGAIN;
372 return 0;
373 }
374
375 spin_lock_irq(&mapping->tree_lock);
376
377 pslot = radix_tree_lookup_slot(&mapping->page_tree,
378 page_index(page));
379
380 expected_count = 2 + page_has_private(page);
381 if (page_count(page) != expected_count ||
29c1f677 382 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
290408d4
NH
383 spin_unlock_irq(&mapping->tree_lock);
384 return -EAGAIN;
385 }
386
387 if (!page_freeze_refs(page, expected_count)) {
388 spin_unlock_irq(&mapping->tree_lock);
389 return -EAGAIN;
390 }
391
392 get_page(newpage);
393
394 radix_tree_replace_slot(pslot, newpage);
395
937a94c9 396 page_unfreeze_refs(page, expected_count - 1);
290408d4
NH
397
398 spin_unlock_irq(&mapping->tree_lock);
399 return 0;
400}
401
b20a3503
CL
402/*
403 * Copy the page to its new location
404 */
290408d4 405void migrate_page_copy(struct page *newpage, struct page *page)
b20a3503 406{
290408d4
NH
407 if (PageHuge(page))
408 copy_huge_page(newpage, page);
409 else
410 copy_highpage(newpage, page);
b20a3503
CL
411
412 if (PageError(page))
413 SetPageError(newpage);
414 if (PageReferenced(page))
415 SetPageReferenced(newpage);
416 if (PageUptodate(page))
417 SetPageUptodate(newpage);
894bc310
LS
418 if (TestClearPageActive(page)) {
419 VM_BUG_ON(PageUnevictable(page));
b20a3503 420 SetPageActive(newpage);
418b27ef
LS
421 } else if (TestClearPageUnevictable(page))
422 SetPageUnevictable(newpage);
b20a3503
CL
423 if (PageChecked(page))
424 SetPageChecked(newpage);
425 if (PageMappedToDisk(page))
426 SetPageMappedToDisk(newpage);
427
428 if (PageDirty(page)) {
429 clear_page_dirty_for_io(page);
3a902c5f
NP
430 /*
431 * Want to mark the page and the radix tree as dirty, and
432 * redo the accounting that clear_page_dirty_for_io undid,
433 * but we can't use set_page_dirty because that function
434 * is actually a signal that all of the page has become dirty.
25985edc 435 * Whereas only part of our page may be dirty.
3a902c5f
NP
436 */
437 __set_page_dirty_nobuffers(newpage);
b20a3503
CL
438 }
439
b291f000 440 mlock_migrate_page(newpage, page);
e9995ef9 441 ksm_migrate_page(newpage, page);
b291f000 442
b20a3503 443 ClearPageSwapCache(page);
b20a3503
CL
444 ClearPagePrivate(page);
445 set_page_private(page, 0);
446 page->mapping = NULL;
447
448 /*
449 * If any waiters have accumulated on the new page then
450 * wake them up.
451 */
452 if (PageWriteback(newpage))
453 end_page_writeback(newpage);
454}
b20a3503 455
1d8b85cc
CL
456/************************************************************
457 * Migration functions
458 ***********************************************************/
459
460/* Always fail migration. Used for mappings that are not movable */
2d1db3b1
CL
461int fail_migrate_page(struct address_space *mapping,
462 struct page *newpage, struct page *page)
1d8b85cc
CL
463{
464 return -EIO;
465}
466EXPORT_SYMBOL(fail_migrate_page);
467
b20a3503
CL
468/*
469 * Common logic to directly migrate a single page suitable for
266cf658 470 * pages that do not use PagePrivate/PagePrivate2.
b20a3503
CL
471 *
472 * Pages are locked upon entry and exit.
473 */
2d1db3b1 474int migrate_page(struct address_space *mapping,
b969c4ab 475 struct page *newpage, struct page *page, bool sync)
b20a3503
CL
476{
477 int rc;
478
479 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
480
b969c4ab 481 rc = migrate_page_move_mapping(mapping, newpage, page, NULL, sync);
b20a3503
CL
482
483 if (rc)
484 return rc;
485
486 migrate_page_copy(newpage, page);
b20a3503
CL
487 return 0;
488}
489EXPORT_SYMBOL(migrate_page);
490
9361401e 491#ifdef CONFIG_BLOCK
1d8b85cc
CL
492/*
493 * Migration function for pages with buffers. This function can only be used
494 * if the underlying filesystem guarantees that no other references to "page"
495 * exist.
496 */
2d1db3b1 497int buffer_migrate_page(struct address_space *mapping,
b969c4ab 498 struct page *newpage, struct page *page, bool sync)
1d8b85cc 499{
1d8b85cc
CL
500 struct buffer_head *bh, *head;
501 int rc;
502
1d8b85cc 503 if (!page_has_buffers(page))
b969c4ab 504 return migrate_page(mapping, newpage, page, sync);
1d8b85cc
CL
505
506 head = page_buffers(page);
507
b969c4ab 508 rc = migrate_page_move_mapping(mapping, newpage, page, head, sync);
1d8b85cc
CL
509
510 if (rc)
511 return rc;
512
b969c4ab
MG
513 /*
514 * In the async case, migrate_page_move_mapping locked the buffers
515 * with an IRQ-safe spinlock held. In the sync case, the buffers
516 * need to be locked now
517 */
518 if (sync)
519 BUG_ON(!buffer_migrate_lock_buffers(head, sync));
1d8b85cc
CL
520
521 ClearPagePrivate(page);
522 set_page_private(newpage, page_private(page));
523 set_page_private(page, 0);
524 put_page(page);
525 get_page(newpage);
526
527 bh = head;
528 do {
529 set_bh_page(bh, newpage, bh_offset(bh));
530 bh = bh->b_this_page;
531
532 } while (bh != head);
533
534 SetPagePrivate(newpage);
535
536 migrate_page_copy(newpage, page);
537
538 bh = head;
539 do {
540 unlock_buffer(bh);
541 put_bh(bh);
542 bh = bh->b_this_page;
543
544 } while (bh != head);
545
546 return 0;
547}
548EXPORT_SYMBOL(buffer_migrate_page);
9361401e 549#endif
1d8b85cc 550
04e62a29
CL
551/*
552 * Writeback a page to clean the dirty state
553 */
554static int writeout(struct address_space *mapping, struct page *page)
8351a6e4 555{
04e62a29
CL
556 struct writeback_control wbc = {
557 .sync_mode = WB_SYNC_NONE,
558 .nr_to_write = 1,
559 .range_start = 0,
560 .range_end = LLONG_MAX,
04e62a29
CL
561 .for_reclaim = 1
562 };
563 int rc;
564
565 if (!mapping->a_ops->writepage)
566 /* No write method for the address space */
567 return -EINVAL;
568
569 if (!clear_page_dirty_for_io(page))
570 /* Someone else already triggered a write */
571 return -EAGAIN;
572
8351a6e4 573 /*
04e62a29
CL
574 * A dirty page may imply that the underlying filesystem has
575 * the page on some queue. So the page must be clean for
576 * migration. Writeout may mean we loose the lock and the
577 * page state is no longer what we checked for earlier.
578 * At this point we know that the migration attempt cannot
579 * be successful.
8351a6e4 580 */
04e62a29 581 remove_migration_ptes(page, page);
8351a6e4 582
04e62a29 583 rc = mapping->a_ops->writepage(page, &wbc);
8351a6e4 584
04e62a29
CL
585 if (rc != AOP_WRITEPAGE_ACTIVATE)
586 /* unlocked. Relock */
587 lock_page(page);
588
bda8550d 589 return (rc < 0) ? -EIO : -EAGAIN;
04e62a29
CL
590}
591
592/*
593 * Default handling if a filesystem does not provide a migration function.
594 */
595static int fallback_migrate_page(struct address_space *mapping,
b969c4ab 596 struct page *newpage, struct page *page, bool sync)
04e62a29 597{
b969c4ab
MG
598 if (PageDirty(page)) {
599 if (!sync)
600 return -EBUSY;
04e62a29 601 return writeout(mapping, page);
b969c4ab 602 }
8351a6e4
CL
603
604 /*
605 * Buffers may be managed in a filesystem specific way.
606 * We must have no buffers or drop them.
607 */
266cf658 608 if (page_has_private(page) &&
8351a6e4
CL
609 !try_to_release_page(page, GFP_KERNEL))
610 return -EAGAIN;
611
b969c4ab 612 return migrate_page(mapping, newpage, page, sync);
8351a6e4
CL
613}
614
e24f0b8f
CL
615/*
616 * Move a page to a newly allocated page
617 * The page is locked and all ptes have been successfully removed.
618 *
619 * The new page will have replaced the old page if this function
620 * is successful.
894bc310
LS
621 *
622 * Return value:
623 * < 0 - error code
624 * == 0 - success
e24f0b8f 625 */
3fe2011f 626static int move_to_new_page(struct page *newpage, struct page *page,
11bc82d6 627 int remap_swapcache, bool sync)
e24f0b8f
CL
628{
629 struct address_space *mapping;
630 int rc;
631
632 /*
633 * Block others from accessing the page when we get around to
634 * establishing additional references. We are the only one
635 * holding a reference to the new page at this point.
636 */
529ae9aa 637 if (!trylock_page(newpage))
e24f0b8f
CL
638 BUG();
639
640 /* Prepare mapping for the new page.*/
641 newpage->index = page->index;
642 newpage->mapping = page->mapping;
b2e18538
RR
643 if (PageSwapBacked(page))
644 SetPageSwapBacked(newpage);
e24f0b8f
CL
645
646 mapping = page_mapping(page);
647 if (!mapping)
b969c4ab
MG
648 rc = migrate_page(mapping, newpage, page, sync);
649 else if (mapping->a_ops->migratepage)
e24f0b8f 650 /*
b969c4ab
MG
651 * Most pages have a mapping and most filesystems provide a
652 * migratepage callback. Anonymous pages are part of swap
653 * space which also has its own migratepage callback. This
654 * is the most common path for page migration.
e24f0b8f 655 */
b969c4ab
MG
656 rc = mapping->a_ops->migratepage(mapping,
657 newpage, page, sync);
658 else
659 rc = fallback_migrate_page(mapping, newpage, page, sync);
e24f0b8f 660
3fe2011f 661 if (rc) {
e24f0b8f 662 newpage->mapping = NULL;
3fe2011f
MG
663 } else {
664 if (remap_swapcache)
665 remove_migration_ptes(page, newpage);
666 }
e24f0b8f
CL
667
668 unlock_page(newpage);
669
670 return rc;
671}
672
0dabec93
MK
673static int __unmap_and_move(struct page *page, struct page *newpage,
674 int force, bool offlining, bool sync)
e24f0b8f 675{
0dabec93 676 int rc = -EAGAIN;
3fe2011f 677 int remap_swapcache = 1;
ae41be37 678 int charge = 0;
56039efa 679 struct mem_cgroup *mem;
3f6c8272 680 struct anon_vma *anon_vma = NULL;
95a402c3 681
529ae9aa 682 if (!trylock_page(page)) {
11bc82d6 683 if (!force || !sync)
0dabec93 684 goto out;
3e7d3449
MG
685
686 /*
687 * It's not safe for direct compaction to call lock_page.
688 * For example, during page readahead pages are added locked
689 * to the LRU. Later, when the IO completes the pages are
690 * marked uptodate and unlocked. However, the queueing
691 * could be merging multiple pages for one bio (e.g.
692 * mpage_readpages). If an allocation happens for the
693 * second or third page, the process can end up locking
694 * the same page twice and deadlocking. Rather than
695 * trying to be clever about what pages can be locked,
696 * avoid the use of lock_page for direct compaction
697 * altogether.
698 */
699 if (current->flags & PF_MEMALLOC)
0dabec93 700 goto out;
3e7d3449 701
e24f0b8f
CL
702 lock_page(page);
703 }
704
62b61f61
HD
705 /*
706 * Only memory hotplug's offline_pages() caller has locked out KSM,
707 * and can safely migrate a KSM page. The other cases have skipped
708 * PageKsm along with PageReserved - but it is only now when we have
709 * the page lock that we can be certain it will not go KSM beneath us
710 * (KSM will not upgrade a page from PageAnon to PageKsm when it sees
711 * its pagecount raised, but only here do we take the page lock which
712 * serializes that).
713 */
714 if (PageKsm(page) && !offlining) {
715 rc = -EBUSY;
716 goto unlock;
717 }
718
01b1ae63 719 /* charge against new page */
ef6a3c63 720 charge = mem_cgroup_prepare_migration(page, newpage, &mem, GFP_KERNEL);
01b1ae63
KH
721 if (charge == -ENOMEM) {
722 rc = -ENOMEM;
723 goto unlock;
724 }
725 BUG_ON(charge);
726
e24f0b8f 727 if (PageWriteback(page)) {
11bc82d6
AA
728 /*
729 * For !sync, there is no point retrying as the retry loop
730 * is expected to be too short for PageWriteback to be cleared
731 */
732 if (!sync) {
733 rc = -EBUSY;
734 goto uncharge;
735 }
736 if (!force)
01b1ae63 737 goto uncharge;
e24f0b8f
CL
738 wait_on_page_writeback(page);
739 }
e24f0b8f 740 /*
dc386d4d
KH
741 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
742 * we cannot notice that anon_vma is freed while we migrates a page.
1ce82b69 743 * This get_anon_vma() delays freeing anon_vma pointer until the end
dc386d4d 744 * of migration. File cache pages are no problem because of page_lock()
989f89c5
KH
745 * File Caches may use write_page() or lock_page() in migration, then,
746 * just care Anon page here.
dc386d4d 747 */
989f89c5 748 if (PageAnon(page)) {
1ce82b69
HD
749 /*
750 * Only page_lock_anon_vma() understands the subtleties of
751 * getting a hold on an anon_vma from outside one of its mms.
752 */
746b18d4 753 anon_vma = page_get_anon_vma(page);
1ce82b69
HD
754 if (anon_vma) {
755 /*
746b18d4 756 * Anon page
1ce82b69 757 */
1ce82b69 758 } else if (PageSwapCache(page)) {
3fe2011f
MG
759 /*
760 * We cannot be sure that the anon_vma of an unmapped
761 * swapcache page is safe to use because we don't
762 * know in advance if the VMA that this page belonged
763 * to still exists. If the VMA and others sharing the
764 * data have been freed, then the anon_vma could
765 * already be invalid.
766 *
767 * To avoid this possibility, swapcache pages get
768 * migrated but are not remapped when migration
769 * completes
770 */
771 remap_swapcache = 0;
772 } else {
1ce82b69 773 goto uncharge;
3fe2011f 774 }
989f89c5 775 }
62e1c553 776
dc386d4d 777 /*
62e1c553
SL
778 * Corner case handling:
779 * 1. When a new swap-cache page is read into, it is added to the LRU
780 * and treated as swapcache but it has no rmap yet.
781 * Calling try_to_unmap() against a page->mapping==NULL page will
782 * trigger a BUG. So handle it here.
783 * 2. An orphaned page (see truncate_complete_page) might have
784 * fs-private metadata. The page can be picked up due to memory
785 * offlining. Everywhere else except page reclaim, the page is
786 * invisible to the vm, so the page can not be migrated. So try to
787 * free the metadata, so the page can be freed.
e24f0b8f 788 */
62e1c553 789 if (!page->mapping) {
1ce82b69
HD
790 VM_BUG_ON(PageAnon(page));
791 if (page_has_private(page)) {
62e1c553 792 try_to_free_buffers(page);
1ce82b69 793 goto uncharge;
62e1c553 794 }
abfc3488 795 goto skip_unmap;
62e1c553
SL
796 }
797
dc386d4d 798 /* Establish migration ptes or remove ptes */
14fa31b8 799 try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
dc386d4d 800
abfc3488 801skip_unmap:
e6a1530d 802 if (!page_mapped(page))
11bc82d6 803 rc = move_to_new_page(newpage, page, remap_swapcache, sync);
e24f0b8f 804
3fe2011f 805 if (rc && remap_swapcache)
e24f0b8f 806 remove_migration_ptes(page, page);
3f6c8272
MG
807
808 /* Drop an anon_vma reference if we took one */
76545066 809 if (anon_vma)
9e60109f 810 put_anon_vma(anon_vma);
3f6c8272 811
01b1ae63
KH
812uncharge:
813 if (!charge)
50de1dd9 814 mem_cgroup_end_migration(mem, page, newpage, rc == 0);
e24f0b8f
CL
815unlock:
816 unlock_page(page);
0dabec93
MK
817out:
818 return rc;
819}
95a402c3 820
0dabec93
MK
821/*
822 * Obtain the lock on page, remove all ptes and migrate the page
823 * to the newly allocated page in newpage.
824 */
825static int unmap_and_move(new_page_t get_new_page, unsigned long private,
826 struct page *page, int force, bool offlining, bool sync)
827{
828 int rc = 0;
829 int *result = NULL;
830 struct page *newpage = get_new_page(page, private, &result);
831
832 if (!newpage)
833 return -ENOMEM;
834
4e5f01c2
KH
835 mem_cgroup_reset_owner(newpage);
836
0dabec93
MK
837 if (page_count(page) == 1) {
838 /* page was freed from under us. So we are done. */
839 goto out;
840 }
841
842 if (unlikely(PageTransHuge(page)))
843 if (unlikely(split_huge_page(page)))
844 goto out;
845
846 rc = __unmap_and_move(page, newpage, force, offlining, sync);
847out:
e24f0b8f 848 if (rc != -EAGAIN) {
0dabec93
MK
849 /*
850 * A page that has been migrated has all references
851 * removed and will be freed. A page that has not been
852 * migrated will have kepts its references and be
853 * restored.
854 */
855 list_del(&page->lru);
a731286d 856 dec_zone_page_state(page, NR_ISOLATED_ANON +
6c0b1351 857 page_is_file_cache(page));
894bc310 858 putback_lru_page(page);
e24f0b8f 859 }
95a402c3
CL
860 /*
861 * Move the new page to the LRU. If migration was not successful
862 * then this will free the page.
863 */
894bc310 864 putback_lru_page(newpage);
742755a1
CL
865 if (result) {
866 if (rc)
867 *result = rc;
868 else
869 *result = page_to_nid(newpage);
870 }
e24f0b8f
CL
871 return rc;
872}
873
290408d4
NH
874/*
875 * Counterpart of unmap_and_move_page() for hugepage migration.
876 *
877 * This function doesn't wait the completion of hugepage I/O
878 * because there is no race between I/O and migration for hugepage.
879 * Note that currently hugepage I/O occurs only in direct I/O
880 * where no lock is held and PG_writeback is irrelevant,
881 * and writeback status of all subpages are counted in the reference
882 * count of the head page (i.e. if all subpages of a 2MB hugepage are
883 * under direct I/O, the reference of the head page is 512 and a bit more.)
884 * This means that when we try to migrate hugepage whose subpages are
885 * doing direct I/O, some references remain after try_to_unmap() and
886 * hugepage migration fails without data corruption.
887 *
888 * There is also no race when direct I/O is issued on the page under migration,
889 * because then pte is replaced with migration swap entry and direct I/O code
890 * will wait in the page fault for migration to complete.
891 */
892static int unmap_and_move_huge_page(new_page_t get_new_page,
893 unsigned long private, struct page *hpage,
7f0f2496 894 int force, bool offlining, bool sync)
290408d4
NH
895{
896 int rc = 0;
897 int *result = NULL;
898 struct page *new_hpage = get_new_page(hpage, private, &result);
290408d4
NH
899 struct anon_vma *anon_vma = NULL;
900
901 if (!new_hpage)
902 return -ENOMEM;
903
904 rc = -EAGAIN;
905
906 if (!trylock_page(hpage)) {
77f1fe6b 907 if (!force || !sync)
290408d4
NH
908 goto out;
909 lock_page(hpage);
910 }
911
746b18d4
PZ
912 if (PageAnon(hpage))
913 anon_vma = page_get_anon_vma(hpage);
290408d4
NH
914
915 try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
916
917 if (!page_mapped(hpage))
11bc82d6 918 rc = move_to_new_page(new_hpage, hpage, 1, sync);
290408d4
NH
919
920 if (rc)
921 remove_migration_ptes(hpage, hpage);
922
fd4a4663 923 if (anon_vma)
9e60109f 924 put_anon_vma(anon_vma);
290408d4
NH
925 unlock_page(hpage);
926
09761333 927out:
290408d4
NH
928 if (rc != -EAGAIN) {
929 list_del(&hpage->lru);
930 put_page(hpage);
931 }
932
933 put_page(new_hpage);
934
935 if (result) {
936 if (rc)
937 *result = rc;
938 else
939 *result = page_to_nid(new_hpage);
940 }
941 return rc;
942}
943
b20a3503
CL
944/*
945 * migrate_pages
946 *
95a402c3
CL
947 * The function takes one list of pages to migrate and a function
948 * that determines from the page to be migrated and the private data
949 * the target of the move and allocates the page.
b20a3503
CL
950 *
951 * The function returns after 10 attempts or if no pages
952 * are movable anymore because to has become empty
cf608ac1
MK
953 * or no retryable pages exist anymore.
954 * Caller should call putback_lru_pages to return pages to the LRU
28bd6578 955 * or free list only if ret != 0.
b20a3503 956 *
95a402c3 957 * Return: Number of pages not migrated or error code.
b20a3503 958 */
95a402c3 959int migrate_pages(struct list_head *from,
7f0f2496 960 new_page_t get_new_page, unsigned long private, bool offlining,
77f1fe6b 961 bool sync)
b20a3503 962{
e24f0b8f 963 int retry = 1;
b20a3503
CL
964 int nr_failed = 0;
965 int pass = 0;
966 struct page *page;
967 struct page *page2;
968 int swapwrite = current->flags & PF_SWAPWRITE;
969 int rc;
970
971 if (!swapwrite)
972 current->flags |= PF_SWAPWRITE;
973
e24f0b8f
CL
974 for(pass = 0; pass < 10 && retry; pass++) {
975 retry = 0;
b20a3503 976
e24f0b8f 977 list_for_each_entry_safe(page, page2, from, lru) {
e24f0b8f 978 cond_resched();
2d1db3b1 979
95a402c3 980 rc = unmap_and_move(get_new_page, private,
77f1fe6b
MG
981 page, pass > 2, offlining,
982 sync);
2d1db3b1 983
e24f0b8f 984 switch(rc) {
95a402c3
CL
985 case -ENOMEM:
986 goto out;
e24f0b8f 987 case -EAGAIN:
2d1db3b1 988 retry++;
e24f0b8f
CL
989 break;
990 case 0:
e24f0b8f
CL
991 break;
992 default:
2d1db3b1 993 /* Permanent failure */
2d1db3b1 994 nr_failed++;
e24f0b8f 995 break;
2d1db3b1 996 }
b20a3503
CL
997 }
998 }
95a402c3
CL
999 rc = 0;
1000out:
b20a3503
CL
1001 if (!swapwrite)
1002 current->flags &= ~PF_SWAPWRITE;
1003
95a402c3
CL
1004 if (rc)
1005 return rc;
b20a3503 1006
95a402c3 1007 return nr_failed + retry;
b20a3503 1008}
95a402c3 1009
290408d4 1010int migrate_huge_pages(struct list_head *from,
7f0f2496 1011 new_page_t get_new_page, unsigned long private, bool offlining,
77f1fe6b 1012 bool sync)
290408d4
NH
1013{
1014 int retry = 1;
1015 int nr_failed = 0;
1016 int pass = 0;
1017 struct page *page;
1018 struct page *page2;
1019 int rc;
1020
1021 for (pass = 0; pass < 10 && retry; pass++) {
1022 retry = 0;
1023
1024 list_for_each_entry_safe(page, page2, from, lru) {
1025 cond_resched();
1026
1027 rc = unmap_and_move_huge_page(get_new_page,
77f1fe6b
MG
1028 private, page, pass > 2, offlining,
1029 sync);
290408d4
NH
1030
1031 switch(rc) {
1032 case -ENOMEM:
1033 goto out;
1034 case -EAGAIN:
1035 retry++;
1036 break;
1037 case 0:
1038 break;
1039 default:
1040 /* Permanent failure */
1041 nr_failed++;
1042 break;
1043 }
1044 }
1045 }
1046 rc = 0;
1047out:
290408d4
NH
1048 if (rc)
1049 return rc;
1050
1051 return nr_failed + retry;
1052}
1053
742755a1
CL
1054#ifdef CONFIG_NUMA
1055/*
1056 * Move a list of individual pages
1057 */
1058struct page_to_node {
1059 unsigned long addr;
1060 struct page *page;
1061 int node;
1062 int status;
1063};
1064
1065static struct page *new_page_node(struct page *p, unsigned long private,
1066 int **result)
1067{
1068 struct page_to_node *pm = (struct page_to_node *)private;
1069
1070 while (pm->node != MAX_NUMNODES && pm->page != p)
1071 pm++;
1072
1073 if (pm->node == MAX_NUMNODES)
1074 return NULL;
1075
1076 *result = &pm->status;
1077
6484eb3e 1078 return alloc_pages_exact_node(pm->node,
769848c0 1079 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
742755a1
CL
1080}
1081
1082/*
1083 * Move a set of pages as indicated in the pm array. The addr
1084 * field must be set to the virtual address of the page to be moved
1085 * and the node number must contain a valid target node.
5e9a0f02 1086 * The pm array ends with node = MAX_NUMNODES.
742755a1 1087 */
5e9a0f02
BG
1088static int do_move_page_to_node_array(struct mm_struct *mm,
1089 struct page_to_node *pm,
1090 int migrate_all)
742755a1
CL
1091{
1092 int err;
1093 struct page_to_node *pp;
1094 LIST_HEAD(pagelist);
1095
1096 down_read(&mm->mmap_sem);
1097
1098 /*
1099 * Build a list of pages to migrate
1100 */
742755a1
CL
1101 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
1102 struct vm_area_struct *vma;
1103 struct page *page;
1104
742755a1
CL
1105 err = -EFAULT;
1106 vma = find_vma(mm, pp->addr);
70384dc6 1107 if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
742755a1
CL
1108 goto set_status;
1109
500d65d4 1110 page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT);
89f5b7da
LT
1111
1112 err = PTR_ERR(page);
1113 if (IS_ERR(page))
1114 goto set_status;
1115
742755a1
CL
1116 err = -ENOENT;
1117 if (!page)
1118 goto set_status;
1119
62b61f61
HD
1120 /* Use PageReserved to check for zero page */
1121 if (PageReserved(page) || PageKsm(page))
742755a1
CL
1122 goto put_and_set;
1123
1124 pp->page = page;
1125 err = page_to_nid(page);
1126
1127 if (err == pp->node)
1128 /*
1129 * Node already in the right place
1130 */
1131 goto put_and_set;
1132
1133 err = -EACCES;
1134 if (page_mapcount(page) > 1 &&
1135 !migrate_all)
1136 goto put_and_set;
1137
62695a84 1138 err = isolate_lru_page(page);
6d9c285a 1139 if (!err) {
62695a84 1140 list_add_tail(&page->lru, &pagelist);
6d9c285a
KM
1141 inc_zone_page_state(page, NR_ISOLATED_ANON +
1142 page_is_file_cache(page));
1143 }
742755a1
CL
1144put_and_set:
1145 /*
1146 * Either remove the duplicate refcount from
1147 * isolate_lru_page() or drop the page ref if it was
1148 * not isolated.
1149 */
1150 put_page(page);
1151set_status:
1152 pp->status = err;
1153 }
1154
e78bbfa8 1155 err = 0;
cf608ac1 1156 if (!list_empty(&pagelist)) {
742755a1 1157 err = migrate_pages(&pagelist, new_page_node,
77f1fe6b 1158 (unsigned long)pm, 0, true);
cf608ac1
MK
1159 if (err)
1160 putback_lru_pages(&pagelist);
1161 }
742755a1
CL
1162
1163 up_read(&mm->mmap_sem);
1164 return err;
1165}
1166
5e9a0f02
BG
1167/*
1168 * Migrate an array of page address onto an array of nodes and fill
1169 * the corresponding array of status.
1170 */
1171static int do_pages_move(struct mm_struct *mm, struct task_struct *task,
1172 unsigned long nr_pages,
1173 const void __user * __user *pages,
1174 const int __user *nodes,
1175 int __user *status, int flags)
1176{
3140a227 1177 struct page_to_node *pm;
5e9a0f02 1178 nodemask_t task_nodes;
3140a227
BG
1179 unsigned long chunk_nr_pages;
1180 unsigned long chunk_start;
1181 int err;
5e9a0f02
BG
1182
1183 task_nodes = cpuset_mems_allowed(task);
1184
3140a227
BG
1185 err = -ENOMEM;
1186 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
1187 if (!pm)
5e9a0f02 1188 goto out;
35282a2d
BG
1189
1190 migrate_prep();
1191
5e9a0f02 1192 /*
3140a227
BG
1193 * Store a chunk of page_to_node array in a page,
1194 * but keep the last one as a marker
5e9a0f02 1195 */
3140a227 1196 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
5e9a0f02 1197
3140a227
BG
1198 for (chunk_start = 0;
1199 chunk_start < nr_pages;
1200 chunk_start += chunk_nr_pages) {
1201 int j;
5e9a0f02 1202
3140a227
BG
1203 if (chunk_start + chunk_nr_pages > nr_pages)
1204 chunk_nr_pages = nr_pages - chunk_start;
1205
1206 /* fill the chunk pm with addrs and nodes from user-space */
1207 for (j = 0; j < chunk_nr_pages; j++) {
1208 const void __user *p;
5e9a0f02
BG
1209 int node;
1210
3140a227
BG
1211 err = -EFAULT;
1212 if (get_user(p, pages + j + chunk_start))
1213 goto out_pm;
1214 pm[j].addr = (unsigned long) p;
1215
1216 if (get_user(node, nodes + j + chunk_start))
5e9a0f02
BG
1217 goto out_pm;
1218
1219 err = -ENODEV;
6f5a55f1
LT
1220 if (node < 0 || node >= MAX_NUMNODES)
1221 goto out_pm;
1222
5e9a0f02
BG
1223 if (!node_state(node, N_HIGH_MEMORY))
1224 goto out_pm;
1225
1226 err = -EACCES;
1227 if (!node_isset(node, task_nodes))
1228 goto out_pm;
1229
3140a227
BG
1230 pm[j].node = node;
1231 }
1232
1233 /* End marker for this chunk */
1234 pm[chunk_nr_pages].node = MAX_NUMNODES;
1235
1236 /* Migrate this chunk */
1237 err = do_move_page_to_node_array(mm, pm,
1238 flags & MPOL_MF_MOVE_ALL);
1239 if (err < 0)
1240 goto out_pm;
5e9a0f02 1241
5e9a0f02 1242 /* Return status information */
3140a227
BG
1243 for (j = 0; j < chunk_nr_pages; j++)
1244 if (put_user(pm[j].status, status + j + chunk_start)) {
5e9a0f02 1245 err = -EFAULT;
3140a227
BG
1246 goto out_pm;
1247 }
1248 }
1249 err = 0;
5e9a0f02
BG
1250
1251out_pm:
3140a227 1252 free_page((unsigned long)pm);
5e9a0f02
BG
1253out:
1254 return err;
1255}
1256
742755a1 1257/*
2f007e74 1258 * Determine the nodes of an array of pages and store it in an array of status.
742755a1 1259 */
80bba129
BG
1260static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1261 const void __user **pages, int *status)
742755a1 1262{
2f007e74 1263 unsigned long i;
2f007e74 1264
742755a1
CL
1265 down_read(&mm->mmap_sem);
1266
2f007e74 1267 for (i = 0; i < nr_pages; i++) {
80bba129 1268 unsigned long addr = (unsigned long)(*pages);
742755a1
CL
1269 struct vm_area_struct *vma;
1270 struct page *page;
c095adbc 1271 int err = -EFAULT;
2f007e74
BG
1272
1273 vma = find_vma(mm, addr);
70384dc6 1274 if (!vma || addr < vma->vm_start)
742755a1
CL
1275 goto set_status;
1276
2f007e74 1277 page = follow_page(vma, addr, 0);
89f5b7da
LT
1278
1279 err = PTR_ERR(page);
1280 if (IS_ERR(page))
1281 goto set_status;
1282
742755a1
CL
1283 err = -ENOENT;
1284 /* Use PageReserved to check for zero page */
62b61f61 1285 if (!page || PageReserved(page) || PageKsm(page))
742755a1
CL
1286 goto set_status;
1287
1288 err = page_to_nid(page);
1289set_status:
80bba129
BG
1290 *status = err;
1291
1292 pages++;
1293 status++;
1294 }
1295
1296 up_read(&mm->mmap_sem);
1297}
1298
1299/*
1300 * Determine the nodes of a user array of pages and store it in
1301 * a user array of status.
1302 */
1303static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1304 const void __user * __user *pages,
1305 int __user *status)
1306{
1307#define DO_PAGES_STAT_CHUNK_NR 16
1308 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1309 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
80bba129 1310
87b8d1ad
PA
1311 while (nr_pages) {
1312 unsigned long chunk_nr;
80bba129 1313
87b8d1ad
PA
1314 chunk_nr = nr_pages;
1315 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1316 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1317
1318 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
1319 break;
80bba129
BG
1320
1321 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1322
87b8d1ad
PA
1323 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1324 break;
742755a1 1325
87b8d1ad
PA
1326 pages += chunk_nr;
1327 status += chunk_nr;
1328 nr_pages -= chunk_nr;
1329 }
1330 return nr_pages ? -EFAULT : 0;
742755a1
CL
1331}
1332
1333/*
1334 * Move a list of pages in the address space of the currently executing
1335 * process.
1336 */
938bb9f5
HC
1337SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1338 const void __user * __user *, pages,
1339 const int __user *, nodes,
1340 int __user *, status, int, flags)
742755a1 1341{
c69e8d9c 1342 const struct cred *cred = current_cred(), *tcred;
742755a1 1343 struct task_struct *task;
742755a1 1344 struct mm_struct *mm;
5e9a0f02 1345 int err;
742755a1
CL
1346
1347 /* Check flags */
1348 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1349 return -EINVAL;
1350
1351 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1352 return -EPERM;
1353
1354 /* Find the mm_struct */
a879bf58 1355 rcu_read_lock();
228ebcbe 1356 task = pid ? find_task_by_vpid(pid) : current;
742755a1 1357 if (!task) {
a879bf58 1358 rcu_read_unlock();
742755a1
CL
1359 return -ESRCH;
1360 }
1361 mm = get_task_mm(task);
a879bf58 1362 rcu_read_unlock();
742755a1
CL
1363
1364 if (!mm)
1365 return -EINVAL;
1366
1367 /*
1368 * Check if this process has the right to modify the specified
1369 * process. The right exists if the process has administrative
1370 * capabilities, superuser privileges or the same
1371 * userid as the target process.
1372 */
c69e8d9c
DH
1373 rcu_read_lock();
1374 tcred = __task_cred(task);
b6dff3ec
DH
1375 if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
1376 cred->uid != tcred->suid && cred->uid != tcred->uid &&
742755a1 1377 !capable(CAP_SYS_NICE)) {
c69e8d9c 1378 rcu_read_unlock();
742755a1 1379 err = -EPERM;
5e9a0f02 1380 goto out;
742755a1 1381 }
c69e8d9c 1382 rcu_read_unlock();
742755a1 1383
86c3a764
DQ
1384 err = security_task_movememory(task);
1385 if (err)
5e9a0f02 1386 goto out;
86c3a764 1387
5e9a0f02
BG
1388 if (nodes) {
1389 err = do_pages_move(mm, task, nr_pages, pages, nodes, status,
1390 flags);
1391 } else {
2f007e74 1392 err = do_pages_stat(mm, nr_pages, pages, status);
742755a1
CL
1393 }
1394
742755a1 1395out:
742755a1
CL
1396 mmput(mm);
1397 return err;
1398}
742755a1 1399
7b2259b3
CL
1400/*
1401 * Call migration functions in the vma_ops that may prepare
1402 * memory in a vm for migration. migration functions may perform
1403 * the migration for vmas that do not have an underlying page struct.
1404 */
1405int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1406 const nodemask_t *from, unsigned long flags)
1407{
1408 struct vm_area_struct *vma;
1409 int err = 0;
1410
1001c9fb 1411 for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
7b2259b3
CL
1412 if (vma->vm_ops && vma->vm_ops->migrate) {
1413 err = vma->vm_ops->migrate(vma, to, from, flags);
1414 if (err)
1415 break;
1416 }
1417 }
1418 return err;
1419}
83d1674a 1420#endif
This page took 0.556088 seconds and 5 git commands to generate.