migrate.c 52 KB

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