migrate.c 44 KB

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