migrate.c 35 KB

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