migrate.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  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/gfp.h>
  36. #include <asm/tlbflush.h>
  37. #include "internal.h"
  38. /*
  39. * migrate_prep() needs to be called before we start compiling a list of pages
  40. * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is
  41. * undesirable, use migrate_prep_local()
  42. */
  43. int migrate_prep(void)
  44. {
  45. /*
  46. * Clear the LRU lists so pages can be isolated.
  47. * Note that pages may be moved off the LRU after we have
  48. * drained them. Those pages will fail to migrate like other
  49. * pages that may be busy.
  50. */
  51. lru_add_drain_all();
  52. return 0;
  53. }
  54. /* Do the necessary work of migrate_prep but not if it involves other CPUs */
  55. int migrate_prep_local(void)
  56. {
  57. lru_add_drain();
  58. return 0;
  59. }
  60. /*
  61. * Add isolated pages on the list back to the LRU under page lock
  62. * to avoid leaking evictable pages back onto unevictable list.
  63. */
  64. void putback_lru_pages(struct list_head *l)
  65. {
  66. struct page *page;
  67. struct page *page2;
  68. list_for_each_entry_safe(page, page2, l, lru) {
  69. list_del(&page->lru);
  70. dec_zone_page_state(page, NR_ISOLATED_ANON +
  71. page_is_file_cache(page));
  72. putback_lru_page(page);
  73. }
  74. }
  75. /*
  76. * Restore a potential migration pte to a working pte entry
  77. */
  78. static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,
  79. unsigned long addr, void *old)
  80. {
  81. struct mm_struct *mm = vma->vm_mm;
  82. swp_entry_t entry;
  83. pgd_t *pgd;
  84. pud_t *pud;
  85. pmd_t *pmd;
  86. pte_t *ptep, pte;
  87. spinlock_t *ptl;
  88. if (unlikely(PageHuge(new))) {
  89. ptep = huge_pte_offset(mm, addr);
  90. if (!ptep)
  91. goto out;
  92. ptl = &mm->page_table_lock;
  93. } else {
  94. pgd = pgd_offset(mm, addr);
  95. if (!pgd_present(*pgd))
  96. goto out;
  97. pud = pud_offset(pgd, addr);
  98. if (!pud_present(*pud))
  99. goto out;
  100. pmd = pmd_offset(pud, addr);
  101. if (pmd_trans_huge(*pmd))
  102. goto out;
  103. if (!pmd_present(*pmd))
  104. goto out;
  105. ptep = pte_offset_map(pmd, addr);
  106. /*
  107. * Peek to check is_swap_pte() before taking ptlock? No, we
  108. * can race mremap's move_ptes(), which skips anon_vma lock.
  109. */
  110. ptl = pte_lockptr(mm, pmd);
  111. }
  112. spin_lock(ptl);
  113. pte = *ptep;
  114. if (!is_swap_pte(pte))
  115. goto unlock;
  116. entry = pte_to_swp_entry(pte);
  117. if (!is_migration_entry(entry) ||
  118. migration_entry_to_page(entry) != old)
  119. goto unlock;
  120. get_page(new);
  121. pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
  122. if (is_write_migration_entry(entry))
  123. pte = pte_mkwrite(pte);
  124. #ifdef CONFIG_HUGETLB_PAGE
  125. if (PageHuge(new))
  126. pte = pte_mkhuge(pte);
  127. #endif
  128. flush_cache_page(vma, addr, pte_pfn(pte));
  129. set_pte_at(mm, addr, ptep, pte);
  130. if (PageHuge(new)) {
  131. if (PageAnon(new))
  132. hugepage_add_anon_rmap(new, vma, addr);
  133. else
  134. page_dup_rmap(new);
  135. } else if (PageAnon(new))
  136. page_add_anon_rmap(new, vma, addr);
  137. else
  138. page_add_file_rmap(new);
  139. /* No need to invalidate - it was non-present before */
  140. update_mmu_cache(vma, addr, ptep);
  141. unlock:
  142. pte_unmap_unlock(ptep, ptl);
  143. out:
  144. return SWAP_AGAIN;
  145. }
  146. /*
  147. * Get rid of all migration entries and replace them by
  148. * references to the indicated page.
  149. */
  150. static void remove_migration_ptes(struct page *old, struct page *new)
  151. {
  152. rmap_walk(new, remove_migration_pte, old);
  153. }
  154. /*
  155. * Something used the pte of a page under migration. We need to
  156. * get to the page and wait until migration is finished.
  157. * When we return from this function the fault will be retried.
  158. */
  159. void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
  160. unsigned long address)
  161. {
  162. pte_t *ptep, pte;
  163. spinlock_t *ptl;
  164. swp_entry_t entry;
  165. struct page *page;
  166. ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
  167. pte = *ptep;
  168. if (!is_swap_pte(pte))
  169. goto out;
  170. entry = pte_to_swp_entry(pte);
  171. if (!is_migration_entry(entry))
  172. goto out;
  173. page = migration_entry_to_page(entry);
  174. /*
  175. * Once radix-tree replacement of page migration started, page_count
  176. * *must* be zero. And, we don't want to call wait_on_page_locked()
  177. * against a page without get_page().
  178. * So, we use get_page_unless_zero(), here. Even failed, page fault
  179. * will occur again.
  180. */
  181. if (!get_page_unless_zero(page))
  182. goto out;
  183. pte_unmap_unlock(ptep, ptl);
  184. wait_on_page_locked(page);
  185. put_page(page);
  186. return;
  187. out:
  188. pte_unmap_unlock(ptep, ptl);
  189. }
  190. #ifdef CONFIG_BLOCK
  191. /* Returns true if all buffers are successfully locked */
  192. static bool buffer_migrate_lock_buffers(struct buffer_head *head,
  193. enum migrate_mode mode)
  194. {
  195. struct buffer_head *bh = head;
  196. /* Simple case, sync compaction */
  197. if (mode != MIGRATE_ASYNC) {
  198. do {
  199. get_bh(bh);
  200. lock_buffer(bh);
  201. bh = bh->b_this_page;
  202. } while (bh != head);
  203. return true;
  204. }
  205. /* async case, we cannot block on lock_buffer so use trylock_buffer */
  206. do {
  207. get_bh(bh);
  208. if (!trylock_buffer(bh)) {
  209. /*
  210. * We failed to lock the buffer and cannot stall in
  211. * async migration. Release the taken locks
  212. */
  213. struct buffer_head *failed_bh = bh;
  214. put_bh(failed_bh);
  215. bh = head;
  216. while (bh != failed_bh) {
  217. unlock_buffer(bh);
  218. put_bh(bh);
  219. bh = bh->b_this_page;
  220. }
  221. return false;
  222. }
  223. bh = bh->b_this_page;
  224. } while (bh != head);
  225. return true;
  226. }
  227. #else
  228. static inline bool buffer_migrate_lock_buffers(struct buffer_head *head,
  229. enum migrate_mode mode)
  230. {
  231. return true;
  232. }
  233. #endif /* CONFIG_BLOCK */
  234. /*
  235. * Replace the page in the mapping.
  236. *
  237. * The number of remaining references must be:
  238. * 1 for anonymous pages without a mapping
  239. * 2 for pages with a mapping
  240. * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
  241. */
  242. static int migrate_page_move_mapping(struct address_space *mapping,
  243. struct page *newpage, struct page *page,
  244. struct buffer_head *head, enum migrate_mode mode)
  245. {
  246. int expected_count;
  247. void **pslot;
  248. if (!mapping) {
  249. /* Anonymous page without mapping */
  250. if (page_count(page) != 1)
  251. return -EAGAIN;
  252. return 0;
  253. }
  254. spin_lock_irq(&mapping->tree_lock);
  255. pslot = radix_tree_lookup_slot(&mapping->page_tree,
  256. page_index(page));
  257. expected_count = 2 + page_has_private(page);
  258. if (page_count(page) != expected_count ||
  259. radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
  260. spin_unlock_irq(&mapping->tree_lock);
  261. return -EAGAIN;
  262. }
  263. if (!page_freeze_refs(page, expected_count)) {
  264. spin_unlock_irq(&mapping->tree_lock);
  265. return -EAGAIN;
  266. }
  267. /*
  268. * In the async migration case of moving a page with buffers, lock the
  269. * buffers using trylock before the mapping is moved. If the mapping
  270. * was moved, we later failed to lock the buffers and could not move
  271. * the mapping back due to an elevated page count, we would have to
  272. * block waiting on other references to be dropped.
  273. */
  274. if (mode == MIGRATE_ASYNC && head &&
  275. !buffer_migrate_lock_buffers(head, mode)) {
  276. page_unfreeze_refs(page, expected_count);
  277. spin_unlock_irq(&mapping->tree_lock);
  278. return -EAGAIN;
  279. }
  280. /*
  281. * Now we know that no one else is looking at the page.
  282. */
  283. get_page(newpage); /* add cache reference */
  284. if (PageSwapCache(page)) {
  285. SetPageSwapCache(newpage);
  286. set_page_private(newpage, page_private(page));
  287. }
  288. radix_tree_replace_slot(pslot, newpage);
  289. /*
  290. * Drop cache reference from old page by unfreezing
  291. * to one less reference.
  292. * We know this isn't the last reference.
  293. */
  294. page_unfreeze_refs(page, expected_count - 1);
  295. /*
  296. * If moved to a different zone then also account
  297. * the page for that zone. Other VM counters will be
  298. * taken care of when we establish references to the
  299. * new page and drop references to the old page.
  300. *
  301. * Note that anonymous pages are accounted for
  302. * via NR_FILE_PAGES and NR_ANON_PAGES if they
  303. * are mapped to swap space.
  304. */
  305. __dec_zone_page_state(page, NR_FILE_PAGES);
  306. __inc_zone_page_state(newpage, NR_FILE_PAGES);
  307. if (!PageSwapCache(page) && PageSwapBacked(page)) {
  308. __dec_zone_page_state(page, NR_SHMEM);
  309. __inc_zone_page_state(newpage, NR_SHMEM);
  310. }
  311. spin_unlock_irq(&mapping->tree_lock);
  312. return 0;
  313. }
  314. /*
  315. * The expected number of remaining references is the same as that
  316. * of migrate_page_move_mapping().
  317. */
  318. int migrate_huge_page_move_mapping(struct address_space *mapping,
  319. struct page *newpage, struct page *page)
  320. {
  321. int expected_count;
  322. void **pslot;
  323. if (!mapping) {
  324. if (page_count(page) != 1)
  325. return -EAGAIN;
  326. return 0;
  327. }
  328. spin_lock_irq(&mapping->tree_lock);
  329. pslot = radix_tree_lookup_slot(&mapping->page_tree,
  330. page_index(page));
  331. expected_count = 2 + page_has_private(page);
  332. if (page_count(page) != expected_count ||
  333. radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
  334. spin_unlock_irq(&mapping->tree_lock);
  335. return -EAGAIN;
  336. }
  337. if (!page_freeze_refs(page, expected_count)) {
  338. spin_unlock_irq(&mapping->tree_lock);
  339. return -EAGAIN;
  340. }
  341. get_page(newpage);
  342. radix_tree_replace_slot(pslot, newpage);
  343. page_unfreeze_refs(page, expected_count - 1);
  344. spin_unlock_irq(&mapping->tree_lock);
  345. return 0;
  346. }
  347. /*
  348. * Copy the page to its new location
  349. */
  350. void migrate_page_copy(struct page *newpage, struct page *page)
  351. {
  352. if (PageHuge(page))
  353. copy_huge_page(newpage, page);
  354. else
  355. copy_highpage(newpage, page);
  356. if (PageError(page))
  357. SetPageError(newpage);
  358. if (PageReferenced(page))
  359. SetPageReferenced(newpage);
  360. if (PageUptodate(page))
  361. SetPageUptodate(newpage);
  362. if (TestClearPageActive(page)) {
  363. VM_BUG_ON(PageUnevictable(page));
  364. SetPageActive(newpage);
  365. } else if (TestClearPageUnevictable(page))
  366. SetPageUnevictable(newpage);
  367. if (PageChecked(page))
  368. SetPageChecked(newpage);
  369. if (PageMappedToDisk(page))
  370. SetPageMappedToDisk(newpage);
  371. if (PageDirty(page)) {
  372. clear_page_dirty_for_io(page);
  373. /*
  374. * Want to mark the page and the radix tree as dirty, and
  375. * redo the accounting that clear_page_dirty_for_io undid,
  376. * but we can't use set_page_dirty because that function
  377. * is actually a signal that all of the page has become dirty.
  378. * Whereas only part of our page may be dirty.
  379. */
  380. if (PageSwapBacked(page))
  381. SetPageDirty(newpage);
  382. else
  383. __set_page_dirty_nobuffers(newpage);
  384. }
  385. mlock_migrate_page(newpage, page);
  386. ksm_migrate_page(newpage, page);
  387. ClearPageSwapCache(page);
  388. ClearPagePrivate(page);
  389. set_page_private(page, 0);
  390. /*
  391. * If any waiters have accumulated on the new page then
  392. * wake them up.
  393. */
  394. if (PageWriteback(newpage))
  395. end_page_writeback(newpage);
  396. }
  397. /************************************************************
  398. * Migration functions
  399. ***********************************************************/
  400. /* Always fail migration. Used for mappings that are not movable */
  401. int fail_migrate_page(struct address_space *mapping,
  402. struct page *newpage, struct page *page)
  403. {
  404. return -EIO;
  405. }
  406. EXPORT_SYMBOL(fail_migrate_page);
  407. /*
  408. * Common logic to directly migrate a single page suitable for
  409. * pages that do not use PagePrivate/PagePrivate2.
  410. *
  411. * Pages are locked upon entry and exit.
  412. */
  413. int migrate_page(struct address_space *mapping,
  414. struct page *newpage, struct page *page,
  415. enum migrate_mode mode)
  416. {
  417. int rc;
  418. BUG_ON(PageWriteback(page)); /* Writeback must be complete */
  419. rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode);
  420. if (rc)
  421. return rc;
  422. migrate_page_copy(newpage, page);
  423. return 0;
  424. }
  425. EXPORT_SYMBOL(migrate_page);
  426. #ifdef CONFIG_BLOCK
  427. /*
  428. * Migration function for pages with buffers. This function can only be used
  429. * if the underlying filesystem guarantees that no other references to "page"
  430. * exist.
  431. */
  432. int buffer_migrate_page(struct address_space *mapping,
  433. struct page *newpage, struct page *page, enum migrate_mode mode)
  434. {
  435. struct buffer_head *bh, *head;
  436. int rc;
  437. if (!page_has_buffers(page))
  438. return migrate_page(mapping, newpage, page, mode);
  439. head = page_buffers(page);
  440. rc = migrate_page_move_mapping(mapping, newpage, page, head, mode);
  441. if (rc)
  442. return rc;
  443. /*
  444. * In the async case, migrate_page_move_mapping locked the buffers
  445. * with an IRQ-safe spinlock held. In the sync case, the buffers
  446. * need to be locked now
  447. */
  448. if (mode != MIGRATE_ASYNC)
  449. BUG_ON(!buffer_migrate_lock_buffers(head, mode));
  450. ClearPagePrivate(page);
  451. set_page_private(newpage, page_private(page));
  452. set_page_private(page, 0);
  453. put_page(page);
  454. get_page(newpage);
  455. bh = head;
  456. do {
  457. set_bh_page(bh, newpage, bh_offset(bh));
  458. bh = bh->b_this_page;
  459. } while (bh != head);
  460. SetPagePrivate(newpage);
  461. migrate_page_copy(newpage, page);
  462. bh = head;
  463. do {
  464. unlock_buffer(bh);
  465. put_bh(bh);
  466. bh = bh->b_this_page;
  467. } while (bh != head);
  468. return 0;
  469. }
  470. EXPORT_SYMBOL(buffer_migrate_page);
  471. #endif
  472. /*
  473. * Writeback a page to clean the dirty state
  474. */
  475. static int writeout(struct address_space *mapping, struct page *page)
  476. {
  477. struct writeback_control wbc = {
  478. .sync_mode = WB_SYNC_NONE,
  479. .nr_to_write = 1,
  480. .range_start = 0,
  481. .range_end = LLONG_MAX,
  482. .for_reclaim = 1
  483. };
  484. int rc;
  485. if (!mapping->a_ops->writepage)
  486. /* No write method for the address space */
  487. return -EINVAL;
  488. if (!clear_page_dirty_for_io(page))
  489. /* Someone else already triggered a write */
  490. return -EAGAIN;
  491. /*
  492. * A dirty page may imply that the underlying filesystem has
  493. * the page on some queue. So the page must be clean for
  494. * migration. Writeout may mean we loose the lock and the
  495. * page state is no longer what we checked for earlier.
  496. * At this point we know that the migration attempt cannot
  497. * be successful.
  498. */
  499. remove_migration_ptes(page, page);
  500. rc = mapping->a_ops->writepage(page, &wbc);
  501. if (rc != AOP_WRITEPAGE_ACTIVATE)
  502. /* unlocked. Relock */
  503. lock_page(page);
  504. return (rc < 0) ? -EIO : -EAGAIN;
  505. }
  506. /*
  507. * Default handling if a filesystem does not provide a migration function.
  508. */
  509. static int fallback_migrate_page(struct address_space *mapping,
  510. struct page *newpage, struct page *page, enum migrate_mode mode)
  511. {
  512. if (PageDirty(page)) {
  513. /* Only writeback pages in full synchronous migration */
  514. if (mode != MIGRATE_SYNC)
  515. return -EBUSY;
  516. return writeout(mapping, page);
  517. }
  518. /*
  519. * Buffers may be managed in a filesystem specific way.
  520. * We must have no buffers or drop them.
  521. */
  522. if (page_has_private(page) &&
  523. !try_to_release_page(page, GFP_KERNEL))
  524. return -EAGAIN;
  525. return migrate_page(mapping, newpage, page, mode);
  526. }
  527. /*
  528. * Move a page to a newly allocated page
  529. * The page is locked and all ptes have been successfully removed.
  530. *
  531. * The new page will have replaced the old page if this function
  532. * is successful.
  533. *
  534. * Return value:
  535. * < 0 - error code
  536. * == 0 - success
  537. */
  538. static int move_to_new_page(struct page *newpage, struct page *page,
  539. int remap_swapcache, enum migrate_mode mode)
  540. {
  541. struct address_space *mapping;
  542. int rc;
  543. /*
  544. * Block others from accessing the page when we get around to
  545. * establishing additional references. We are the only one
  546. * holding a reference to the new page at this point.
  547. */
  548. if (!trylock_page(newpage))
  549. BUG();
  550. /* Prepare mapping for the new page.*/
  551. newpage->index = page->index;
  552. newpage->mapping = page->mapping;
  553. if (PageSwapBacked(page))
  554. SetPageSwapBacked(newpage);
  555. mapping = page_mapping(page);
  556. if (!mapping)
  557. rc = migrate_page(mapping, newpage, page, mode);
  558. else if (mapping->a_ops->migratepage)
  559. /*
  560. * Most pages have a mapping and most filesystems provide a
  561. * migratepage callback. Anonymous pages are part of swap
  562. * space which also has its own migratepage callback. This
  563. * is the most common path for page migration.
  564. */
  565. rc = mapping->a_ops->migratepage(mapping,
  566. newpage, page, mode);
  567. else
  568. rc = fallback_migrate_page(mapping, newpage, page, mode);
  569. if (rc) {
  570. newpage->mapping = NULL;
  571. } else {
  572. if (remap_swapcache)
  573. remove_migration_ptes(page, newpage);
  574. page->mapping = NULL;
  575. }
  576. unlock_page(newpage);
  577. return rc;
  578. }
  579. static int __unmap_and_move(struct page *page, struct page *newpage,
  580. int force, bool offlining, enum migrate_mode mode)
  581. {
  582. int rc = -EAGAIN;
  583. int remap_swapcache = 1;
  584. int charge = 0;
  585. struct mem_cgroup *mem;
  586. struct anon_vma *anon_vma = NULL;
  587. if (!trylock_page(page)) {
  588. if (!force || mode == MIGRATE_ASYNC)
  589. goto out;
  590. /*
  591. * It's not safe for direct compaction to call lock_page.
  592. * For example, during page readahead pages are added locked
  593. * to the LRU. Later, when the IO completes the pages are
  594. * marked uptodate and unlocked. However, the queueing
  595. * could be merging multiple pages for one bio (e.g.
  596. * mpage_readpages). If an allocation happens for the
  597. * second or third page, the process can end up locking
  598. * the same page twice and deadlocking. Rather than
  599. * trying to be clever about what pages can be locked,
  600. * avoid the use of lock_page for direct compaction
  601. * altogether.
  602. */
  603. if (current->flags & PF_MEMALLOC)
  604. goto out;
  605. lock_page(page);
  606. }
  607. /*
  608. * Only memory hotplug's offline_pages() caller has locked out KSM,
  609. * and can safely migrate a KSM page. The other cases have skipped
  610. * PageKsm along with PageReserved - but it is only now when we have
  611. * the page lock that we can be certain it will not go KSM beneath us
  612. * (KSM will not upgrade a page from PageAnon to PageKsm when it sees
  613. * its pagecount raised, but only here do we take the page lock which
  614. * serializes that).
  615. */
  616. if (PageKsm(page) && !offlining) {
  617. rc = -EBUSY;
  618. goto unlock;
  619. }
  620. /* charge against new page */
  621. charge = mem_cgroup_prepare_migration(page, newpage, &mem, GFP_KERNEL);
  622. if (charge == -ENOMEM) {
  623. rc = -ENOMEM;
  624. goto unlock;
  625. }
  626. BUG_ON(charge);
  627. if (PageWriteback(page)) {
  628. /*
  629. * Only in the case of a full syncronous migration is it
  630. * necessary to wait for PageWriteback. In the async case,
  631. * the retry loop is too short and in the sync-light case,
  632. * the overhead of stalling is too much
  633. */
  634. if (mode != MIGRATE_SYNC) {
  635. rc = -EBUSY;
  636. goto uncharge;
  637. }
  638. if (!force)
  639. goto uncharge;
  640. wait_on_page_writeback(page);
  641. }
  642. /*
  643. * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
  644. * we cannot notice that anon_vma is freed while we migrates a page.
  645. * This get_anon_vma() delays freeing anon_vma pointer until the end
  646. * of migration. File cache pages are no problem because of page_lock()
  647. * File Caches may use write_page() or lock_page() in migration, then,
  648. * just care Anon page here.
  649. */
  650. if (PageAnon(page)) {
  651. /*
  652. * Only page_lock_anon_vma() understands the subtleties of
  653. * getting a hold on an anon_vma from outside one of its mms.
  654. */
  655. anon_vma = page_get_anon_vma(page);
  656. if (anon_vma) {
  657. /*
  658. * Anon page
  659. */
  660. } else if (PageSwapCache(page)) {
  661. /*
  662. * We cannot be sure that the anon_vma of an unmapped
  663. * swapcache page is safe to use because we don't
  664. * know in advance if the VMA that this page belonged
  665. * to still exists. If the VMA and others sharing the
  666. * data have been freed, then the anon_vma could
  667. * already be invalid.
  668. *
  669. * To avoid this possibility, swapcache pages get
  670. * migrated but are not remapped when migration
  671. * completes
  672. */
  673. remap_swapcache = 0;
  674. } else {
  675. goto uncharge;
  676. }
  677. }
  678. /*
  679. * Corner case handling:
  680. * 1. When a new swap-cache page is read into, it is added to the LRU
  681. * and treated as swapcache but it has no rmap yet.
  682. * Calling try_to_unmap() against a page->mapping==NULL page will
  683. * trigger a BUG. So handle it here.
  684. * 2. An orphaned page (see truncate_complete_page) might have
  685. * fs-private metadata. The page can be picked up due to memory
  686. * offlining. Everywhere else except page reclaim, the page is
  687. * invisible to the vm, so the page can not be migrated. So try to
  688. * free the metadata, so the page can be freed.
  689. */
  690. if (!page->mapping) {
  691. VM_BUG_ON(PageAnon(page));
  692. if (page_has_private(page)) {
  693. try_to_free_buffers(page);
  694. goto uncharge;
  695. }
  696. goto skip_unmap;
  697. }
  698. /* Establish migration ptes or remove ptes */
  699. try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
  700. skip_unmap:
  701. if (!page_mapped(page))
  702. rc = move_to_new_page(newpage, page, remap_swapcache, mode);
  703. if (rc && remap_swapcache)
  704. remove_migration_ptes(page, page);
  705. /* Drop an anon_vma reference if we took one */
  706. if (anon_vma)
  707. put_anon_vma(anon_vma);
  708. uncharge:
  709. if (!charge)
  710. mem_cgroup_end_migration(mem, page, newpage, rc == 0);
  711. unlock:
  712. unlock_page(page);
  713. out:
  714. return rc;
  715. }
  716. /*
  717. * Obtain the lock on page, remove all ptes and migrate the page
  718. * to the newly allocated page in newpage.
  719. */
  720. static int unmap_and_move(new_page_t get_new_page, unsigned long private,
  721. struct page *page, int force, bool offlining,
  722. enum migrate_mode mode)
  723. {
  724. int rc = 0;
  725. int *result = NULL;
  726. struct page *newpage = get_new_page(page, private, &result);
  727. if (!newpage)
  728. return -ENOMEM;
  729. if (page_count(page) == 1) {
  730. /* page was freed from under us. So we are done. */
  731. goto out;
  732. }
  733. if (unlikely(PageTransHuge(page)))
  734. if (unlikely(split_huge_page(page)))
  735. goto out;
  736. rc = __unmap_and_move(page, newpage, force, offlining, mode);
  737. out:
  738. if (rc != -EAGAIN) {
  739. /*
  740. * A page that has been migrated has all references
  741. * removed and will be freed. A page that has not been
  742. * migrated will have kepts its references and be
  743. * restored.
  744. */
  745. list_del(&page->lru);
  746. dec_zone_page_state(page, NR_ISOLATED_ANON +
  747. page_is_file_cache(page));
  748. putback_lru_page(page);
  749. }
  750. /*
  751. * Move the new page to the LRU. If migration was not successful
  752. * then this will free the page.
  753. */
  754. putback_lru_page(newpage);
  755. if (result) {
  756. if (rc)
  757. *result = rc;
  758. else
  759. *result = page_to_nid(newpage);
  760. }
  761. return rc;
  762. }
  763. /*
  764. * Counterpart of unmap_and_move_page() for hugepage migration.
  765. *
  766. * This function doesn't wait the completion of hugepage I/O
  767. * because there is no race between I/O and migration for hugepage.
  768. * Note that currently hugepage I/O occurs only in direct I/O
  769. * where no lock is held and PG_writeback is irrelevant,
  770. * and writeback status of all subpages are counted in the reference
  771. * count of the head page (i.e. if all subpages of a 2MB hugepage are
  772. * under direct I/O, the reference of the head page is 512 and a bit more.)
  773. * This means that when we try to migrate hugepage whose subpages are
  774. * doing direct I/O, some references remain after try_to_unmap() and
  775. * hugepage migration fails without data corruption.
  776. *
  777. * There is also no race when direct I/O is issued on the page under migration,
  778. * because then pte is replaced with migration swap entry and direct I/O code
  779. * will wait in the page fault for migration to complete.
  780. */
  781. static int unmap_and_move_huge_page(new_page_t get_new_page,
  782. unsigned long private, struct page *hpage,
  783. int force, bool offlining,
  784. enum migrate_mode mode)
  785. {
  786. int rc = 0;
  787. int *result = NULL;
  788. struct page *new_hpage = get_new_page(hpage, private, &result);
  789. struct anon_vma *anon_vma = NULL;
  790. if (!new_hpage)
  791. return -ENOMEM;
  792. rc = -EAGAIN;
  793. if (!trylock_page(hpage)) {
  794. if (!force || mode != MIGRATE_SYNC)
  795. goto out;
  796. lock_page(hpage);
  797. }
  798. if (PageAnon(hpage))
  799. anon_vma = page_get_anon_vma(hpage);
  800. try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
  801. if (!page_mapped(hpage))
  802. rc = move_to_new_page(new_hpage, hpage, 1, mode);
  803. if (rc)
  804. remove_migration_ptes(hpage, hpage);
  805. if (anon_vma)
  806. put_anon_vma(anon_vma);
  807. unlock_page(hpage);
  808. out:
  809. if (rc != -EAGAIN) {
  810. list_del(&hpage->lru);
  811. put_page(hpage);
  812. }
  813. put_page(new_hpage);
  814. if (result) {
  815. if (rc)
  816. *result = rc;
  817. else
  818. *result = page_to_nid(new_hpage);
  819. }
  820. return rc;
  821. }
  822. /*
  823. * migrate_pages
  824. *
  825. * The function takes one list of pages to migrate and a function
  826. * that determines from the page to be migrated and the private data
  827. * the target of the move and allocates the page.
  828. *
  829. * The function returns after 10 attempts or if no pages
  830. * are movable anymore because to has become empty
  831. * or no retryable pages exist anymore.
  832. * Caller should call putback_lru_pages to return pages to the LRU
  833. * or free list only if ret != 0.
  834. *
  835. * Return: Number of pages not migrated or error code.
  836. */
  837. int migrate_pages(struct list_head *from,
  838. new_page_t get_new_page, unsigned long private, bool offlining,
  839. enum migrate_mode mode)
  840. {
  841. int retry = 1;
  842. int nr_failed = 0;
  843. int pass = 0;
  844. struct page *page;
  845. struct page *page2;
  846. int swapwrite = current->flags & PF_SWAPWRITE;
  847. int rc;
  848. if (!swapwrite)
  849. current->flags |= PF_SWAPWRITE;
  850. for(pass = 0; pass < 10 && retry; pass++) {
  851. retry = 0;
  852. list_for_each_entry_safe(page, page2, from, lru) {
  853. cond_resched();
  854. rc = unmap_and_move(get_new_page, private,
  855. page, pass > 2, offlining,
  856. mode);
  857. switch(rc) {
  858. case -ENOMEM:
  859. goto out;
  860. case -EAGAIN:
  861. retry++;
  862. break;
  863. case 0:
  864. break;
  865. default:
  866. /* Permanent failure */
  867. nr_failed++;
  868. break;
  869. }
  870. }
  871. }
  872. rc = 0;
  873. out:
  874. if (!swapwrite)
  875. current->flags &= ~PF_SWAPWRITE;
  876. if (rc)
  877. return rc;
  878. return nr_failed + retry;
  879. }
  880. int migrate_huge_pages(struct list_head *from,
  881. new_page_t get_new_page, unsigned long private, bool offlining,
  882. enum migrate_mode mode)
  883. {
  884. int retry = 1;
  885. int nr_failed = 0;
  886. int pass = 0;
  887. struct page *page;
  888. struct page *page2;
  889. int rc;
  890. for (pass = 0; pass < 10 && retry; pass++) {
  891. retry = 0;
  892. list_for_each_entry_safe(page, page2, from, lru) {
  893. cond_resched();
  894. rc = unmap_and_move_huge_page(get_new_page,
  895. private, page, pass > 2, offlining,
  896. mode);
  897. switch(rc) {
  898. case -ENOMEM:
  899. goto out;
  900. case -EAGAIN:
  901. retry++;
  902. break;
  903. case 0:
  904. break;
  905. default:
  906. /* Permanent failure */
  907. nr_failed++;
  908. break;
  909. }
  910. }
  911. }
  912. rc = 0;
  913. out:
  914. if (rc)
  915. return rc;
  916. return nr_failed + retry;
  917. }
  918. #ifdef CONFIG_NUMA
  919. /*
  920. * Move a list of individual pages
  921. */
  922. struct page_to_node {
  923. unsigned long addr;
  924. struct page *page;
  925. int node;
  926. int status;
  927. };
  928. static struct page *new_page_node(struct page *p, unsigned long private,
  929. int **result)
  930. {
  931. struct page_to_node *pm = (struct page_to_node *)private;
  932. while (pm->node != MAX_NUMNODES && pm->page != p)
  933. pm++;
  934. if (pm->node == MAX_NUMNODES)
  935. return NULL;
  936. *result = &pm->status;
  937. return alloc_pages_exact_node(pm->node,
  938. GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
  939. }
  940. /*
  941. * Move a set of pages as indicated in the pm array. The addr
  942. * field must be set to the virtual address of the page to be moved
  943. * and the node number must contain a valid target node.
  944. * The pm array ends with node = MAX_NUMNODES.
  945. */
  946. static int do_move_page_to_node_array(struct mm_struct *mm,
  947. struct page_to_node *pm,
  948. int migrate_all)
  949. {
  950. int err;
  951. struct page_to_node *pp;
  952. LIST_HEAD(pagelist);
  953. down_read(&mm->mmap_sem);
  954. /*
  955. * Build a list of pages to migrate
  956. */
  957. for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
  958. struct vm_area_struct *vma;
  959. struct page *page;
  960. err = -EFAULT;
  961. vma = find_vma(mm, pp->addr);
  962. if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
  963. goto set_status;
  964. page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT);
  965. err = PTR_ERR(page);
  966. if (IS_ERR(page))
  967. goto set_status;
  968. err = -ENOENT;
  969. if (!page)
  970. goto set_status;
  971. /* Use PageReserved to check for zero page */
  972. if (PageReserved(page) || PageKsm(page))
  973. goto put_and_set;
  974. pp->page = page;
  975. err = page_to_nid(page);
  976. if (err == pp->node)
  977. /*
  978. * Node already in the right place
  979. */
  980. goto put_and_set;
  981. err = -EACCES;
  982. if (page_mapcount(page) > 1 &&
  983. !migrate_all)
  984. goto put_and_set;
  985. err = isolate_lru_page(page);
  986. if (!err) {
  987. list_add_tail(&page->lru, &pagelist);
  988. inc_zone_page_state(page, NR_ISOLATED_ANON +
  989. page_is_file_cache(page));
  990. }
  991. put_and_set:
  992. /*
  993. * Either remove the duplicate refcount from
  994. * isolate_lru_page() or drop the page ref if it was
  995. * not isolated.
  996. */
  997. put_page(page);
  998. set_status:
  999. pp->status = err;
  1000. }
  1001. err = 0;
  1002. if (!list_empty(&pagelist)) {
  1003. err = migrate_pages(&pagelist, new_page_node,
  1004. (unsigned long)pm, 0, MIGRATE_SYNC);
  1005. if (err)
  1006. putback_lru_pages(&pagelist);
  1007. }
  1008. up_read(&mm->mmap_sem);
  1009. return err;
  1010. }
  1011. /*
  1012. * Migrate an array of page address onto an array of nodes and fill
  1013. * the corresponding array of status.
  1014. */
  1015. static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
  1016. unsigned long nr_pages,
  1017. const void __user * __user *pages,
  1018. const int __user *nodes,
  1019. int __user *status, int flags)
  1020. {
  1021. struct page_to_node *pm;
  1022. unsigned long chunk_nr_pages;
  1023. unsigned long chunk_start;
  1024. int err;
  1025. err = -ENOMEM;
  1026. pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
  1027. if (!pm)
  1028. goto out;
  1029. migrate_prep();
  1030. /*
  1031. * Store a chunk of page_to_node array in a page,
  1032. * but keep the last one as a marker
  1033. */
  1034. chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
  1035. for (chunk_start = 0;
  1036. chunk_start < nr_pages;
  1037. chunk_start += chunk_nr_pages) {
  1038. int j;
  1039. if (chunk_start + chunk_nr_pages > nr_pages)
  1040. chunk_nr_pages = nr_pages - chunk_start;
  1041. /* fill the chunk pm with addrs and nodes from user-space */
  1042. for (j = 0; j < chunk_nr_pages; j++) {
  1043. const void __user *p;
  1044. int node;
  1045. err = -EFAULT;
  1046. if (get_user(p, pages + j + chunk_start))
  1047. goto out_pm;
  1048. pm[j].addr = (unsigned long) p;
  1049. if (get_user(node, nodes + j + chunk_start))
  1050. goto out_pm;
  1051. err = -ENODEV;
  1052. if (node < 0 || node >= MAX_NUMNODES)
  1053. goto out_pm;
  1054. if (!node_state(node, N_HIGH_MEMORY))
  1055. goto out_pm;
  1056. err = -EACCES;
  1057. if (!node_isset(node, task_nodes))
  1058. goto out_pm;
  1059. pm[j].node = node;
  1060. }
  1061. /* End marker for this chunk */
  1062. pm[chunk_nr_pages].node = MAX_NUMNODES;
  1063. /* Migrate this chunk */
  1064. err = do_move_page_to_node_array(mm, pm,
  1065. flags & MPOL_MF_MOVE_ALL);
  1066. if (err < 0)
  1067. goto out_pm;
  1068. /* Return status information */
  1069. for (j = 0; j < chunk_nr_pages; j++)
  1070. if (put_user(pm[j].status, status + j + chunk_start)) {
  1071. err = -EFAULT;
  1072. goto out_pm;
  1073. }
  1074. }
  1075. err = 0;
  1076. out_pm:
  1077. free_page((unsigned long)pm);
  1078. out:
  1079. return err;
  1080. }
  1081. /*
  1082. * Determine the nodes of an array of pages and store it in an array of status.
  1083. */
  1084. static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
  1085. const void __user **pages, int *status)
  1086. {
  1087. unsigned long i;
  1088. down_read(&mm->mmap_sem);
  1089. for (i = 0; i < nr_pages; i++) {
  1090. unsigned long addr = (unsigned long)(*pages);
  1091. struct vm_area_struct *vma;
  1092. struct page *page;
  1093. int err = -EFAULT;
  1094. vma = find_vma(mm, addr);
  1095. if (!vma || addr < vma->vm_start)
  1096. goto set_status;
  1097. page = follow_page(vma, addr, 0);
  1098. err = PTR_ERR(page);
  1099. if (IS_ERR(page))
  1100. goto set_status;
  1101. err = -ENOENT;
  1102. /* Use PageReserved to check for zero page */
  1103. if (!page || PageReserved(page) || PageKsm(page))
  1104. goto set_status;
  1105. err = page_to_nid(page);
  1106. set_status:
  1107. *status = err;
  1108. pages++;
  1109. status++;
  1110. }
  1111. up_read(&mm->mmap_sem);
  1112. }
  1113. /*
  1114. * Determine the nodes of a user array of pages and store it in
  1115. * a user array of status.
  1116. */
  1117. static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
  1118. const void __user * __user *pages,
  1119. int __user *status)
  1120. {
  1121. #define DO_PAGES_STAT_CHUNK_NR 16
  1122. const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
  1123. int chunk_status[DO_PAGES_STAT_CHUNK_NR];
  1124. while (nr_pages) {
  1125. unsigned long chunk_nr;
  1126. chunk_nr = nr_pages;
  1127. if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
  1128. chunk_nr = DO_PAGES_STAT_CHUNK_NR;
  1129. if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
  1130. break;
  1131. do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
  1132. if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
  1133. break;
  1134. pages += chunk_nr;
  1135. status += chunk_nr;
  1136. nr_pages -= chunk_nr;
  1137. }
  1138. return nr_pages ? -EFAULT : 0;
  1139. }
  1140. /*
  1141. * Move a list of pages in the address space of the currently executing
  1142. * process.
  1143. */
  1144. SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
  1145. const void __user * __user *, pages,
  1146. const int __user *, nodes,
  1147. int __user *, status, int, flags)
  1148. {
  1149. const struct cred *cred = current_cred(), *tcred;
  1150. struct task_struct *task;
  1151. struct mm_struct *mm;
  1152. int err;
  1153. nodemask_t task_nodes;
  1154. /* Check flags */
  1155. if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
  1156. return -EINVAL;
  1157. if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
  1158. return -EPERM;
  1159. /* Find the mm_struct */
  1160. rcu_read_lock();
  1161. task = pid ? find_task_by_vpid(pid) : current;
  1162. if (!task) {
  1163. rcu_read_unlock();
  1164. return -ESRCH;
  1165. }
  1166. get_task_struct(task);
  1167. /*
  1168. * Check if this process has the right to modify the specified
  1169. * process. The right exists if the process has administrative
  1170. * capabilities, superuser privileges or the same
  1171. * userid as the target process.
  1172. */
  1173. tcred = __task_cred(task);
  1174. if (!uid_eq(cred->euid, tcred->suid) && !uid_eq(cred->euid, tcred->uid) &&
  1175. !uid_eq(cred->uid, tcred->suid) && !uid_eq(cred->uid, tcred->uid) &&
  1176. !capable(CAP_SYS_NICE)) {
  1177. rcu_read_unlock();
  1178. err = -EPERM;
  1179. goto out;
  1180. }
  1181. rcu_read_unlock();
  1182. err = security_task_movememory(task);
  1183. if (err)
  1184. goto out;
  1185. task_nodes = cpuset_mems_allowed(task);
  1186. mm = get_task_mm(task);
  1187. put_task_struct(task);
  1188. if (!mm)
  1189. return -EINVAL;
  1190. if (nodes)
  1191. err = do_pages_move(mm, task_nodes, nr_pages, pages,
  1192. nodes, status, flags);
  1193. else
  1194. err = do_pages_stat(mm, nr_pages, pages, status);
  1195. mmput(mm);
  1196. return err;
  1197. out:
  1198. put_task_struct(task);
  1199. return err;
  1200. }
  1201. /*
  1202. * Call migration functions in the vma_ops that may prepare
  1203. * memory in a vm for migration. migration functions may perform
  1204. * the migration for vmas that do not have an underlying page struct.
  1205. */
  1206. int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
  1207. const nodemask_t *from, unsigned long flags)
  1208. {
  1209. struct vm_area_struct *vma;
  1210. int err = 0;
  1211. for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
  1212. if (vma->vm_ops && vma->vm_ops->migrate) {
  1213. err = vma->vm_ops->migrate(vma, to, from, flags);
  1214. if (err)
  1215. break;
  1216. }
  1217. }
  1218. return err;
  1219. }
  1220. #endif