migrate.c 52 KB

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