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