migrate.c 44 KB

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