migrate.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754
  1. /*
  2. * Memory Migration functionality - linux/mm/migration.c
  3. *
  4. * Copyright (C) 2006 Silicon Graphics, Inc., Christoph Lameter
  5. *
  6. * Page migration was first developed in the context of the memory hotplug
  7. * project. The main authors of the migration code are:
  8. *
  9. * IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
  10. * Hirokazu Takahashi <taka@valinux.co.jp>
  11. * Dave Hansen <haveblue@us.ibm.com>
  12. * Christoph Lameter
  13. */
  14. #include <linux/migrate.h>
  15. #include <linux/export.h>
  16. #include <linux/swap.h>
  17. #include <linux/swapops.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/buffer_head.h>
  20. #include <linux/mm_inline.h>
  21. #include <linux/nsproxy.h>
  22. #include <linux/pagevec.h>
  23. #include <linux/ksm.h>
  24. #include <linux/rmap.h>
  25. #include <linux/topology.h>
  26. #include <linux/cpu.h>
  27. #include <linux/cpuset.h>
  28. #include <linux/writeback.h>
  29. #include <linux/mempolicy.h>
  30. #include <linux/vmalloc.h>
  31. #include <linux/security.h>
  32. #include <linux/memcontrol.h>
  33. #include <linux/syscalls.h>
  34. #include <linux/hugetlb.h>
  35. #include <linux/hugetlb_cgroup.h>
  36. #include <linux/gfp.h>
  37. #include <linux/balloon_compaction.h>
  38. #include <asm/tlbflush.h>
  39. #define CREATE_TRACE_POINTS
  40. #include <trace/events/migrate.h>
  41. #include "internal.h"
  42. /*
  43. * migrate_prep() needs to be called before we start compiling a list of pages
  44. * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is
  45. * undesirable, use migrate_prep_local()
  46. */
  47. int migrate_prep(void)
  48. {
  49. /*
  50. * Clear the LRU lists so pages can be isolated.
  51. * Note that pages may be moved off the LRU after we have
  52. * drained them. Those pages will fail to migrate like other
  53. * pages that may be busy.
  54. */
  55. lru_add_drain_all();
  56. return 0;
  57. }
  58. /* Do the necessary work of migrate_prep but not if it involves other CPUs */
  59. int migrate_prep_local(void)
  60. {
  61. lru_add_drain();
  62. return 0;
  63. }
  64. /*
  65. * Add isolated pages on the list back to the LRU under page lock
  66. * to avoid leaking evictable pages back onto unevictable list.
  67. */
  68. void putback_lru_pages(struct list_head *l)
  69. {
  70. struct page *page;
  71. struct page *page2;
  72. list_for_each_entry_safe(page, page2, l, lru) {
  73. list_del(&page->lru);
  74. dec_zone_page_state(page, NR_ISOLATED_ANON +
  75. page_is_file_cache(page));
  76. putback_lru_page(page);
  77. }
  78. }
  79. /*
  80. * Put previously isolated pages back onto the appropriate lists
  81. * from where they were once taken off for compaction/migration.
  82. *
  83. * This function shall be used instead of putback_lru_pages(),
  84. * whenever the isolated pageset has been built by isolate_migratepages_range()
  85. */
  86. void putback_movable_pages(struct list_head *l)
  87. {
  88. struct page *page;
  89. struct page *page2;
  90. list_for_each_entry_safe(page, page2, l, lru) {
  91. list_del(&page->lru);
  92. dec_zone_page_state(page, NR_ISOLATED_ANON +
  93. page_is_file_cache(page));
  94. if (unlikely(balloon_page_movable(page)))
  95. balloon_page_putback(page);
  96. else
  97. putback_lru_page(page);
  98. }
  99. }
  100. /*
  101. * Restore a potential migration pte to a working pte entry
  102. */
  103. static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,
  104. unsigned long addr, void *old)
  105. {
  106. struct mm_struct *mm = vma->vm_mm;
  107. swp_entry_t entry;
  108. pmd_t *pmd;
  109. pte_t *ptep, pte;
  110. spinlock_t *ptl;
  111. if (unlikely(PageHuge(new))) {
  112. ptep = huge_pte_offset(mm, addr);
  113. if (!ptep)
  114. goto out;
  115. ptl = &mm->page_table_lock;
  116. } else {
  117. pmd = mm_find_pmd(mm, addr);
  118. if (!pmd)
  119. goto out;
  120. if (pmd_trans_huge(*pmd))
  121. goto out;
  122. ptep = pte_offset_map(pmd, addr);
  123. /*
  124. * Peek to check is_swap_pte() before taking ptlock? No, we
  125. * can race mremap's move_ptes(), which skips anon_vma lock.
  126. */
  127. ptl = pte_lockptr(mm, pmd);
  128. }
  129. spin_lock(ptl);
  130. pte = *ptep;
  131. if (!is_swap_pte(pte))
  132. goto unlock;
  133. entry = pte_to_swp_entry(pte);
  134. if (!is_migration_entry(entry) ||
  135. migration_entry_to_page(entry) != old)
  136. goto unlock;
  137. get_page(new);
  138. pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
  139. if (is_write_migration_entry(entry))
  140. pte = pte_mkwrite(pte);
  141. #ifdef CONFIG_HUGETLB_PAGE
  142. if (PageHuge(new)) {
  143. pte = pte_mkhuge(pte);
  144. pte = arch_make_huge_pte(pte, vma, new, 0);
  145. }
  146. #endif
  147. flush_cache_page(vma, addr, pte_pfn(pte));
  148. set_pte_at(mm, addr, ptep, pte);
  149. if (PageHuge(new)) {
  150. if (PageAnon(new))
  151. hugepage_add_anon_rmap(new, vma, addr);
  152. else
  153. page_dup_rmap(new);
  154. } else if (PageAnon(new))
  155. page_add_anon_rmap(new, vma, addr);
  156. else
  157. page_add_file_rmap(new);
  158. /* No need to invalidate - it was non-present before */
  159. update_mmu_cache(vma, addr, ptep);
  160. unlock:
  161. pte_unmap_unlock(ptep, ptl);
  162. out:
  163. return SWAP_AGAIN;
  164. }
  165. /*
  166. * Get rid of all migration entries and replace them by
  167. * references to the indicated page.
  168. */
  169. static void remove_migration_ptes(struct page *old, struct page *new)
  170. {
  171. rmap_walk(new, remove_migration_pte, old);
  172. }
  173. /*
  174. * Something used the pte of a page under migration. We need to
  175. * get to the page and wait until migration is finished.
  176. * When we return from this function the fault will be retried.
  177. */
  178. void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
  179. unsigned long address)
  180. {
  181. pte_t *ptep, pte;
  182. spinlock_t *ptl;
  183. swp_entry_t entry;
  184. struct page *page;
  185. ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
  186. pte = *ptep;
  187. if (!is_swap_pte(pte))
  188. goto out;
  189. entry = pte_to_swp_entry(pte);
  190. if (!is_migration_entry(entry))
  191. goto out;
  192. page = migration_entry_to_page(entry);
  193. /*
  194. * Once radix-tree replacement of page migration started, page_count
  195. * *must* be zero. And, we don't want to call wait_on_page_locked()
  196. * against a page without get_page().
  197. * So, we use get_page_unless_zero(), here. Even failed, page fault
  198. * will occur again.
  199. */
  200. if (!get_page_unless_zero(page))
  201. goto out;
  202. pte_unmap_unlock(ptep, ptl);
  203. wait_on_page_locked(page);
  204. put_page(page);
  205. return;
  206. out:
  207. pte_unmap_unlock(ptep, ptl);
  208. }
  209. #ifdef CONFIG_BLOCK
  210. /* Returns true if all buffers are successfully locked */
  211. static bool buffer_migrate_lock_buffers(struct buffer_head *head,
  212. enum migrate_mode mode)
  213. {
  214. struct buffer_head *bh = head;
  215. /* Simple case, sync compaction */
  216. if (mode != MIGRATE_ASYNC) {
  217. do {
  218. get_bh(bh);
  219. lock_buffer(bh);
  220. bh = bh->b_this_page;
  221. } while (bh != head);
  222. return true;
  223. }
  224. /* async case, we cannot block on lock_buffer so use trylock_buffer */
  225. do {
  226. get_bh(bh);
  227. if (!trylock_buffer(bh)) {
  228. /*
  229. * We failed to lock the buffer and cannot stall in
  230. * async migration. Release the taken locks
  231. */
  232. struct buffer_head *failed_bh = bh;
  233. put_bh(failed_bh);
  234. bh = head;
  235. while (bh != failed_bh) {
  236. unlock_buffer(bh);
  237. put_bh(bh);
  238. bh = bh->b_this_page;
  239. }
  240. return false;
  241. }
  242. bh = bh->b_this_page;
  243. } while (bh != head);
  244. return true;
  245. }
  246. #else
  247. static inline bool buffer_migrate_lock_buffers(struct buffer_head *head,
  248. enum migrate_mode mode)
  249. {
  250. return true;
  251. }
  252. #endif /* CONFIG_BLOCK */
  253. /*
  254. * Replace the page in the mapping.
  255. *
  256. * The number of remaining references must be:
  257. * 1 for anonymous pages without a mapping
  258. * 2 for pages with a mapping
  259. * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
  260. */
  261. static int migrate_page_move_mapping(struct address_space *mapping,
  262. struct page *newpage, struct page *page,
  263. struct buffer_head *head, enum migrate_mode mode)
  264. {
  265. int expected_count = 0;
  266. void **pslot;
  267. if (!mapping) {
  268. /* Anonymous page without mapping */
  269. if (page_count(page) != 1)
  270. return -EAGAIN;
  271. return MIGRATEPAGE_SUCCESS;
  272. }
  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. radix_tree_replace_slot(pslot, newpage);
  308. /*
  309. * Drop cache reference from old page by unfreezing
  310. * to one less reference.
  311. * We know this isn't the last reference.
  312. */
  313. page_unfreeze_refs(page, expected_count - 1);
  314. /*
  315. * If moved to a different zone then also account
  316. * the page for that zone. Other VM counters will be
  317. * taken care of when we establish references to the
  318. * new page and drop references to the old page.
  319. *
  320. * Note that anonymous pages are accounted for
  321. * via NR_FILE_PAGES and NR_ANON_PAGES if they
  322. * are mapped to swap space.
  323. */
  324. __dec_zone_page_state(page, NR_FILE_PAGES);
  325. __inc_zone_page_state(newpage, NR_FILE_PAGES);
  326. if (!PageSwapCache(page) && PageSwapBacked(page)) {
  327. __dec_zone_page_state(page, NR_SHMEM);
  328. __inc_zone_page_state(newpage, NR_SHMEM);
  329. }
  330. spin_unlock_irq(&mapping->tree_lock);
  331. return MIGRATEPAGE_SUCCESS;
  332. }
  333. /*
  334. * The expected number of remaining references is the same as that
  335. * of migrate_page_move_mapping().
  336. */
  337. int migrate_huge_page_move_mapping(struct address_space *mapping,
  338. struct page *newpage, struct page *page)
  339. {
  340. int expected_count;
  341. void **pslot;
  342. if (!mapping) {
  343. if (page_count(page) != 1)
  344. return -EAGAIN;
  345. return MIGRATEPAGE_SUCCESS;
  346. }
  347. spin_lock_irq(&mapping->tree_lock);
  348. pslot = radix_tree_lookup_slot(&mapping->page_tree,
  349. page_index(page));
  350. expected_count = 2 + page_has_private(page);
  351. if (page_count(page) != expected_count ||
  352. radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
  353. spin_unlock_irq(&mapping->tree_lock);
  354. return -EAGAIN;
  355. }
  356. if (!page_freeze_refs(page, expected_count)) {
  357. spin_unlock_irq(&mapping->tree_lock);
  358. return -EAGAIN;
  359. }
  360. get_page(newpage);
  361. radix_tree_replace_slot(pslot, newpage);
  362. page_unfreeze_refs(page, expected_count - 1);
  363. spin_unlock_irq(&mapping->tree_lock);
  364. return MIGRATEPAGE_SUCCESS;
  365. }
  366. /*
  367. * Copy the page to its new location
  368. */
  369. void migrate_page_copy(struct page *newpage, struct page *page)
  370. {
  371. if (PageHuge(page) || PageTransHuge(page))
  372. copy_huge_page(newpage, page);
  373. else
  374. copy_highpage(newpage, page);
  375. if (PageError(page))
  376. SetPageError(newpage);
  377. if (PageReferenced(page))
  378. SetPageReferenced(newpage);
  379. if (PageUptodate(page))
  380. SetPageUptodate(newpage);
  381. if (TestClearPageActive(page)) {
  382. VM_BUG_ON(PageUnevictable(page));
  383. SetPageActive(newpage);
  384. } else if (TestClearPageUnevictable(page))
  385. SetPageUnevictable(newpage);
  386. if (PageChecked(page))
  387. SetPageChecked(newpage);
  388. if (PageMappedToDisk(page))
  389. SetPageMappedToDisk(newpage);
  390. if (PageDirty(page)) {
  391. clear_page_dirty_for_io(page);
  392. /*
  393. * Want to mark the page and the radix tree as dirty, and
  394. * redo the accounting that clear_page_dirty_for_io undid,
  395. * but we can't use set_page_dirty because that function
  396. * is actually a signal that all of the page has become dirty.
  397. * Whereas only part of our page may be dirty.
  398. */
  399. if (PageSwapBacked(page))
  400. SetPageDirty(newpage);
  401. else
  402. __set_page_dirty_nobuffers(newpage);
  403. }
  404. mlock_migrate_page(newpage, page);
  405. ksm_migrate_page(newpage, page);
  406. /*
  407. * Please do not reorder this without considering how mm/ksm.c's
  408. * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache().
  409. */
  410. ClearPageSwapCache(page);
  411. ClearPagePrivate(page);
  412. set_page_private(page, 0);
  413. /*
  414. * If any waiters have accumulated on the new page then
  415. * wake them up.
  416. */
  417. if (PageWriteback(newpage))
  418. end_page_writeback(newpage);
  419. }
  420. /************************************************************
  421. * Migration functions
  422. ***********************************************************/
  423. /* Always fail migration. Used for mappings that are not movable */
  424. int fail_migrate_page(struct address_space *mapping,
  425. struct page *newpage, struct page *page)
  426. {
  427. return -EIO;
  428. }
  429. EXPORT_SYMBOL(fail_migrate_page);
  430. /*
  431. * Common logic to directly migrate a single page suitable for
  432. * pages that do not use PagePrivate/PagePrivate2.
  433. *
  434. * Pages are locked upon entry and exit.
  435. */
  436. int migrate_page(struct address_space *mapping,
  437. struct page *newpage, struct page *page,
  438. enum migrate_mode mode)
  439. {
  440. int rc;
  441. BUG_ON(PageWriteback(page)); /* Writeback must be complete */
  442. rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode);
  443. if (rc != MIGRATEPAGE_SUCCESS)
  444. return rc;
  445. migrate_page_copy(newpage, page);
  446. return MIGRATEPAGE_SUCCESS;
  447. }
  448. EXPORT_SYMBOL(migrate_page);
  449. #ifdef CONFIG_BLOCK
  450. /*
  451. * Migration function for pages with buffers. This function can only be used
  452. * if the underlying filesystem guarantees that no other references to "page"
  453. * exist.
  454. */
  455. int buffer_migrate_page(struct address_space *mapping,
  456. struct page *newpage, struct page *page, enum migrate_mode mode)
  457. {
  458. struct buffer_head *bh, *head;
  459. int rc;
  460. if (!page_has_buffers(page))
  461. return migrate_page(mapping, newpage, page, mode);
  462. head = page_buffers(page);
  463. rc = migrate_page_move_mapping(mapping, newpage, page, head, mode);
  464. if (rc != MIGRATEPAGE_SUCCESS)
  465. return rc;
  466. /*
  467. * In the async case, migrate_page_move_mapping locked the buffers
  468. * with an IRQ-safe spinlock held. In the sync case, the buffers
  469. * need to be locked now
  470. */
  471. if (mode != MIGRATE_ASYNC)
  472. BUG_ON(!buffer_migrate_lock_buffers(head, mode));
  473. ClearPagePrivate(page);
  474. set_page_private(newpage, page_private(page));
  475. set_page_private(page, 0);
  476. put_page(page);
  477. get_page(newpage);
  478. bh = head;
  479. do {
  480. set_bh_page(bh, newpage, bh_offset(bh));
  481. bh = bh->b_this_page;
  482. } while (bh != head);
  483. SetPagePrivate(newpage);
  484. migrate_page_copy(newpage, page);
  485. bh = head;
  486. do {
  487. unlock_buffer(bh);
  488. put_bh(bh);
  489. bh = bh->b_this_page;
  490. } while (bh != head);
  491. return MIGRATEPAGE_SUCCESS;
  492. }
  493. EXPORT_SYMBOL(buffer_migrate_page);
  494. #endif
  495. /*
  496. * Writeback a page to clean the dirty state
  497. */
  498. static int writeout(struct address_space *mapping, struct page *page)
  499. {
  500. struct writeback_control wbc = {
  501. .sync_mode = WB_SYNC_NONE,
  502. .nr_to_write = 1,
  503. .range_start = 0,
  504. .range_end = LLONG_MAX,
  505. .for_reclaim = 1
  506. };
  507. int rc;
  508. if (!mapping->a_ops->writepage)
  509. /* No write method for the address space */
  510. return -EINVAL;
  511. if (!clear_page_dirty_for_io(page))
  512. /* Someone else already triggered a write */
  513. return -EAGAIN;
  514. /*
  515. * A dirty page may imply that the underlying filesystem has
  516. * the page on some queue. So the page must be clean for
  517. * migration. Writeout may mean we loose the lock and the
  518. * page state is no longer what we checked for earlier.
  519. * At this point we know that the migration attempt cannot
  520. * be successful.
  521. */
  522. remove_migration_ptes(page, page);
  523. rc = mapping->a_ops->writepage(page, &wbc);
  524. if (rc != AOP_WRITEPAGE_ACTIVATE)
  525. /* unlocked. Relock */
  526. lock_page(page);
  527. return (rc < 0) ? -EIO : -EAGAIN;
  528. }
  529. /*
  530. * Default handling if a filesystem does not provide a migration function.
  531. */
  532. static int fallback_migrate_page(struct address_space *mapping,
  533. struct page *newpage, struct page *page, enum migrate_mode mode)
  534. {
  535. if (PageDirty(page)) {
  536. /* Only writeback pages in full synchronous migration */
  537. if (mode != MIGRATE_SYNC)
  538. return -EBUSY;
  539. return writeout(mapping, page);
  540. }
  541. /*
  542. * Buffers may be managed in a filesystem specific way.
  543. * We must have no buffers or drop them.
  544. */
  545. if (page_has_private(page) &&
  546. !try_to_release_page(page, GFP_KERNEL))
  547. return -EAGAIN;
  548. return migrate_page(mapping, newpage, page, mode);
  549. }
  550. /*
  551. * Move a page to a newly allocated page
  552. * The page is locked and all ptes have been successfully removed.
  553. *
  554. * The new page will have replaced the old page if this function
  555. * is successful.
  556. *
  557. * Return value:
  558. * < 0 - error code
  559. * MIGRATEPAGE_SUCCESS - success
  560. */
  561. static int move_to_new_page(struct page *newpage, struct page *page,
  562. int remap_swapcache, enum migrate_mode mode)
  563. {
  564. struct address_space *mapping;
  565. int rc;
  566. /*
  567. * Block others from accessing the page when we get around to
  568. * establishing additional references. We are the only one
  569. * holding a reference to the new page at this point.
  570. */
  571. if (!trylock_page(newpage))
  572. BUG();
  573. /* Prepare mapping for the new page.*/
  574. newpage->index = page->index;
  575. newpage->mapping = page->mapping;
  576. if (PageSwapBacked(page))
  577. SetPageSwapBacked(newpage);
  578. mapping = page_mapping(page);
  579. if (!mapping)
  580. rc = migrate_page(mapping, newpage, page, mode);
  581. else if (mapping->a_ops->migratepage)
  582. /*
  583. * Most pages have a mapping and most filesystems provide a
  584. * migratepage callback. Anonymous pages are part of swap
  585. * space which also has its own migratepage callback. This
  586. * is the most common path for page migration.
  587. */
  588. rc = mapping->a_ops->migratepage(mapping,
  589. newpage, page, mode);
  590. else
  591. rc = fallback_migrate_page(mapping, newpage, page, mode);
  592. if (rc != MIGRATEPAGE_SUCCESS) {
  593. newpage->mapping = NULL;
  594. } else {
  595. if (remap_swapcache)
  596. remove_migration_ptes(page, newpage);
  597. page->mapping = NULL;
  598. }
  599. unlock_page(newpage);
  600. return rc;
  601. }
  602. static int __unmap_and_move(struct page *page, struct page *newpage,
  603. int force, enum migrate_mode mode)
  604. {
  605. int rc = -EAGAIN;
  606. int remap_swapcache = 1;
  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. /* charge against new page */
  630. mem_cgroup_prepare_migration(page, newpage, &mem);
  631. if (PageWriteback(page)) {
  632. /*
  633. * Only in the case of a full syncronous migration is it
  634. * necessary to wait for PageWriteback. In the async case,
  635. * the retry loop is too short and in the sync-light case,
  636. * the overhead of stalling is too much
  637. */
  638. if (mode != MIGRATE_SYNC) {
  639. rc = -EBUSY;
  640. goto uncharge;
  641. }
  642. if (!force)
  643. goto uncharge;
  644. wait_on_page_writeback(page);
  645. }
  646. /*
  647. * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
  648. * we cannot notice that anon_vma is freed while we migrates a page.
  649. * This get_anon_vma() delays freeing anon_vma pointer until the end
  650. * of migration. File cache pages are no problem because of page_lock()
  651. * File Caches may use write_page() or lock_page() in migration, then,
  652. * just care Anon page here.
  653. */
  654. if (PageAnon(page) && !PageKsm(page)) {
  655. /*
  656. * Only page_lock_anon_vma_read() understands the subtleties of
  657. * getting a hold on an anon_vma from outside one of its mms.
  658. */
  659. anon_vma = page_get_anon_vma(page);
  660. if (anon_vma) {
  661. /*
  662. * Anon page
  663. */
  664. } else if (PageSwapCache(page)) {
  665. /*
  666. * We cannot be sure that the anon_vma of an unmapped
  667. * swapcache page is safe to use because we don't
  668. * know in advance if the VMA that this page belonged
  669. * to still exists. If the VMA and others sharing the
  670. * data have been freed, then the anon_vma could
  671. * already be invalid.
  672. *
  673. * To avoid this possibility, swapcache pages get
  674. * migrated but are not remapped when migration
  675. * completes
  676. */
  677. remap_swapcache = 0;
  678. } else {
  679. goto uncharge;
  680. }
  681. }
  682. if (unlikely(balloon_page_movable(page))) {
  683. /*
  684. * A ballooned page does not need any special attention from
  685. * physical to virtual reverse mapping procedures.
  686. * Skip any attempt to unmap PTEs or to remap swap cache,
  687. * in order to avoid burning cycles at rmap level, and perform
  688. * the page migration right away (proteced by page lock).
  689. */
  690. rc = balloon_page_migrate(newpage, page, mode);
  691. goto uncharge;
  692. }
  693. /*
  694. * Corner case handling:
  695. * 1. When a new swap-cache page is read into, it is added to the LRU
  696. * and treated as swapcache but it has no rmap yet.
  697. * Calling try_to_unmap() against a page->mapping==NULL page will
  698. * trigger a BUG. So handle it here.
  699. * 2. An orphaned page (see truncate_complete_page) might have
  700. * fs-private metadata. The page can be picked up due to memory
  701. * offlining. Everywhere else except page reclaim, the page is
  702. * invisible to the vm, so the page can not be migrated. So try to
  703. * free the metadata, so the page can be freed.
  704. */
  705. if (!page->mapping) {
  706. VM_BUG_ON(PageAnon(page));
  707. if (page_has_private(page)) {
  708. try_to_free_buffers(page);
  709. goto uncharge;
  710. }
  711. goto skip_unmap;
  712. }
  713. /* Establish migration ptes or remove ptes */
  714. try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
  715. skip_unmap:
  716. if (!page_mapped(page))
  717. rc = move_to_new_page(newpage, page, remap_swapcache, mode);
  718. if (rc && remap_swapcache)
  719. remove_migration_ptes(page, page);
  720. /* Drop an anon_vma reference if we took one */
  721. if (anon_vma)
  722. put_anon_vma(anon_vma);
  723. uncharge:
  724. mem_cgroup_end_migration(mem, page, newpage,
  725. (rc == MIGRATEPAGE_SUCCESS ||
  726. rc == MIGRATEPAGE_BALLOON_SUCCESS));
  727. unlock_page(page);
  728. out:
  729. return rc;
  730. }
  731. /*
  732. * Obtain the lock on page, remove all ptes and migrate the page
  733. * to the newly allocated page in newpage.
  734. */
  735. static int unmap_and_move(new_page_t get_new_page, unsigned long private,
  736. struct page *page, int force, enum migrate_mode mode)
  737. {
  738. int rc = 0;
  739. int *result = NULL;
  740. struct page *newpage = get_new_page(page, private, &result);
  741. if (!newpage)
  742. return -ENOMEM;
  743. if (page_count(page) == 1) {
  744. /* page was freed from under us. So we are done. */
  745. goto out;
  746. }
  747. if (unlikely(PageTransHuge(page)))
  748. if (unlikely(split_huge_page(page)))
  749. goto out;
  750. rc = __unmap_and_move(page, newpage, force, mode);
  751. if (unlikely(rc == MIGRATEPAGE_BALLOON_SUCCESS)) {
  752. /*
  753. * A ballooned page has been migrated already.
  754. * Now, it's the time to wrap-up counters,
  755. * handle the page back to Buddy and return.
  756. */
  757. dec_zone_page_state(page, NR_ISOLATED_ANON +
  758. page_is_file_cache(page));
  759. balloon_page_free(page);
  760. return MIGRATEPAGE_SUCCESS;
  761. }
  762. out:
  763. if (rc != -EAGAIN) {
  764. /*
  765. * A page that has been migrated has all references
  766. * removed and will be freed. A page that has not been
  767. * migrated will have kepts its references and be
  768. * restored.
  769. */
  770. list_del(&page->lru);
  771. dec_zone_page_state(page, NR_ISOLATED_ANON +
  772. page_is_file_cache(page));
  773. putback_lru_page(page);
  774. }
  775. /*
  776. * Move the new page to the LRU. If migration was not successful
  777. * then this will free the page.
  778. */
  779. putback_lru_page(newpage);
  780. if (result) {
  781. if (rc)
  782. *result = rc;
  783. else
  784. *result = page_to_nid(newpage);
  785. }
  786. return rc;
  787. }
  788. /*
  789. * Counterpart of unmap_and_move_page() for hugepage migration.
  790. *
  791. * This function doesn't wait the completion of hugepage I/O
  792. * because there is no race between I/O and migration for hugepage.
  793. * Note that currently hugepage I/O occurs only in direct I/O
  794. * where no lock is held and PG_writeback is irrelevant,
  795. * and writeback status of all subpages are counted in the reference
  796. * count of the head page (i.e. if all subpages of a 2MB hugepage are
  797. * under direct I/O, the reference of the head page is 512 and a bit more.)
  798. * This means that when we try to migrate hugepage whose subpages are
  799. * doing direct I/O, some references remain after try_to_unmap() and
  800. * hugepage migration fails without data corruption.
  801. *
  802. * There is also no race when direct I/O is issued on the page under migration,
  803. * because then pte is replaced with migration swap entry and direct I/O code
  804. * will wait in the page fault for migration to complete.
  805. */
  806. static int unmap_and_move_huge_page(new_page_t get_new_page,
  807. unsigned long private, struct page *hpage,
  808. int force, enum migrate_mode mode)
  809. {
  810. int rc = 0;
  811. int *result = NULL;
  812. struct page *new_hpage = get_new_page(hpage, private, &result);
  813. struct anon_vma *anon_vma = NULL;
  814. if (!new_hpage)
  815. return -ENOMEM;
  816. rc = -EAGAIN;
  817. if (!trylock_page(hpage)) {
  818. if (!force || mode != MIGRATE_SYNC)
  819. goto out;
  820. lock_page(hpage);
  821. }
  822. if (PageAnon(hpage))
  823. anon_vma = page_get_anon_vma(hpage);
  824. try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
  825. if (!page_mapped(hpage))
  826. rc = move_to_new_page(new_hpage, hpage, 1, mode);
  827. if (rc)
  828. remove_migration_ptes(hpage, hpage);
  829. if (anon_vma)
  830. put_anon_vma(anon_vma);
  831. if (!rc)
  832. hugetlb_cgroup_migrate(hpage, new_hpage);
  833. unlock_page(hpage);
  834. out:
  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, new_page_t get_new_page,
  860. unsigned long private, enum migrate_mode mode, int reason)
  861. {
  862. int retry = 1;
  863. int nr_failed = 0;
  864. int nr_succeeded = 0;
  865. int pass = 0;
  866. struct page *page;
  867. struct page *page2;
  868. int swapwrite = current->flags & PF_SWAPWRITE;
  869. int rc;
  870. if (!swapwrite)
  871. current->flags |= PF_SWAPWRITE;
  872. for(pass = 0; pass < 10 && retry; pass++) {
  873. retry = 0;
  874. list_for_each_entry_safe(page, page2, from, lru) {
  875. cond_resched();
  876. rc = unmap_and_move(get_new_page, private,
  877. page, pass > 2, mode);
  878. switch(rc) {
  879. case -ENOMEM:
  880. goto out;
  881. case -EAGAIN:
  882. retry++;
  883. break;
  884. case MIGRATEPAGE_SUCCESS:
  885. nr_succeeded++;
  886. break;
  887. default:
  888. /* Permanent failure */
  889. nr_failed++;
  890. break;
  891. }
  892. }
  893. }
  894. rc = nr_failed + retry;
  895. out:
  896. if (nr_succeeded)
  897. count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
  898. if (nr_failed)
  899. count_vm_events(PGMIGRATE_FAIL, nr_failed);
  900. trace_mm_migrate_pages(nr_succeeded, nr_failed, mode, reason);
  901. if (!swapwrite)
  902. current->flags &= ~PF_SWAPWRITE;
  903. return rc;
  904. }
  905. int migrate_huge_page(struct page *hpage, new_page_t get_new_page,
  906. unsigned long private, enum migrate_mode mode)
  907. {
  908. int pass, rc;
  909. for (pass = 0; pass < 10; pass++) {
  910. rc = unmap_and_move_huge_page(get_new_page, private,
  911. hpage, pass > 2, mode);
  912. switch (rc) {
  913. case -ENOMEM:
  914. goto out;
  915. case -EAGAIN:
  916. /* try again */
  917. cond_resched();
  918. break;
  919. case MIGRATEPAGE_SUCCESS:
  920. goto out;
  921. default:
  922. rc = -EIO;
  923. goto out;
  924. }
  925. }
  926. out:
  927. return rc;
  928. }
  929. #ifdef CONFIG_NUMA
  930. /*
  931. * Move a list of individual pages
  932. */
  933. struct page_to_node {
  934. unsigned long addr;
  935. struct page *page;
  936. int node;
  937. int status;
  938. };
  939. static struct page *new_page_node(struct page *p, unsigned long private,
  940. int **result)
  941. {
  942. struct page_to_node *pm = (struct page_to_node *)private;
  943. while (pm->node != MAX_NUMNODES && pm->page != p)
  944. pm++;
  945. if (pm->node == MAX_NUMNODES)
  946. return NULL;
  947. *result = &pm->status;
  948. return alloc_pages_exact_node(pm->node,
  949. GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
  950. }
  951. /*
  952. * Move a set of pages as indicated in the pm array. The addr
  953. * field must be set to the virtual address of the page to be moved
  954. * and the node number must contain a valid target node.
  955. * The pm array ends with node = MAX_NUMNODES.
  956. */
  957. static int do_move_page_to_node_array(struct mm_struct *mm,
  958. struct page_to_node *pm,
  959. int migrate_all)
  960. {
  961. int err;
  962. struct page_to_node *pp;
  963. LIST_HEAD(pagelist);
  964. down_read(&mm->mmap_sem);
  965. /*
  966. * Build a list of pages to migrate
  967. */
  968. for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
  969. struct vm_area_struct *vma;
  970. struct page *page;
  971. err = -EFAULT;
  972. vma = find_vma(mm, pp->addr);
  973. if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
  974. goto set_status;
  975. page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT);
  976. err = PTR_ERR(page);
  977. if (IS_ERR(page))
  978. goto set_status;
  979. err = -ENOENT;
  980. if (!page)
  981. goto set_status;
  982. /* Use PageReserved to check for zero page */
  983. if (PageReserved(page))
  984. goto put_and_set;
  985. pp->page = page;
  986. err = page_to_nid(page);
  987. if (err == pp->node)
  988. /*
  989. * Node already in the right place
  990. */
  991. goto put_and_set;
  992. err = -EACCES;
  993. if (page_mapcount(page) > 1 &&
  994. !migrate_all)
  995. goto put_and_set;
  996. err = isolate_lru_page(page);
  997. if (!err) {
  998. list_add_tail(&page->lru, &pagelist);
  999. inc_zone_page_state(page, NR_ISOLATED_ANON +
  1000. page_is_file_cache(page));
  1001. }
  1002. put_and_set:
  1003. /*
  1004. * Either remove the duplicate refcount from
  1005. * isolate_lru_page() or drop the page ref if it was
  1006. * not isolated.
  1007. */
  1008. put_page(page);
  1009. set_status:
  1010. pp->status = err;
  1011. }
  1012. err = 0;
  1013. if (!list_empty(&pagelist)) {
  1014. err = migrate_pages(&pagelist, new_page_node,
  1015. (unsigned long)pm, MIGRATE_SYNC, MR_SYSCALL);
  1016. if (err)
  1017. putback_lru_pages(&pagelist);
  1018. }
  1019. up_read(&mm->mmap_sem);
  1020. return err;
  1021. }
  1022. /*
  1023. * Migrate an array of page address onto an array of nodes and fill
  1024. * the corresponding array of status.
  1025. */
  1026. static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
  1027. unsigned long nr_pages,
  1028. const void __user * __user *pages,
  1029. const int __user *nodes,
  1030. int __user *status, int flags)
  1031. {
  1032. struct page_to_node *pm;
  1033. unsigned long chunk_nr_pages;
  1034. unsigned long chunk_start;
  1035. int err;
  1036. err = -ENOMEM;
  1037. pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
  1038. if (!pm)
  1039. goto out;
  1040. migrate_prep();
  1041. /*
  1042. * Store a chunk of page_to_node array in a page,
  1043. * but keep the last one as a marker
  1044. */
  1045. chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
  1046. for (chunk_start = 0;
  1047. chunk_start < nr_pages;
  1048. chunk_start += chunk_nr_pages) {
  1049. int j;
  1050. if (chunk_start + chunk_nr_pages > nr_pages)
  1051. chunk_nr_pages = nr_pages - chunk_start;
  1052. /* fill the chunk pm with addrs and nodes from user-space */
  1053. for (j = 0; j < chunk_nr_pages; j++) {
  1054. const void __user *p;
  1055. int node;
  1056. err = -EFAULT;
  1057. if (get_user(p, pages + j + chunk_start))
  1058. goto out_pm;
  1059. pm[j].addr = (unsigned long) p;
  1060. if (get_user(node, nodes + j + chunk_start))
  1061. goto out_pm;
  1062. err = -ENODEV;
  1063. if (node < 0 || node >= MAX_NUMNODES)
  1064. goto out_pm;
  1065. if (!node_state(node, N_MEMORY))
  1066. goto out_pm;
  1067. err = -EACCES;
  1068. if (!node_isset(node, task_nodes))
  1069. goto out_pm;
  1070. pm[j].node = node;
  1071. }
  1072. /* End marker for this chunk */
  1073. pm[chunk_nr_pages].node = MAX_NUMNODES;
  1074. /* Migrate this chunk */
  1075. err = do_move_page_to_node_array(mm, pm,
  1076. flags & MPOL_MF_MOVE_ALL);
  1077. if (err < 0)
  1078. goto out_pm;
  1079. /* Return status information */
  1080. for (j = 0; j < chunk_nr_pages; j++)
  1081. if (put_user(pm[j].status, status + j + chunk_start)) {
  1082. err = -EFAULT;
  1083. goto out_pm;
  1084. }
  1085. }
  1086. err = 0;
  1087. out_pm:
  1088. free_page((unsigned long)pm);
  1089. out:
  1090. return err;
  1091. }
  1092. /*
  1093. * Determine the nodes of an array of pages and store it in an array of status.
  1094. */
  1095. static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
  1096. const void __user **pages, int *status)
  1097. {
  1098. unsigned long i;
  1099. down_read(&mm->mmap_sem);
  1100. for (i = 0; i < nr_pages; i++) {
  1101. unsigned long addr = (unsigned long)(*pages);
  1102. struct vm_area_struct *vma;
  1103. struct page *page;
  1104. int err = -EFAULT;
  1105. vma = find_vma(mm, addr);
  1106. if (!vma || addr < vma->vm_start)
  1107. goto set_status;
  1108. page = follow_page(vma, addr, 0);
  1109. err = PTR_ERR(page);
  1110. if (IS_ERR(page))
  1111. goto set_status;
  1112. err = -ENOENT;
  1113. /* Use PageReserved to check for zero page */
  1114. if (!page || PageReserved(page))
  1115. goto set_status;
  1116. err = page_to_nid(page);
  1117. set_status:
  1118. *status = err;
  1119. pages++;
  1120. status++;
  1121. }
  1122. up_read(&mm->mmap_sem);
  1123. }
  1124. /*
  1125. * Determine the nodes of a user array of pages and store it in
  1126. * a user array of status.
  1127. */
  1128. static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
  1129. const void __user * __user *pages,
  1130. int __user *status)
  1131. {
  1132. #define DO_PAGES_STAT_CHUNK_NR 16
  1133. const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
  1134. int chunk_status[DO_PAGES_STAT_CHUNK_NR];
  1135. while (nr_pages) {
  1136. unsigned long chunk_nr;
  1137. chunk_nr = nr_pages;
  1138. if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
  1139. chunk_nr = DO_PAGES_STAT_CHUNK_NR;
  1140. if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
  1141. break;
  1142. do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
  1143. if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
  1144. break;
  1145. pages += chunk_nr;
  1146. status += chunk_nr;
  1147. nr_pages -= chunk_nr;
  1148. }
  1149. return nr_pages ? -EFAULT : 0;
  1150. }
  1151. /*
  1152. * Move a list of pages in the address space of the currently executing
  1153. * process.
  1154. */
  1155. SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
  1156. const void __user * __user *, pages,
  1157. const int __user *, nodes,
  1158. int __user *, status, int, flags)
  1159. {
  1160. const struct cred *cred = current_cred(), *tcred;
  1161. struct task_struct *task;
  1162. struct mm_struct *mm;
  1163. int err;
  1164. nodemask_t task_nodes;
  1165. /* Check flags */
  1166. if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
  1167. return -EINVAL;
  1168. if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
  1169. return -EPERM;
  1170. /* Find the mm_struct */
  1171. rcu_read_lock();
  1172. task = pid ? find_task_by_vpid(pid) : current;
  1173. if (!task) {
  1174. rcu_read_unlock();
  1175. return -ESRCH;
  1176. }
  1177. get_task_struct(task);
  1178. /*
  1179. * Check if this process has the right to modify the specified
  1180. * process. The right exists if the process has administrative
  1181. * capabilities, superuser privileges or the same
  1182. * userid as the target process.
  1183. */
  1184. tcred = __task_cred(task);
  1185. if (!uid_eq(cred->euid, tcred->suid) && !uid_eq(cred->euid, tcred->uid) &&
  1186. !uid_eq(cred->uid, tcred->suid) && !uid_eq(cred->uid, tcred->uid) &&
  1187. !capable(CAP_SYS_NICE)) {
  1188. rcu_read_unlock();
  1189. err = -EPERM;
  1190. goto out;
  1191. }
  1192. rcu_read_unlock();
  1193. err = security_task_movememory(task);
  1194. if (err)
  1195. goto out;
  1196. task_nodes = cpuset_mems_allowed(task);
  1197. mm = get_task_mm(task);
  1198. put_task_struct(task);
  1199. if (!mm)
  1200. return -EINVAL;
  1201. if (nodes)
  1202. err = do_pages_move(mm, task_nodes, nr_pages, pages,
  1203. nodes, status, flags);
  1204. else
  1205. err = do_pages_stat(mm, nr_pages, pages, status);
  1206. mmput(mm);
  1207. return err;
  1208. out:
  1209. put_task_struct(task);
  1210. return err;
  1211. }
  1212. /*
  1213. * Call migration functions in the vma_ops that may prepare
  1214. * memory in a vm for migration. migration functions may perform
  1215. * the migration for vmas that do not have an underlying page struct.
  1216. */
  1217. int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
  1218. const nodemask_t *from, unsigned long flags)
  1219. {
  1220. struct vm_area_struct *vma;
  1221. int err = 0;
  1222. for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
  1223. if (vma->vm_ops && vma->vm_ops->migrate) {
  1224. err = vma->vm_ops->migrate(vma, to, from, flags);
  1225. if (err)
  1226. break;
  1227. }
  1228. }
  1229. return err;
  1230. }
  1231. #ifdef CONFIG_NUMA_BALANCING
  1232. /*
  1233. * Returns true if this is a safe migration target node for misplaced NUMA
  1234. * pages. Currently it only checks the watermarks which crude
  1235. */
  1236. static bool migrate_balanced_pgdat(struct pglist_data *pgdat,
  1237. unsigned long nr_migrate_pages)
  1238. {
  1239. int z;
  1240. for (z = pgdat->nr_zones - 1; z >= 0; z--) {
  1241. struct zone *zone = pgdat->node_zones + z;
  1242. if (!populated_zone(zone))
  1243. continue;
  1244. if (zone->all_unreclaimable)
  1245. continue;
  1246. /* Avoid waking kswapd by allocating pages_to_migrate pages. */
  1247. if (!zone_watermark_ok(zone, 0,
  1248. high_wmark_pages(zone) +
  1249. nr_migrate_pages,
  1250. 0, 0))
  1251. continue;
  1252. return true;
  1253. }
  1254. return false;
  1255. }
  1256. static struct page *alloc_misplaced_dst_page(struct page *page,
  1257. unsigned long data,
  1258. int **result)
  1259. {
  1260. int nid = (int) data;
  1261. struct page *newpage;
  1262. newpage = alloc_pages_exact_node(nid,
  1263. (GFP_HIGHUSER_MOVABLE | GFP_THISNODE |
  1264. __GFP_NOMEMALLOC | __GFP_NORETRY |
  1265. __GFP_NOWARN) &
  1266. ~GFP_IOFS, 0);
  1267. if (newpage)
  1268. page_nid_xchg_last(newpage, page_nid_last(page));
  1269. return newpage;
  1270. }
  1271. /*
  1272. * page migration rate limiting control.
  1273. * Do not migrate more than @pages_to_migrate in a @migrate_interval_millisecs
  1274. * window of time. Default here says do not migrate more than 1280M per second.
  1275. * If a node is rate-limited then PTE NUMA updates are also rate-limited. However
  1276. * as it is faults that reset the window, pte updates will happen unconditionally
  1277. * if there has not been a fault since @pteupdate_interval_millisecs after the
  1278. * throttle window closed.
  1279. */
  1280. static unsigned int migrate_interval_millisecs __read_mostly = 100;
  1281. static unsigned int pteupdate_interval_millisecs __read_mostly = 1000;
  1282. static unsigned int ratelimit_pages __read_mostly = 128 << (20 - PAGE_SHIFT);
  1283. /* Returns true if NUMA migration is currently rate limited */
  1284. bool migrate_ratelimited(int node)
  1285. {
  1286. pg_data_t *pgdat = NODE_DATA(node);
  1287. if (time_after(jiffies, pgdat->numabalancing_migrate_next_window +
  1288. msecs_to_jiffies(pteupdate_interval_millisecs)))
  1289. return false;
  1290. if (pgdat->numabalancing_migrate_nr_pages < ratelimit_pages)
  1291. return false;
  1292. return true;
  1293. }
  1294. /* Returns true if the node is migrate rate-limited after the update */
  1295. bool numamigrate_update_ratelimit(pg_data_t *pgdat, unsigned long nr_pages)
  1296. {
  1297. bool rate_limited = false;
  1298. /*
  1299. * Rate-limit the amount of data that is being migrated to a node.
  1300. * Optimal placement is no good if the memory bus is saturated and
  1301. * all the time is being spent migrating!
  1302. */
  1303. spin_lock(&pgdat->numabalancing_migrate_lock);
  1304. if (time_after(jiffies, pgdat->numabalancing_migrate_next_window)) {
  1305. pgdat->numabalancing_migrate_nr_pages = 0;
  1306. pgdat->numabalancing_migrate_next_window = jiffies +
  1307. msecs_to_jiffies(migrate_interval_millisecs);
  1308. }
  1309. if (pgdat->numabalancing_migrate_nr_pages > ratelimit_pages)
  1310. rate_limited = true;
  1311. else
  1312. pgdat->numabalancing_migrate_nr_pages += nr_pages;
  1313. spin_unlock(&pgdat->numabalancing_migrate_lock);
  1314. return rate_limited;
  1315. }
  1316. int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
  1317. {
  1318. int page_lru;
  1319. VM_BUG_ON(compound_order(page) && !PageTransHuge(page));
  1320. /* Avoid migrating to a node that is nearly full */
  1321. if (!migrate_balanced_pgdat(pgdat, 1UL << compound_order(page)))
  1322. return 0;
  1323. if (isolate_lru_page(page))
  1324. return 0;
  1325. /*
  1326. * migrate_misplaced_transhuge_page() skips page migration's usual
  1327. * check on page_count(), so we must do it here, now that the page
  1328. * has been isolated: a GUP pin, or any other pin, prevents migration.
  1329. * The expected page count is 3: 1 for page's mapcount and 1 for the
  1330. * caller's pin and 1 for the reference taken by isolate_lru_page().
  1331. */
  1332. if (PageTransHuge(page) && page_count(page) != 3) {
  1333. putback_lru_page(page);
  1334. return 0;
  1335. }
  1336. page_lru = page_is_file_cache(page);
  1337. mod_zone_page_state(page_zone(page), NR_ISOLATED_ANON + page_lru,
  1338. hpage_nr_pages(page));
  1339. /*
  1340. * Isolating the page has taken another reference, so the
  1341. * caller's reference can be safely dropped without the page
  1342. * disappearing underneath us during migration.
  1343. */
  1344. put_page(page);
  1345. return 1;
  1346. }
  1347. /*
  1348. * Attempt to migrate a misplaced page to the specified destination
  1349. * node. Caller is expected to have an elevated reference count on
  1350. * the page that will be dropped by this function before returning.
  1351. */
  1352. int migrate_misplaced_page(struct page *page, int node)
  1353. {
  1354. pg_data_t *pgdat = NODE_DATA(node);
  1355. int isolated;
  1356. int nr_remaining;
  1357. LIST_HEAD(migratepages);
  1358. /*
  1359. * Don't migrate pages that are mapped in multiple processes.
  1360. * TODO: Handle false sharing detection instead of this hammer
  1361. */
  1362. if (page_mapcount(page) != 1)
  1363. goto out;
  1364. /*
  1365. * Rate-limit the amount of data that is being migrated to a node.
  1366. * Optimal placement is no good if the memory bus is saturated and
  1367. * all the time is being spent migrating!
  1368. */
  1369. if (numamigrate_update_ratelimit(pgdat, 1))
  1370. goto out;
  1371. isolated = numamigrate_isolate_page(pgdat, page);
  1372. if (!isolated)
  1373. goto out;
  1374. list_add(&page->lru, &migratepages);
  1375. nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_page,
  1376. node, MIGRATE_ASYNC, MR_NUMA_MISPLACED);
  1377. if (nr_remaining) {
  1378. putback_lru_pages(&migratepages);
  1379. isolated = 0;
  1380. } else
  1381. count_vm_numa_event(NUMA_PAGE_MIGRATE);
  1382. BUG_ON(!list_empty(&migratepages));
  1383. return isolated;
  1384. out:
  1385. put_page(page);
  1386. return 0;
  1387. }
  1388. #endif /* CONFIG_NUMA_BALANCING */
  1389. #if defined(CONFIG_NUMA_BALANCING) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
  1390. /*
  1391. * Migrates a THP to a given target node. page must be locked and is unlocked
  1392. * before returning.
  1393. */
  1394. int migrate_misplaced_transhuge_page(struct mm_struct *mm,
  1395. struct vm_area_struct *vma,
  1396. pmd_t *pmd, pmd_t entry,
  1397. unsigned long address,
  1398. struct page *page, int node)
  1399. {
  1400. unsigned long haddr = address & HPAGE_PMD_MASK;
  1401. pg_data_t *pgdat = NODE_DATA(node);
  1402. int isolated = 0;
  1403. struct page *new_page = NULL;
  1404. struct mem_cgroup *memcg = NULL;
  1405. int page_lru = page_is_file_cache(page);
  1406. /*
  1407. * Don't migrate pages that are mapped in multiple processes.
  1408. * TODO: Handle false sharing detection instead of this hammer
  1409. */
  1410. if (page_mapcount(page) != 1)
  1411. goto out_dropref;
  1412. /*
  1413. * Rate-limit the amount of data that is being migrated to a node.
  1414. * Optimal placement is no good if the memory bus is saturated and
  1415. * all the time is being spent migrating!
  1416. */
  1417. if (numamigrate_update_ratelimit(pgdat, HPAGE_PMD_NR))
  1418. goto out_dropref;
  1419. new_page = alloc_pages_node(node,
  1420. (GFP_TRANSHUGE | GFP_THISNODE) & ~__GFP_WAIT, HPAGE_PMD_ORDER);
  1421. if (!new_page)
  1422. goto out_fail;
  1423. page_nid_xchg_last(new_page, page_nid_last(page));
  1424. isolated = numamigrate_isolate_page(pgdat, page);
  1425. if (!isolated) {
  1426. put_page(new_page);
  1427. goto out_fail;
  1428. }
  1429. /* Prepare a page as a migration target */
  1430. __set_page_locked(new_page);
  1431. SetPageSwapBacked(new_page);
  1432. /* anon mapping, we can simply copy page->mapping to the new page: */
  1433. new_page->mapping = page->mapping;
  1434. new_page->index = page->index;
  1435. migrate_page_copy(new_page, page);
  1436. WARN_ON(PageLRU(new_page));
  1437. /* Recheck the target PMD */
  1438. spin_lock(&mm->page_table_lock);
  1439. if (unlikely(!pmd_same(*pmd, entry))) {
  1440. spin_unlock(&mm->page_table_lock);
  1441. /* Reverse changes made by migrate_page_copy() */
  1442. if (TestClearPageActive(new_page))
  1443. SetPageActive(page);
  1444. if (TestClearPageUnevictable(new_page))
  1445. SetPageUnevictable(page);
  1446. mlock_migrate_page(page, new_page);
  1447. unlock_page(new_page);
  1448. put_page(new_page); /* Free it */
  1449. unlock_page(page);
  1450. putback_lru_page(page);
  1451. count_vm_events(PGMIGRATE_FAIL, HPAGE_PMD_NR);
  1452. isolated = 0;
  1453. goto out;
  1454. }
  1455. /*
  1456. * Traditional migration needs to prepare the memcg charge
  1457. * transaction early to prevent the old page from being
  1458. * uncharged when installing migration entries. Here we can
  1459. * save the potential rollback and start the charge transfer
  1460. * only when migration is already known to end successfully.
  1461. */
  1462. mem_cgroup_prepare_migration(page, new_page, &memcg);
  1463. entry = mk_pmd(new_page, vma->vm_page_prot);
  1464. entry = pmd_mknonnuma(entry);
  1465. entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
  1466. entry = pmd_mkhuge(entry);
  1467. page_add_new_anon_rmap(new_page, vma, haddr);
  1468. set_pmd_at(mm, haddr, pmd, entry);
  1469. update_mmu_cache_pmd(vma, address, &entry);
  1470. page_remove_rmap(page);
  1471. /*
  1472. * Finish the charge transaction under the page table lock to
  1473. * prevent split_huge_page() from dividing up the charge
  1474. * before it's fully transferred to the new page.
  1475. */
  1476. mem_cgroup_end_migration(memcg, page, new_page, true);
  1477. spin_unlock(&mm->page_table_lock);
  1478. unlock_page(new_page);
  1479. unlock_page(page);
  1480. put_page(page); /* Drop the rmap reference */
  1481. put_page(page); /* Drop the LRU isolation reference */
  1482. count_vm_events(PGMIGRATE_SUCCESS, HPAGE_PMD_NR);
  1483. count_vm_numa_events(NUMA_PAGE_MIGRATE, HPAGE_PMD_NR);
  1484. out:
  1485. mod_zone_page_state(page_zone(page),
  1486. NR_ISOLATED_ANON + page_lru,
  1487. -HPAGE_PMD_NR);
  1488. return isolated;
  1489. out_fail:
  1490. count_vm_events(PGMIGRATE_FAIL, HPAGE_PMD_NR);
  1491. out_dropref:
  1492. unlock_page(page);
  1493. put_page(page);
  1494. return 0;
  1495. }
  1496. #endif /* CONFIG_NUMA_BALANCING */
  1497. #endif /* CONFIG_NUMA */