migrate.c 53 KB

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