migrate.c 52 KB

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