migrate.c 48 KB

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