migrate.c 34 KB

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