migrate.c 48 KB

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