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