migrate.c 47 KB

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