migrate.c 52 KB

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