migrate.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878
  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. if (pmd_trans_huge(*pmd))
  112. goto out;
  113. ptep = pte_offset_map(pmd, addr);
  114. /*
  115. * Peek to check is_swap_pte() before taking ptlock? No, we
  116. * can race mremap's move_ptes(), which skips anon_vma lock.
  117. */
  118. ptl = pte_lockptr(mm, pmd);
  119. }
  120. spin_lock(ptl);
  121. pte = *ptep;
  122. if (!is_swap_pte(pte))
  123. goto unlock;
  124. entry = pte_to_swp_entry(pte);
  125. if (!is_migration_entry(entry) ||
  126. migration_entry_to_page(entry) != old)
  127. goto unlock;
  128. get_page(new);
  129. pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
  130. if (pte_swp_soft_dirty(*ptep))
  131. pte = pte_mksoft_dirty(pte);
  132. if (is_write_migration_entry(entry))
  133. pte = pte_mkwrite(pte);
  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. static 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. /*
  457. * Copy NUMA information to the new page, to prevent over-eager
  458. * future migrations of this same page.
  459. */
  460. cpupid = page_cpupid_xchg_last(page, -1);
  461. page_cpupid_xchg_last(newpage, cpupid);
  462. mlock_migrate_page(newpage, page);
  463. ksm_migrate_page(newpage, page);
  464. /*
  465. * Please do not reorder this without considering how mm/ksm.c's
  466. * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache().
  467. */
  468. ClearPageSwapCache(page);
  469. ClearPagePrivate(page);
  470. set_page_private(page, 0);
  471. /*
  472. * If any waiters have accumulated on the new page then
  473. * wake them up.
  474. */
  475. if (PageWriteback(newpage))
  476. end_page_writeback(newpage);
  477. }
  478. /************************************************************
  479. * Migration functions
  480. ***********************************************************/
  481. /*
  482. * Common logic to directly migrate a single page suitable for
  483. * pages that do not use PagePrivate/PagePrivate2.
  484. *
  485. * Pages are locked upon entry and exit.
  486. */
  487. int migrate_page(struct address_space *mapping,
  488. struct page *newpage, struct page *page,
  489. enum migrate_mode mode)
  490. {
  491. int rc;
  492. BUG_ON(PageWriteback(page)); /* Writeback must be complete */
  493. rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode, 0);
  494. if (rc != MIGRATEPAGE_SUCCESS)
  495. return rc;
  496. migrate_page_copy(newpage, page);
  497. return MIGRATEPAGE_SUCCESS;
  498. }
  499. EXPORT_SYMBOL(migrate_page);
  500. #ifdef CONFIG_BLOCK
  501. /*
  502. * Migration function for pages with buffers. This function can only be used
  503. * if the underlying filesystem guarantees that no other references to "page"
  504. * exist.
  505. */
  506. int buffer_migrate_page(struct address_space *mapping,
  507. struct page *newpage, struct page *page, enum migrate_mode mode)
  508. {
  509. struct buffer_head *bh, *head;
  510. int rc;
  511. if (!page_has_buffers(page))
  512. return migrate_page(mapping, newpage, page, mode);
  513. head = page_buffers(page);
  514. rc = migrate_page_move_mapping(mapping, newpage, page, head, mode, 0);
  515. if (rc != MIGRATEPAGE_SUCCESS)
  516. return rc;
  517. /*
  518. * In the async case, migrate_page_move_mapping locked the buffers
  519. * with an IRQ-safe spinlock held. In the sync case, the buffers
  520. * need to be locked now
  521. */
  522. if (mode != MIGRATE_ASYNC)
  523. BUG_ON(!buffer_migrate_lock_buffers(head, mode));
  524. ClearPagePrivate(page);
  525. set_page_private(newpage, page_private(page));
  526. set_page_private(page, 0);
  527. put_page(page);
  528. get_page(newpage);
  529. bh = head;
  530. do {
  531. set_bh_page(bh, newpage, bh_offset(bh));
  532. bh = bh->b_this_page;
  533. } while (bh != head);
  534. SetPagePrivate(newpage);
  535. migrate_page_copy(newpage, page);
  536. bh = head;
  537. do {
  538. unlock_buffer(bh);
  539. put_bh(bh);
  540. bh = bh->b_this_page;
  541. } while (bh != head);
  542. return MIGRATEPAGE_SUCCESS;
  543. }
  544. EXPORT_SYMBOL(buffer_migrate_page);
  545. #endif
  546. /*
  547. * Writeback a page to clean the dirty state
  548. */
  549. static int writeout(struct address_space *mapping, struct page *page)
  550. {
  551. struct writeback_control wbc = {
  552. .sync_mode = WB_SYNC_NONE,
  553. .nr_to_write = 1,
  554. .range_start = 0,
  555. .range_end = LLONG_MAX,
  556. .for_reclaim = 1
  557. };
  558. int rc;
  559. if (!mapping->a_ops->writepage)
  560. /* No write method for the address space */
  561. return -EINVAL;
  562. if (!clear_page_dirty_for_io(page))
  563. /* Someone else already triggered a write */
  564. return -EAGAIN;
  565. /*
  566. * A dirty page may imply that the underlying filesystem has
  567. * the page on some queue. So the page must be clean for
  568. * migration. Writeout may mean we loose the lock and the
  569. * page state is no longer what we checked for earlier.
  570. * At this point we know that the migration attempt cannot
  571. * be successful.
  572. */
  573. remove_migration_ptes(page, page);
  574. rc = mapping->a_ops->writepage(page, &wbc);
  575. if (rc != AOP_WRITEPAGE_ACTIVATE)
  576. /* unlocked. Relock */
  577. lock_page(page);
  578. return (rc < 0) ? -EIO : -EAGAIN;
  579. }
  580. /*
  581. * Default handling if a filesystem does not provide a migration function.
  582. */
  583. static int fallback_migrate_page(struct address_space *mapping,
  584. struct page *newpage, struct page *page, enum migrate_mode mode)
  585. {
  586. if (PageDirty(page)) {
  587. /* Only writeback pages in full synchronous migration */
  588. if (mode != MIGRATE_SYNC)
  589. return -EBUSY;
  590. return writeout(mapping, page);
  591. }
  592. /*
  593. * Buffers may be managed in a filesystem specific way.
  594. * We must have no buffers or drop them.
  595. */
  596. if (page_has_private(page) &&
  597. !try_to_release_page(page, GFP_KERNEL))
  598. return -EAGAIN;
  599. return migrate_page(mapping, newpage, page, mode);
  600. }
  601. /*
  602. * Move a page to a newly allocated page
  603. * The page is locked and all ptes have been successfully removed.
  604. *
  605. * The new page will have replaced the old page if this function
  606. * is successful.
  607. *
  608. * Return value:
  609. * < 0 - error code
  610. * MIGRATEPAGE_SUCCESS - success
  611. */
  612. static int move_to_new_page(struct page *newpage, struct page *page,
  613. int remap_swapcache, enum migrate_mode mode)
  614. {
  615. struct address_space *mapping;
  616. int rc;
  617. /*
  618. * Block others from accessing the page when we get around to
  619. * establishing additional references. We are the only one
  620. * holding a reference to the new page at this point.
  621. */
  622. if (!trylock_page(newpage))
  623. BUG();
  624. /* Prepare mapping for the new page.*/
  625. newpage->index = page->index;
  626. newpage->mapping = page->mapping;
  627. if (PageSwapBacked(page))
  628. SetPageSwapBacked(newpage);
  629. mapping = page_mapping(page);
  630. if (!mapping)
  631. rc = migrate_page(mapping, newpage, page, mode);
  632. else if (mapping->a_ops->migratepage)
  633. /*
  634. * Most pages have a mapping and most filesystems provide a
  635. * migratepage callback. Anonymous pages are part of swap
  636. * space which also has its own migratepage callback. This
  637. * is the most common path for page migration.
  638. */
  639. rc = mapping->a_ops->migratepage(mapping,
  640. newpage, page, mode);
  641. else
  642. rc = fallback_migrate_page(mapping, newpage, page, mode);
  643. if (rc != MIGRATEPAGE_SUCCESS) {
  644. newpage->mapping = NULL;
  645. } else {
  646. if (remap_swapcache)
  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 remap_swapcache = 1;
  658. struct mem_cgroup *mem;
  659. struct anon_vma *anon_vma = NULL;
  660. if (!trylock_page(page)) {
  661. if (!force || mode == MIGRATE_ASYNC)
  662. goto out;
  663. /*
  664. * It's not safe for direct compaction to call lock_page.
  665. * For example, during page readahead pages are added locked
  666. * to the LRU. Later, when the IO completes the pages are
  667. * marked uptodate and unlocked. However, the queueing
  668. * could be merging multiple pages for one bio (e.g.
  669. * mpage_readpages). If an allocation happens for the
  670. * second or third page, the process can end up locking
  671. * the same page twice and deadlocking. Rather than
  672. * trying to be clever about what pages can be locked,
  673. * avoid the use of lock_page for direct compaction
  674. * altogether.
  675. */
  676. if (current->flags & PF_MEMALLOC)
  677. goto out;
  678. lock_page(page);
  679. }
  680. /* charge against new page */
  681. mem_cgroup_prepare_migration(page, newpage, &mem);
  682. if (PageWriteback(page)) {
  683. /*
  684. * Only in the case of a full synchronous migration is it
  685. * necessary to wait for PageWriteback. In the async case,
  686. * the retry loop is too short and in the sync-light case,
  687. * the overhead of stalling is too much
  688. */
  689. if (mode != MIGRATE_SYNC) {
  690. rc = -EBUSY;
  691. goto uncharge;
  692. }
  693. if (!force)
  694. goto uncharge;
  695. wait_on_page_writeback(page);
  696. }
  697. /*
  698. * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
  699. * we cannot notice that anon_vma is freed while we migrates a page.
  700. * This get_anon_vma() delays freeing anon_vma pointer until the end
  701. * of migration. File cache pages are no problem because of page_lock()
  702. * File Caches may use write_page() or lock_page() in migration, then,
  703. * just care Anon page here.
  704. */
  705. if (PageAnon(page) && !PageKsm(page)) {
  706. /*
  707. * Only page_lock_anon_vma_read() understands the subtleties of
  708. * getting a hold on an anon_vma from outside one of its mms.
  709. */
  710. anon_vma = page_get_anon_vma(page);
  711. if (anon_vma) {
  712. /*
  713. * Anon page
  714. */
  715. } else if (PageSwapCache(page)) {
  716. /*
  717. * We cannot be sure that the anon_vma of an unmapped
  718. * swapcache page is safe to use because we don't
  719. * know in advance if the VMA that this page belonged
  720. * to still exists. If the VMA and others sharing the
  721. * data have been freed, then the anon_vma could
  722. * already be invalid.
  723. *
  724. * To avoid this possibility, swapcache pages get
  725. * migrated but are not remapped when migration
  726. * completes
  727. */
  728. remap_swapcache = 0;
  729. } else {
  730. goto uncharge;
  731. }
  732. }
  733. if (unlikely(balloon_page_movable(page))) {
  734. /*
  735. * A ballooned page does not need any special attention from
  736. * physical to virtual reverse mapping procedures.
  737. * Skip any attempt to unmap PTEs or to remap swap cache,
  738. * in order to avoid burning cycles at rmap level, and perform
  739. * the page migration right away (proteced by page lock).
  740. */
  741. rc = balloon_page_migrate(newpage, page, mode);
  742. goto uncharge;
  743. }
  744. /*
  745. * Corner case handling:
  746. * 1. When a new swap-cache page is read into, it is added to the LRU
  747. * and treated as swapcache but it has no rmap yet.
  748. * Calling try_to_unmap() against a page->mapping==NULL page will
  749. * trigger a BUG. So handle it here.
  750. * 2. An orphaned page (see truncate_complete_page) might have
  751. * fs-private metadata. The page can be picked up due to memory
  752. * offlining. Everywhere else except page reclaim, the page is
  753. * invisible to the vm, so the page can not be migrated. So try to
  754. * free the metadata, so the page can be freed.
  755. */
  756. if (!page->mapping) {
  757. VM_BUG_ON_PAGE(PageAnon(page), page);
  758. if (page_has_private(page)) {
  759. try_to_free_buffers(page);
  760. goto uncharge;
  761. }
  762. goto skip_unmap;
  763. }
  764. /* Establish migration ptes or remove ptes */
  765. try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
  766. skip_unmap:
  767. if (!page_mapped(page))
  768. rc = move_to_new_page(newpage, page, remap_swapcache, mode);
  769. if (rc && remap_swapcache)
  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. uncharge:
  775. mem_cgroup_end_migration(mem, page, newpage,
  776. (rc == MIGRATEPAGE_SUCCESS ||
  777. rc == MIGRATEPAGE_BALLOON_SUCCESS));
  778. unlock_page(page);
  779. out:
  780. return rc;
  781. }
  782. /*
  783. * Obtain the lock on page, remove all ptes and migrate the page
  784. * to the newly allocated page in newpage.
  785. */
  786. static int unmap_and_move(new_page_t get_new_page, unsigned long private,
  787. struct page *page, int force, enum migrate_mode mode)
  788. {
  789. int rc = 0;
  790. int *result = NULL;
  791. struct page *newpage = get_new_page(page, private, &result);
  792. if (!newpage)
  793. return -ENOMEM;
  794. if (page_count(page) == 1) {
  795. /* page was freed from under us. So we are done. */
  796. goto out;
  797. }
  798. if (unlikely(PageTransHuge(page)))
  799. if (unlikely(split_huge_page(page)))
  800. goto out;
  801. rc = __unmap_and_move(page, newpage, force, mode);
  802. if (unlikely(rc == MIGRATEPAGE_BALLOON_SUCCESS)) {
  803. /*
  804. * A ballooned page has been migrated already.
  805. * Now, it's the time to wrap-up counters,
  806. * handle the page back to Buddy and return.
  807. */
  808. dec_zone_page_state(page, NR_ISOLATED_ANON +
  809. page_is_file_cache(page));
  810. balloon_page_free(page);
  811. return MIGRATEPAGE_SUCCESS;
  812. }
  813. out:
  814. if (rc != -EAGAIN) {
  815. /*
  816. * A page that has been migrated has all references
  817. * removed and will be freed. A page that has not been
  818. * migrated will have kepts its references and be
  819. * restored.
  820. */
  821. list_del(&page->lru);
  822. dec_zone_page_state(page, NR_ISOLATED_ANON +
  823. page_is_file_cache(page));
  824. putback_lru_page(page);
  825. }
  826. /*
  827. * Move the new page to the LRU. If migration was not successful
  828. * then this will free the page.
  829. */
  830. putback_lru_page(newpage);
  831. if (result) {
  832. if (rc)
  833. *result = rc;
  834. else
  835. *result = page_to_nid(newpage);
  836. }
  837. return rc;
  838. }
  839. /*
  840. * Counterpart of unmap_and_move_page() for hugepage migration.
  841. *
  842. * This function doesn't wait the completion of hugepage I/O
  843. * because there is no race between I/O and migration for hugepage.
  844. * Note that currently hugepage I/O occurs only in direct I/O
  845. * where no lock is held and PG_writeback is irrelevant,
  846. * and writeback status of all subpages are counted in the reference
  847. * count of the head page (i.e. if all subpages of a 2MB hugepage are
  848. * under direct I/O, the reference of the head page is 512 and a bit more.)
  849. * This means that when we try to migrate hugepage whose subpages are
  850. * doing direct I/O, some references remain after try_to_unmap() and
  851. * hugepage migration fails without data corruption.
  852. *
  853. * There is also no race when direct I/O is issued on the page under migration,
  854. * because then pte is replaced with migration swap entry and direct I/O code
  855. * will wait in the page fault for migration to complete.
  856. */
  857. static int unmap_and_move_huge_page(new_page_t get_new_page,
  858. unsigned long private, struct page *hpage,
  859. int force, enum migrate_mode mode)
  860. {
  861. int rc = 0;
  862. int *result = NULL;
  863. struct page *new_hpage;
  864. struct anon_vma *anon_vma = NULL;
  865. /*
  866. * Movability of hugepages depends on architectures and hugepage size.
  867. * This check is necessary because some callers of hugepage migration
  868. * like soft offline and memory hotremove don't walk through page
  869. * tables or check whether the hugepage is pmd-based or not before
  870. * kicking migration.
  871. */
  872. if (!hugepage_migration_support(page_hstate(hpage))) {
  873. putback_active_hugepage(hpage);
  874. return -ENOSYS;
  875. }
  876. new_hpage = get_new_page(hpage, private, &result);
  877. if (!new_hpage)
  878. return -ENOMEM;
  879. rc = -EAGAIN;
  880. if (!trylock_page(hpage)) {
  881. if (!force || mode != MIGRATE_SYNC)
  882. goto out;
  883. lock_page(hpage);
  884. }
  885. if (PageAnon(hpage))
  886. anon_vma = page_get_anon_vma(hpage);
  887. try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
  888. if (!page_mapped(hpage))
  889. rc = move_to_new_page(new_hpage, hpage, 1, mode);
  890. if (rc)
  891. remove_migration_ptes(hpage, hpage);
  892. if (anon_vma)
  893. put_anon_vma(anon_vma);
  894. if (!rc)
  895. hugetlb_cgroup_migrate(hpage, new_hpage);
  896. unlock_page(hpage);
  897. out:
  898. if (rc != -EAGAIN)
  899. putback_active_hugepage(hpage);
  900. put_page(new_hpage);
  901. if (result) {
  902. if (rc)
  903. *result = rc;
  904. else
  905. *result = page_to_nid(new_hpage);
  906. }
  907. return rc;
  908. }
  909. /*
  910. * migrate_pages - migrate the pages specified in a list, to the free pages
  911. * supplied as the target for the page migration
  912. *
  913. * @from: The list of pages to be migrated.
  914. * @get_new_page: The function used to allocate free pages to be used
  915. * as the target of the page migration.
  916. * @private: Private data to be passed on to get_new_page()
  917. * @mode: The migration mode that specifies the constraints for
  918. * page migration, if any.
  919. * @reason: The reason for page migration.
  920. *
  921. * The function returns after 10 attempts or if no pages are movable any more
  922. * because the list has become empty or no retryable pages exist any more.
  923. * The caller should call putback_lru_pages() to return pages to the LRU
  924. * or free list only if ret != 0.
  925. *
  926. * Returns the number of pages that were not migrated, or an error code.
  927. */
  928. int migrate_pages(struct list_head *from, new_page_t get_new_page,
  929. unsigned long private, enum migrate_mode mode, int reason)
  930. {
  931. int retry = 1;
  932. int nr_failed = 0;
  933. int nr_succeeded = 0;
  934. int pass = 0;
  935. struct page *page;
  936. struct page *page2;
  937. int swapwrite = current->flags & PF_SWAPWRITE;
  938. int rc;
  939. if (!swapwrite)
  940. current->flags |= PF_SWAPWRITE;
  941. for(pass = 0; pass < 10 && retry; pass++) {
  942. retry = 0;
  943. list_for_each_entry_safe(page, page2, from, lru) {
  944. cond_resched();
  945. if (PageHuge(page))
  946. rc = unmap_and_move_huge_page(get_new_page,
  947. private, page, pass > 2, mode);
  948. else
  949. rc = unmap_and_move(get_new_page, private,
  950. page, pass > 2, mode);
  951. switch(rc) {
  952. case -ENOMEM:
  953. goto out;
  954. case -EAGAIN:
  955. retry++;
  956. break;
  957. case MIGRATEPAGE_SUCCESS:
  958. nr_succeeded++;
  959. break;
  960. default:
  961. /*
  962. * Permanent failure (-EBUSY, -ENOSYS, etc.):
  963. * unlike -EAGAIN case, the failed page is
  964. * removed from migration page list and not
  965. * retried in the next outer loop.
  966. */
  967. nr_failed++;
  968. break;
  969. }
  970. }
  971. }
  972. rc = nr_failed + retry;
  973. out:
  974. if (nr_succeeded)
  975. count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
  976. if (nr_failed)
  977. count_vm_events(PGMIGRATE_FAIL, nr_failed);
  978. trace_mm_migrate_pages(nr_succeeded, nr_failed, mode, reason);
  979. if (!swapwrite)
  980. current->flags &= ~PF_SWAPWRITE;
  981. return rc;
  982. }
  983. #ifdef CONFIG_NUMA
  984. /*
  985. * Move a list of individual pages
  986. */
  987. struct page_to_node {
  988. unsigned long addr;
  989. struct page *page;
  990. int node;
  991. int status;
  992. };
  993. static struct page *new_page_node(struct page *p, unsigned long private,
  994. int **result)
  995. {
  996. struct page_to_node *pm = (struct page_to_node *)private;
  997. while (pm->node != MAX_NUMNODES && pm->page != p)
  998. pm++;
  999. if (pm->node == MAX_NUMNODES)
  1000. return NULL;
  1001. *result = &pm->status;
  1002. if (PageHuge(p))
  1003. return alloc_huge_page_node(page_hstate(compound_head(p)),
  1004. pm->node);
  1005. else
  1006. return alloc_pages_exact_node(pm->node,
  1007. GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
  1008. }
  1009. /*
  1010. * Move a set of pages as indicated in the pm array. The addr
  1011. * field must be set to the virtual address of the page to be moved
  1012. * and the node number must contain a valid target node.
  1013. * The pm array ends with node = MAX_NUMNODES.
  1014. */
  1015. static int do_move_page_to_node_array(struct mm_struct *mm,
  1016. struct page_to_node *pm,
  1017. int migrate_all)
  1018. {
  1019. int err;
  1020. struct page_to_node *pp;
  1021. LIST_HEAD(pagelist);
  1022. down_read(&mm->mmap_sem);
  1023. /*
  1024. * Build a list of pages to migrate
  1025. */
  1026. for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
  1027. struct vm_area_struct *vma;
  1028. struct page *page;
  1029. err = -EFAULT;
  1030. vma = find_vma(mm, pp->addr);
  1031. if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
  1032. goto set_status;
  1033. page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT);
  1034. err = PTR_ERR(page);
  1035. if (IS_ERR(page))
  1036. goto set_status;
  1037. err = -ENOENT;
  1038. if (!page)
  1039. goto set_status;
  1040. /* Use PageReserved to check for zero page */
  1041. if (PageReserved(page))
  1042. goto put_and_set;
  1043. pp->page = page;
  1044. err = page_to_nid(page);
  1045. if (err == pp->node)
  1046. /*
  1047. * Node already in the right place
  1048. */
  1049. goto put_and_set;
  1050. err = -EACCES;
  1051. if (page_mapcount(page) > 1 &&
  1052. !migrate_all)
  1053. goto put_and_set;
  1054. if (PageHuge(page)) {
  1055. isolate_huge_page(page, &pagelist);
  1056. goto put_and_set;
  1057. }
  1058. err = isolate_lru_page(page);
  1059. if (!err) {
  1060. list_add_tail(&page->lru, &pagelist);
  1061. inc_zone_page_state(page, NR_ISOLATED_ANON +
  1062. page_is_file_cache(page));
  1063. }
  1064. put_and_set:
  1065. /*
  1066. * Either remove the duplicate refcount from
  1067. * isolate_lru_page() or drop the page ref if it was
  1068. * not isolated.
  1069. */
  1070. put_page(page);
  1071. set_status:
  1072. pp->status = err;
  1073. }
  1074. err = 0;
  1075. if (!list_empty(&pagelist)) {
  1076. err = migrate_pages(&pagelist, new_page_node,
  1077. (unsigned long)pm, MIGRATE_SYNC, MR_SYSCALL);
  1078. if (err)
  1079. putback_movable_pages(&pagelist);
  1080. }
  1081. up_read(&mm->mmap_sem);
  1082. return err;
  1083. }
  1084. /*
  1085. * Migrate an array of page address onto an array of nodes and fill
  1086. * the corresponding array of status.
  1087. */
  1088. static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
  1089. unsigned long nr_pages,
  1090. const void __user * __user *pages,
  1091. const int __user *nodes,
  1092. int __user *status, int flags)
  1093. {
  1094. struct page_to_node *pm;
  1095. unsigned long chunk_nr_pages;
  1096. unsigned long chunk_start;
  1097. int err;
  1098. err = -ENOMEM;
  1099. pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
  1100. if (!pm)
  1101. goto out;
  1102. migrate_prep();
  1103. /*
  1104. * Store a chunk of page_to_node array in a page,
  1105. * but keep the last one as a marker
  1106. */
  1107. chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
  1108. for (chunk_start = 0;
  1109. chunk_start < nr_pages;
  1110. chunk_start += chunk_nr_pages) {
  1111. int j;
  1112. if (chunk_start + chunk_nr_pages > nr_pages)
  1113. chunk_nr_pages = nr_pages - chunk_start;
  1114. /* fill the chunk pm with addrs and nodes from user-space */
  1115. for (j = 0; j < chunk_nr_pages; j++) {
  1116. const void __user *p;
  1117. int node;
  1118. err = -EFAULT;
  1119. if (get_user(p, pages + j + chunk_start))
  1120. goto out_pm;
  1121. pm[j].addr = (unsigned long) p;
  1122. if (get_user(node, nodes + j + chunk_start))
  1123. goto out_pm;
  1124. err = -ENODEV;
  1125. if (node < 0 || node >= MAX_NUMNODES)
  1126. goto out_pm;
  1127. if (!node_state(node, N_MEMORY))
  1128. goto out_pm;
  1129. err = -EACCES;
  1130. if (!node_isset(node, task_nodes))
  1131. goto out_pm;
  1132. pm[j].node = node;
  1133. }
  1134. /* End marker for this chunk */
  1135. pm[chunk_nr_pages].node = MAX_NUMNODES;
  1136. /* Migrate this chunk */
  1137. err = do_move_page_to_node_array(mm, pm,
  1138. flags & MPOL_MF_MOVE_ALL);
  1139. if (err < 0)
  1140. goto out_pm;
  1141. /* Return status information */
  1142. for (j = 0; j < chunk_nr_pages; j++)
  1143. if (put_user(pm[j].status, status + j + chunk_start)) {
  1144. err = -EFAULT;
  1145. goto out_pm;
  1146. }
  1147. }
  1148. err = 0;
  1149. out_pm:
  1150. free_page((unsigned long)pm);
  1151. out:
  1152. return err;
  1153. }
  1154. /*
  1155. * Determine the nodes of an array of pages and store it in an array of status.
  1156. */
  1157. static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
  1158. const void __user **pages, int *status)
  1159. {
  1160. unsigned long i;
  1161. down_read(&mm->mmap_sem);
  1162. for (i = 0; i < nr_pages; i++) {
  1163. unsigned long addr = (unsigned long)(*pages);
  1164. struct vm_area_struct *vma;
  1165. struct page *page;
  1166. int err = -EFAULT;
  1167. vma = find_vma(mm, addr);
  1168. if (!vma || addr < vma->vm_start)
  1169. goto set_status;
  1170. page = follow_page(vma, addr, 0);
  1171. err = PTR_ERR(page);
  1172. if (IS_ERR(page))
  1173. goto set_status;
  1174. err = -ENOENT;
  1175. /* Use PageReserved to check for zero page */
  1176. if (!page || PageReserved(page))
  1177. goto set_status;
  1178. err = page_to_nid(page);
  1179. set_status:
  1180. *status = err;
  1181. pages++;
  1182. status++;
  1183. }
  1184. up_read(&mm->mmap_sem);
  1185. }
  1186. /*
  1187. * Determine the nodes of a user array of pages and store it in
  1188. * a user array of status.
  1189. */
  1190. static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
  1191. const void __user * __user *pages,
  1192. int __user *status)
  1193. {
  1194. #define DO_PAGES_STAT_CHUNK_NR 16
  1195. const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
  1196. int chunk_status[DO_PAGES_STAT_CHUNK_NR];
  1197. while (nr_pages) {
  1198. unsigned long chunk_nr;
  1199. chunk_nr = nr_pages;
  1200. if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
  1201. chunk_nr = DO_PAGES_STAT_CHUNK_NR;
  1202. if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
  1203. break;
  1204. do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
  1205. if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
  1206. break;
  1207. pages += chunk_nr;
  1208. status += chunk_nr;
  1209. nr_pages -= chunk_nr;
  1210. }
  1211. return nr_pages ? -EFAULT : 0;
  1212. }
  1213. /*
  1214. * Move a list of pages in the address space of the currently executing
  1215. * process.
  1216. */
  1217. SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
  1218. const void __user * __user *, pages,
  1219. const int __user *, nodes,
  1220. int __user *, status, int, flags)
  1221. {
  1222. const struct cred *cred = current_cred(), *tcred;
  1223. struct task_struct *task;
  1224. struct mm_struct *mm;
  1225. int err;
  1226. nodemask_t task_nodes;
  1227. /* Check flags */
  1228. if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
  1229. return -EINVAL;
  1230. if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
  1231. return -EPERM;
  1232. /* Find the mm_struct */
  1233. rcu_read_lock();
  1234. task = pid ? find_task_by_vpid(pid) : current;
  1235. if (!task) {
  1236. rcu_read_unlock();
  1237. return -ESRCH;
  1238. }
  1239. get_task_struct(task);
  1240. /*
  1241. * Check if this process has the right to modify the specified
  1242. * process. The right exists if the process has administrative
  1243. * capabilities, superuser privileges or the same
  1244. * userid as the target process.
  1245. */
  1246. tcred = __task_cred(task);
  1247. if (!uid_eq(cred->euid, tcred->suid) && !uid_eq(cred->euid, tcred->uid) &&
  1248. !uid_eq(cred->uid, tcred->suid) && !uid_eq(cred->uid, tcred->uid) &&
  1249. !capable(CAP_SYS_NICE)) {
  1250. rcu_read_unlock();
  1251. err = -EPERM;
  1252. goto out;
  1253. }
  1254. rcu_read_unlock();
  1255. err = security_task_movememory(task);
  1256. if (err)
  1257. goto out;
  1258. task_nodes = cpuset_mems_allowed(task);
  1259. mm = get_task_mm(task);
  1260. put_task_struct(task);
  1261. if (!mm)
  1262. return -EINVAL;
  1263. if (nodes)
  1264. err = do_pages_move(mm, task_nodes, nr_pages, pages,
  1265. nodes, status, flags);
  1266. else
  1267. err = do_pages_stat(mm, nr_pages, pages, status);
  1268. mmput(mm);
  1269. return err;
  1270. out:
  1271. put_task_struct(task);
  1272. return err;
  1273. }
  1274. /*
  1275. * Call migration functions in the vma_ops that may prepare
  1276. * memory in a vm for migration. migration functions may perform
  1277. * the migration for vmas that do not have an underlying page struct.
  1278. */
  1279. int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
  1280. const nodemask_t *from, unsigned long flags)
  1281. {
  1282. struct vm_area_struct *vma;
  1283. int err = 0;
  1284. for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
  1285. if (vma->vm_ops && vma->vm_ops->migrate) {
  1286. err = vma->vm_ops->migrate(vma, to, from, flags);
  1287. if (err)
  1288. break;
  1289. }
  1290. }
  1291. return err;
  1292. }
  1293. #ifdef CONFIG_NUMA_BALANCING
  1294. /*
  1295. * Returns true if this is a safe migration target node for misplaced NUMA
  1296. * pages. Currently it only checks the watermarks which crude
  1297. */
  1298. static bool migrate_balanced_pgdat(struct pglist_data *pgdat,
  1299. unsigned long nr_migrate_pages)
  1300. {
  1301. int z;
  1302. for (z = pgdat->nr_zones - 1; z >= 0; z--) {
  1303. struct zone *zone = pgdat->node_zones + z;
  1304. if (!populated_zone(zone))
  1305. continue;
  1306. if (!zone_reclaimable(zone))
  1307. continue;
  1308. /* Avoid waking kswapd by allocating pages_to_migrate pages. */
  1309. if (!zone_watermark_ok(zone, 0,
  1310. high_wmark_pages(zone) +
  1311. nr_migrate_pages,
  1312. 0, 0))
  1313. continue;
  1314. return true;
  1315. }
  1316. return false;
  1317. }
  1318. static struct page *alloc_misplaced_dst_page(struct page *page,
  1319. unsigned long data,
  1320. int **result)
  1321. {
  1322. int nid = (int) data;
  1323. struct page *newpage;
  1324. newpage = alloc_pages_exact_node(nid,
  1325. (GFP_HIGHUSER_MOVABLE | GFP_THISNODE |
  1326. __GFP_NOMEMALLOC | __GFP_NORETRY |
  1327. __GFP_NOWARN) &
  1328. ~GFP_IOFS, 0);
  1329. return newpage;
  1330. }
  1331. /*
  1332. * page migration rate limiting control.
  1333. * Do not migrate more than @pages_to_migrate in a @migrate_interval_millisecs
  1334. * window of time. Default here says do not migrate more than 1280M per second.
  1335. * If a node is rate-limited then PTE NUMA updates are also rate-limited. However
  1336. * as it is faults that reset the window, pte updates will happen unconditionally
  1337. * if there has not been a fault since @pteupdate_interval_millisecs after the
  1338. * throttle window closed.
  1339. */
  1340. static unsigned int migrate_interval_millisecs __read_mostly = 100;
  1341. static unsigned int pteupdate_interval_millisecs __read_mostly = 1000;
  1342. static unsigned int ratelimit_pages __read_mostly = 128 << (20 - PAGE_SHIFT);
  1343. /* Returns true if NUMA migration is currently rate limited */
  1344. bool migrate_ratelimited(int node)
  1345. {
  1346. pg_data_t *pgdat = NODE_DATA(node);
  1347. if (time_after(jiffies, pgdat->numabalancing_migrate_next_window +
  1348. msecs_to_jiffies(pteupdate_interval_millisecs)))
  1349. return false;
  1350. if (pgdat->numabalancing_migrate_nr_pages < ratelimit_pages)
  1351. return false;
  1352. return true;
  1353. }
  1354. /* Returns true if the node is migrate rate-limited after the update */
  1355. static bool numamigrate_update_ratelimit(pg_data_t *pgdat,
  1356. unsigned long nr_pages)
  1357. {
  1358. /*
  1359. * Rate-limit the amount of data that is being migrated to a node.
  1360. * Optimal placement is no good if the memory bus is saturated and
  1361. * all the time is being spent migrating!
  1362. */
  1363. if (time_after(jiffies, pgdat->numabalancing_migrate_next_window)) {
  1364. spin_lock(&pgdat->numabalancing_migrate_lock);
  1365. pgdat->numabalancing_migrate_nr_pages = 0;
  1366. pgdat->numabalancing_migrate_next_window = jiffies +
  1367. msecs_to_jiffies(migrate_interval_millisecs);
  1368. spin_unlock(&pgdat->numabalancing_migrate_lock);
  1369. }
  1370. if (pgdat->numabalancing_migrate_nr_pages > ratelimit_pages) {
  1371. trace_mm_numa_migrate_ratelimit(current, pgdat->node_id,
  1372. nr_pages);
  1373. return true;
  1374. }
  1375. /*
  1376. * This is an unlocked non-atomic update so errors are possible.
  1377. * The consequences are failing to migrate when we potentiall should
  1378. * have which is not severe enough to warrant locking. If it is ever
  1379. * a problem, it can be converted to a per-cpu counter.
  1380. */
  1381. pgdat->numabalancing_migrate_nr_pages += nr_pages;
  1382. return false;
  1383. }
  1384. static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
  1385. {
  1386. int page_lru;
  1387. VM_BUG_ON_PAGE(compound_order(page) && !PageTransHuge(page), page);
  1388. /* Avoid migrating to a node that is nearly full */
  1389. if (!migrate_balanced_pgdat(pgdat, 1UL << compound_order(page)))
  1390. return 0;
  1391. if (isolate_lru_page(page))
  1392. return 0;
  1393. /*
  1394. * migrate_misplaced_transhuge_page() skips page migration's usual
  1395. * check on page_count(), so we must do it here, now that the page
  1396. * has been isolated: a GUP pin, or any other pin, prevents migration.
  1397. * The expected page count is 3: 1 for page's mapcount and 1 for the
  1398. * caller's pin and 1 for the reference taken by isolate_lru_page().
  1399. */
  1400. if (PageTransHuge(page) && page_count(page) != 3) {
  1401. putback_lru_page(page);
  1402. return 0;
  1403. }
  1404. page_lru = page_is_file_cache(page);
  1405. mod_zone_page_state(page_zone(page), NR_ISOLATED_ANON + page_lru,
  1406. hpage_nr_pages(page));
  1407. /*
  1408. * Isolating the page has taken another reference, so the
  1409. * caller's reference can be safely dropped without the page
  1410. * disappearing underneath us during migration.
  1411. */
  1412. put_page(page);
  1413. return 1;
  1414. }
  1415. bool pmd_trans_migrating(pmd_t pmd)
  1416. {
  1417. struct page *page = pmd_page(pmd);
  1418. return PageLocked(page);
  1419. }
  1420. void wait_migrate_huge_page(struct anon_vma *anon_vma, pmd_t *pmd)
  1421. {
  1422. struct page *page = pmd_page(*pmd);
  1423. wait_on_page_locked(page);
  1424. }
  1425. /*
  1426. * Attempt to migrate a misplaced page to the specified destination
  1427. * node. Caller is expected to have an elevated reference count on
  1428. * the page that will be dropped by this function before returning.
  1429. */
  1430. int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma,
  1431. int node)
  1432. {
  1433. pg_data_t *pgdat = NODE_DATA(node);
  1434. int isolated;
  1435. int nr_remaining;
  1436. LIST_HEAD(migratepages);
  1437. /*
  1438. * Don't migrate file pages that are mapped in multiple processes
  1439. * with execute permissions as they are probably shared libraries.
  1440. */
  1441. if (page_mapcount(page) != 1 && page_is_file_cache(page) &&
  1442. (vma->vm_flags & VM_EXEC))
  1443. goto out;
  1444. /*
  1445. * Rate-limit the amount of data that is being migrated to a node.
  1446. * Optimal placement is no good if the memory bus is saturated and
  1447. * all the time is being spent migrating!
  1448. */
  1449. if (numamigrate_update_ratelimit(pgdat, 1))
  1450. goto out;
  1451. isolated = numamigrate_isolate_page(pgdat, page);
  1452. if (!isolated)
  1453. goto out;
  1454. list_add(&page->lru, &migratepages);
  1455. nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_page,
  1456. node, MIGRATE_ASYNC, MR_NUMA_MISPLACED);
  1457. if (nr_remaining) {
  1458. if (!list_empty(&migratepages)) {
  1459. list_del(&page->lru);
  1460. dec_zone_page_state(page, NR_ISOLATED_ANON +
  1461. page_is_file_cache(page));
  1462. putback_lru_page(page);
  1463. }
  1464. isolated = 0;
  1465. } else
  1466. count_vm_numa_event(NUMA_PAGE_MIGRATE);
  1467. BUG_ON(!list_empty(&migratepages));
  1468. return isolated;
  1469. out:
  1470. put_page(page);
  1471. return 0;
  1472. }
  1473. #endif /* CONFIG_NUMA_BALANCING */
  1474. #if defined(CONFIG_NUMA_BALANCING) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
  1475. /*
  1476. * Migrates a THP to a given target node. page must be locked and is unlocked
  1477. * before returning.
  1478. */
  1479. int migrate_misplaced_transhuge_page(struct mm_struct *mm,
  1480. struct vm_area_struct *vma,
  1481. pmd_t *pmd, pmd_t entry,
  1482. unsigned long address,
  1483. struct page *page, int node)
  1484. {
  1485. spinlock_t *ptl;
  1486. pg_data_t *pgdat = NODE_DATA(node);
  1487. int isolated = 0;
  1488. struct page *new_page = NULL;
  1489. struct mem_cgroup *memcg = NULL;
  1490. int page_lru = page_is_file_cache(page);
  1491. unsigned long mmun_start = address & HPAGE_PMD_MASK;
  1492. unsigned long mmun_end = mmun_start + HPAGE_PMD_SIZE;
  1493. pmd_t orig_entry;
  1494. /*
  1495. * Rate-limit the amount of data that is being migrated to a node.
  1496. * Optimal placement is no good if the memory bus is saturated and
  1497. * all the time is being spent migrating!
  1498. */
  1499. if (numamigrate_update_ratelimit(pgdat, HPAGE_PMD_NR))
  1500. goto out_dropref;
  1501. new_page = alloc_pages_node(node,
  1502. (GFP_TRANSHUGE | GFP_THISNODE) & ~__GFP_WAIT, HPAGE_PMD_ORDER);
  1503. if (!new_page)
  1504. goto out_fail;
  1505. isolated = numamigrate_isolate_page(pgdat, page);
  1506. if (!isolated) {
  1507. put_page(new_page);
  1508. goto out_fail;
  1509. }
  1510. if (mm_tlb_flush_pending(mm))
  1511. flush_tlb_range(vma, mmun_start, mmun_end);
  1512. /* Prepare a page as a migration target */
  1513. __set_page_locked(new_page);
  1514. SetPageSwapBacked(new_page);
  1515. /* anon mapping, we can simply copy page->mapping to the new page: */
  1516. new_page->mapping = page->mapping;
  1517. new_page->index = page->index;
  1518. migrate_page_copy(new_page, page);
  1519. WARN_ON(PageLRU(new_page));
  1520. /* Recheck the target PMD */
  1521. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  1522. ptl = pmd_lock(mm, pmd);
  1523. if (unlikely(!pmd_same(*pmd, entry) || page_count(page) != 2)) {
  1524. fail_putback:
  1525. spin_unlock(ptl);
  1526. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  1527. /* Reverse changes made by migrate_page_copy() */
  1528. if (TestClearPageActive(new_page))
  1529. SetPageActive(page);
  1530. if (TestClearPageUnevictable(new_page))
  1531. SetPageUnevictable(page);
  1532. mlock_migrate_page(page, new_page);
  1533. unlock_page(new_page);
  1534. put_page(new_page); /* Free it */
  1535. /* Retake the callers reference and putback on LRU */
  1536. get_page(page);
  1537. putback_lru_page(page);
  1538. mod_zone_page_state(page_zone(page),
  1539. NR_ISOLATED_ANON + page_lru, -HPAGE_PMD_NR);
  1540. goto out_unlock;
  1541. }
  1542. /*
  1543. * Traditional migration needs to prepare the memcg charge
  1544. * transaction early to prevent the old page from being
  1545. * uncharged when installing migration entries. Here we can
  1546. * save the potential rollback and start the charge transfer
  1547. * only when migration is already known to end successfully.
  1548. */
  1549. mem_cgroup_prepare_migration(page, new_page, &memcg);
  1550. orig_entry = *pmd;
  1551. entry = mk_pmd(new_page, vma->vm_page_prot);
  1552. entry = pmd_mkhuge(entry);
  1553. entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
  1554. /*
  1555. * Clear the old entry under pagetable lock and establish the new PTE.
  1556. * Any parallel GUP will either observe the old page blocking on the
  1557. * page lock, block on the page table lock or observe the new page.
  1558. * The SetPageUptodate on the new page and page_add_new_anon_rmap
  1559. * guarantee the copy is visible before the pagetable update.
  1560. */
  1561. flush_cache_range(vma, mmun_start, mmun_end);
  1562. page_add_new_anon_rmap(new_page, vma, mmun_start);
  1563. pmdp_clear_flush(vma, mmun_start, pmd);
  1564. set_pmd_at(mm, mmun_start, pmd, entry);
  1565. flush_tlb_range(vma, mmun_start, mmun_end);
  1566. update_mmu_cache_pmd(vma, address, &entry);
  1567. if (page_count(page) != 2) {
  1568. set_pmd_at(mm, mmun_start, pmd, orig_entry);
  1569. flush_tlb_range(vma, mmun_start, mmun_end);
  1570. update_mmu_cache_pmd(vma, address, &entry);
  1571. page_remove_rmap(new_page);
  1572. goto fail_putback;
  1573. }
  1574. page_remove_rmap(page);
  1575. /*
  1576. * Finish the charge transaction under the page table lock to
  1577. * prevent split_huge_page() from dividing up the charge
  1578. * before it's fully transferred to the new page.
  1579. */
  1580. mem_cgroup_end_migration(memcg, page, new_page, true);
  1581. spin_unlock(ptl);
  1582. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  1583. unlock_page(new_page);
  1584. unlock_page(page);
  1585. put_page(page); /* Drop the rmap reference */
  1586. put_page(page); /* Drop the LRU isolation reference */
  1587. count_vm_events(PGMIGRATE_SUCCESS, HPAGE_PMD_NR);
  1588. count_vm_numa_events(NUMA_PAGE_MIGRATE, HPAGE_PMD_NR);
  1589. mod_zone_page_state(page_zone(page),
  1590. NR_ISOLATED_ANON + page_lru,
  1591. -HPAGE_PMD_NR);
  1592. return isolated;
  1593. out_fail:
  1594. count_vm_events(PGMIGRATE_FAIL, HPAGE_PMD_NR);
  1595. out_dropref:
  1596. ptl = pmd_lock(mm, pmd);
  1597. if (pmd_same(*pmd, entry)) {
  1598. entry = pmd_mknonnuma(entry);
  1599. set_pmd_at(mm, mmun_start, pmd, entry);
  1600. update_mmu_cache_pmd(vma, address, &entry);
  1601. }
  1602. spin_unlock(ptl);
  1603. out_unlock:
  1604. unlock_page(page);
  1605. put_page(page);
  1606. return 0;
  1607. }
  1608. #endif /* CONFIG_NUMA_BALANCING */
  1609. #endif /* CONFIG_NUMA */