migrate.c 52 KB

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