migrate.c 44 KB

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