migrate.c 48 KB

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