migrate.c 52 KB

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