migrate.c 48 KB

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