migrate.c 45 KB

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