migrate.c 48 KB

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