migrate.c 47 KB

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