migrate.c 38 KB

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