migrate.c 52 KB

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