migrate.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  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. __set_page_dirty_nobuffers(newpage);
  381. }
  382. mlock_migrate_page(newpage, page);
  383. ksm_migrate_page(newpage, page);
  384. ClearPageSwapCache(page);
  385. ClearPagePrivate(page);
  386. set_page_private(page, 0);
  387. /*
  388. * If any waiters have accumulated on the new page then
  389. * wake them up.
  390. */
  391. if (PageWriteback(newpage))
  392. end_page_writeback(newpage);
  393. }
  394. /************************************************************
  395. * Migration functions
  396. ***********************************************************/
  397. /* Always fail migration. Used for mappings that are not movable */
  398. int fail_migrate_page(struct address_space *mapping,
  399. struct page *newpage, struct page *page)
  400. {
  401. return -EIO;
  402. }
  403. EXPORT_SYMBOL(fail_migrate_page);
  404. /*
  405. * Common logic to directly migrate a single page suitable for
  406. * pages that do not use PagePrivate/PagePrivate2.
  407. *
  408. * Pages are locked upon entry and exit.
  409. */
  410. int migrate_page(struct address_space *mapping,
  411. struct page *newpage, struct page *page,
  412. enum migrate_mode mode)
  413. {
  414. int rc;
  415. BUG_ON(PageWriteback(page)); /* Writeback must be complete */
  416. rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode);
  417. if (rc)
  418. return rc;
  419. migrate_page_copy(newpage, page);
  420. return 0;
  421. }
  422. EXPORT_SYMBOL(migrate_page);
  423. #ifdef CONFIG_BLOCK
  424. /*
  425. * Migration function for pages with buffers. This function can only be used
  426. * if the underlying filesystem guarantees that no other references to "page"
  427. * exist.
  428. */
  429. int buffer_migrate_page(struct address_space *mapping,
  430. struct page *newpage, struct page *page, enum migrate_mode mode)
  431. {
  432. struct buffer_head *bh, *head;
  433. int rc;
  434. if (!page_has_buffers(page))
  435. return migrate_page(mapping, newpage, page, mode);
  436. head = page_buffers(page);
  437. rc = migrate_page_move_mapping(mapping, newpage, page, head, mode);
  438. if (rc)
  439. return rc;
  440. /*
  441. * In the async case, migrate_page_move_mapping locked the buffers
  442. * with an IRQ-safe spinlock held. In the sync case, the buffers
  443. * need to be locked now
  444. */
  445. if (mode != MIGRATE_ASYNC)
  446. BUG_ON(!buffer_migrate_lock_buffers(head, mode));
  447. ClearPagePrivate(page);
  448. set_page_private(newpage, page_private(page));
  449. set_page_private(page, 0);
  450. put_page(page);
  451. get_page(newpage);
  452. bh = head;
  453. do {
  454. set_bh_page(bh, newpage, bh_offset(bh));
  455. bh = bh->b_this_page;
  456. } while (bh != head);
  457. SetPagePrivate(newpage);
  458. migrate_page_copy(newpage, page);
  459. bh = head;
  460. do {
  461. unlock_buffer(bh);
  462. put_bh(bh);
  463. bh = bh->b_this_page;
  464. } while (bh != head);
  465. return 0;
  466. }
  467. EXPORT_SYMBOL(buffer_migrate_page);
  468. #endif
  469. /*
  470. * Writeback a page to clean the dirty state
  471. */
  472. static int writeout(struct address_space *mapping, struct page *page)
  473. {
  474. struct writeback_control wbc = {
  475. .sync_mode = WB_SYNC_NONE,
  476. .nr_to_write = 1,
  477. .range_start = 0,
  478. .range_end = LLONG_MAX,
  479. .for_reclaim = 1
  480. };
  481. int rc;
  482. if (!mapping->a_ops->writepage)
  483. /* No write method for the address space */
  484. return -EINVAL;
  485. if (!clear_page_dirty_for_io(page))
  486. /* Someone else already triggered a write */
  487. return -EAGAIN;
  488. /*
  489. * A dirty page may imply that the underlying filesystem has
  490. * the page on some queue. So the page must be clean for
  491. * migration. Writeout may mean we loose the lock and the
  492. * page state is no longer what we checked for earlier.
  493. * At this point we know that the migration attempt cannot
  494. * be successful.
  495. */
  496. remove_migration_ptes(page, page);
  497. rc = mapping->a_ops->writepage(page, &wbc);
  498. if (rc != AOP_WRITEPAGE_ACTIVATE)
  499. /* unlocked. Relock */
  500. lock_page(page);
  501. return (rc < 0) ? -EIO : -EAGAIN;
  502. }
  503. /*
  504. * Default handling if a filesystem does not provide a migration function.
  505. */
  506. static int fallback_migrate_page(struct address_space *mapping,
  507. struct page *newpage, struct page *page, enum migrate_mode mode)
  508. {
  509. if (PageDirty(page)) {
  510. /* Only writeback pages in full synchronous migration */
  511. if (mode != MIGRATE_SYNC)
  512. return -EBUSY;
  513. return writeout(mapping, page);
  514. }
  515. /*
  516. * Buffers may be managed in a filesystem specific way.
  517. * We must have no buffers or drop them.
  518. */
  519. if (page_has_private(page) &&
  520. !try_to_release_page(page, GFP_KERNEL))
  521. return -EAGAIN;
  522. return migrate_page(mapping, newpage, page, mode);
  523. }
  524. /*
  525. * Move a page to a newly allocated page
  526. * The page is locked and all ptes have been successfully removed.
  527. *
  528. * The new page will have replaced the old page if this function
  529. * is successful.
  530. *
  531. * Return value:
  532. * < 0 - error code
  533. * == 0 - success
  534. */
  535. static int move_to_new_page(struct page *newpage, struct page *page,
  536. int remap_swapcache, enum migrate_mode mode)
  537. {
  538. struct address_space *mapping;
  539. int rc;
  540. /*
  541. * Block others from accessing the page when we get around to
  542. * establishing additional references. We are the only one
  543. * holding a reference to the new page at this point.
  544. */
  545. if (!trylock_page(newpage))
  546. BUG();
  547. /* Prepare mapping for the new page.*/
  548. newpage->index = page->index;
  549. newpage->mapping = page->mapping;
  550. if (PageSwapBacked(page))
  551. SetPageSwapBacked(newpage);
  552. mapping = page_mapping(page);
  553. if (!mapping)
  554. rc = migrate_page(mapping, newpage, page, mode);
  555. else if (mapping->a_ops->migratepage)
  556. /*
  557. * Most pages have a mapping and most filesystems provide a
  558. * migratepage callback. Anonymous pages are part of swap
  559. * space which also has its own migratepage callback. This
  560. * is the most common path for page migration.
  561. */
  562. rc = mapping->a_ops->migratepage(mapping,
  563. newpage, page, mode);
  564. else
  565. rc = fallback_migrate_page(mapping, newpage, page, mode);
  566. if (rc) {
  567. newpage->mapping = NULL;
  568. } else {
  569. if (remap_swapcache)
  570. remove_migration_ptes(page, newpage);
  571. page->mapping = NULL;
  572. }
  573. unlock_page(newpage);
  574. return rc;
  575. }
  576. static int __unmap_and_move(struct page *page, struct page *newpage,
  577. int force, bool offlining, enum migrate_mode mode)
  578. {
  579. int rc = -EAGAIN;
  580. int remap_swapcache = 1;
  581. int charge = 0;
  582. struct mem_cgroup *mem;
  583. struct anon_vma *anon_vma = NULL;
  584. if (!trylock_page(page)) {
  585. if (!force || mode == MIGRATE_ASYNC)
  586. goto out;
  587. /*
  588. * It's not safe for direct compaction to call lock_page.
  589. * For example, during page readahead pages are added locked
  590. * to the LRU. Later, when the IO completes the pages are
  591. * marked uptodate and unlocked. However, the queueing
  592. * could be merging multiple pages for one bio (e.g.
  593. * mpage_readpages). If an allocation happens for the
  594. * second or third page, the process can end up locking
  595. * the same page twice and deadlocking. Rather than
  596. * trying to be clever about what pages can be locked,
  597. * avoid the use of lock_page for direct compaction
  598. * altogether.
  599. */
  600. if (current->flags & PF_MEMALLOC)
  601. goto out;
  602. lock_page(page);
  603. }
  604. /*
  605. * Only memory hotplug's offline_pages() caller has locked out KSM,
  606. * and can safely migrate a KSM page. The other cases have skipped
  607. * PageKsm along with PageReserved - but it is only now when we have
  608. * the page lock that we can be certain it will not go KSM beneath us
  609. * (KSM will not upgrade a page from PageAnon to PageKsm when it sees
  610. * its pagecount raised, but only here do we take the page lock which
  611. * serializes that).
  612. */
  613. if (PageKsm(page) && !offlining) {
  614. rc = -EBUSY;
  615. goto unlock;
  616. }
  617. /* charge against new page */
  618. charge = mem_cgroup_prepare_migration(page, newpage, &mem, GFP_KERNEL);
  619. if (charge == -ENOMEM) {
  620. rc = -ENOMEM;
  621. goto unlock;
  622. }
  623. BUG_ON(charge);
  624. if (PageWriteback(page)) {
  625. /*
  626. * Only in the case of a full syncronous migration is it
  627. * necessary to wait for PageWriteback. In the async case,
  628. * the retry loop is too short and in the sync-light case,
  629. * the overhead of stalling is too much
  630. */
  631. if (mode != MIGRATE_SYNC) {
  632. rc = -EBUSY;
  633. goto uncharge;
  634. }
  635. if (!force)
  636. goto uncharge;
  637. wait_on_page_writeback(page);
  638. }
  639. /*
  640. * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
  641. * we cannot notice that anon_vma is freed while we migrates a page.
  642. * This get_anon_vma() delays freeing anon_vma pointer until the end
  643. * of migration. File cache pages are no problem because of page_lock()
  644. * File Caches may use write_page() or lock_page() in migration, then,
  645. * just care Anon page here.
  646. */
  647. if (PageAnon(page)) {
  648. /*
  649. * Only page_lock_anon_vma() understands the subtleties of
  650. * getting a hold on an anon_vma from outside one of its mms.
  651. */
  652. anon_vma = page_get_anon_vma(page);
  653. if (anon_vma) {
  654. /*
  655. * Anon page
  656. */
  657. } else if (PageSwapCache(page)) {
  658. /*
  659. * We cannot be sure that the anon_vma of an unmapped
  660. * swapcache page is safe to use because we don't
  661. * know in advance if the VMA that this page belonged
  662. * to still exists. If the VMA and others sharing the
  663. * data have been freed, then the anon_vma could
  664. * already be invalid.
  665. *
  666. * To avoid this possibility, swapcache pages get
  667. * migrated but are not remapped when migration
  668. * completes
  669. */
  670. remap_swapcache = 0;
  671. } else {
  672. goto uncharge;
  673. }
  674. }
  675. /*
  676. * Corner case handling:
  677. * 1. When a new swap-cache page is read into, it is added to the LRU
  678. * and treated as swapcache but it has no rmap yet.
  679. * Calling try_to_unmap() against a page->mapping==NULL page will
  680. * trigger a BUG. So handle it here.
  681. * 2. An orphaned page (see truncate_complete_page) might have
  682. * fs-private metadata. The page can be picked up due to memory
  683. * offlining. Everywhere else except page reclaim, the page is
  684. * invisible to the vm, so the page can not be migrated. So try to
  685. * free the metadata, so the page can be freed.
  686. */
  687. if (!page->mapping) {
  688. VM_BUG_ON(PageAnon(page));
  689. if (page_has_private(page)) {
  690. try_to_free_buffers(page);
  691. goto uncharge;
  692. }
  693. goto skip_unmap;
  694. }
  695. /* Establish migration ptes or remove ptes */
  696. try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
  697. skip_unmap:
  698. if (!page_mapped(page))
  699. rc = move_to_new_page(newpage, page, remap_swapcache, mode);
  700. if (rc && remap_swapcache)
  701. remove_migration_ptes(page, page);
  702. /* Drop an anon_vma reference if we took one */
  703. if (anon_vma)
  704. put_anon_vma(anon_vma);
  705. uncharge:
  706. if (!charge)
  707. mem_cgroup_end_migration(mem, page, newpage, rc == 0);
  708. unlock:
  709. unlock_page(page);
  710. out:
  711. return rc;
  712. }
  713. /*
  714. * Obtain the lock on page, remove all ptes and migrate the page
  715. * to the newly allocated page in newpage.
  716. */
  717. static int unmap_and_move(new_page_t get_new_page, unsigned long private,
  718. struct page *page, int force, bool offlining,
  719. enum migrate_mode mode)
  720. {
  721. int rc = 0;
  722. int *result = NULL;
  723. struct page *newpage = get_new_page(page, private, &result);
  724. if (!newpage)
  725. return -ENOMEM;
  726. if (page_count(page) == 1) {
  727. /* page was freed from under us. So we are done. */
  728. goto out;
  729. }
  730. if (unlikely(PageTransHuge(page)))
  731. if (unlikely(split_huge_page(page)))
  732. goto out;
  733. rc = __unmap_and_move(page, newpage, force, offlining, mode);
  734. out:
  735. if (rc != -EAGAIN) {
  736. /*
  737. * A page that has been migrated has all references
  738. * removed and will be freed. A page that has not been
  739. * migrated will have kepts its references and be
  740. * restored.
  741. */
  742. list_del(&page->lru);
  743. dec_zone_page_state(page, NR_ISOLATED_ANON +
  744. page_is_file_cache(page));
  745. putback_lru_page(page);
  746. }
  747. /*
  748. * Move the new page to the LRU. If migration was not successful
  749. * then this will free the page.
  750. */
  751. putback_lru_page(newpage);
  752. if (result) {
  753. if (rc)
  754. *result = rc;
  755. else
  756. *result = page_to_nid(newpage);
  757. }
  758. return rc;
  759. }
  760. /*
  761. * Counterpart of unmap_and_move_page() for hugepage migration.
  762. *
  763. * This function doesn't wait the completion of hugepage I/O
  764. * because there is no race between I/O and migration for hugepage.
  765. * Note that currently hugepage I/O occurs only in direct I/O
  766. * where no lock is held and PG_writeback is irrelevant,
  767. * and writeback status of all subpages are counted in the reference
  768. * count of the head page (i.e. if all subpages of a 2MB hugepage are
  769. * under direct I/O, the reference of the head page is 512 and a bit more.)
  770. * This means that when we try to migrate hugepage whose subpages are
  771. * doing direct I/O, some references remain after try_to_unmap() and
  772. * hugepage migration fails without data corruption.
  773. *
  774. * There is also no race when direct I/O is issued on the page under migration,
  775. * because then pte is replaced with migration swap entry and direct I/O code
  776. * will wait in the page fault for migration to complete.
  777. */
  778. static int unmap_and_move_huge_page(new_page_t get_new_page,
  779. unsigned long private, struct page *hpage,
  780. int force, bool offlining,
  781. enum migrate_mode mode)
  782. {
  783. int rc = 0;
  784. int *result = NULL;
  785. struct page *new_hpage = get_new_page(hpage, private, &result);
  786. struct anon_vma *anon_vma = NULL;
  787. if (!new_hpage)
  788. return -ENOMEM;
  789. rc = -EAGAIN;
  790. if (!trylock_page(hpage)) {
  791. if (!force || mode != MIGRATE_SYNC)
  792. goto out;
  793. lock_page(hpage);
  794. }
  795. if (PageAnon(hpage))
  796. anon_vma = page_get_anon_vma(hpage);
  797. try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
  798. if (!page_mapped(hpage))
  799. rc = move_to_new_page(new_hpage, hpage, 1, mode);
  800. if (rc)
  801. remove_migration_ptes(hpage, hpage);
  802. if (anon_vma)
  803. put_anon_vma(anon_vma);
  804. unlock_page(hpage);
  805. out:
  806. if (rc != -EAGAIN) {
  807. list_del(&hpage->lru);
  808. put_page(hpage);
  809. }
  810. put_page(new_hpage);
  811. if (result) {
  812. if (rc)
  813. *result = rc;
  814. else
  815. *result = page_to_nid(new_hpage);
  816. }
  817. return rc;
  818. }
  819. /*
  820. * migrate_pages
  821. *
  822. * The function takes one list of pages to migrate and a function
  823. * that determines from the page to be migrated and the private data
  824. * the target of the move and allocates the page.
  825. *
  826. * The function returns after 10 attempts or if no pages
  827. * are movable anymore because to has become empty
  828. * or no retryable pages exist anymore.
  829. * Caller should call putback_lru_pages to return pages to the LRU
  830. * or free list only if ret != 0.
  831. *
  832. * Return: Number of pages not migrated or error code.
  833. */
  834. int migrate_pages(struct list_head *from,
  835. new_page_t get_new_page, unsigned long private, bool offlining,
  836. enum migrate_mode mode)
  837. {
  838. int retry = 1;
  839. int nr_failed = 0;
  840. int pass = 0;
  841. struct page *page;
  842. struct page *page2;
  843. int swapwrite = current->flags & PF_SWAPWRITE;
  844. int rc;
  845. if (!swapwrite)
  846. current->flags |= PF_SWAPWRITE;
  847. for(pass = 0; pass < 10 && retry; pass++) {
  848. retry = 0;
  849. list_for_each_entry_safe(page, page2, from, lru) {
  850. cond_resched();
  851. rc = unmap_and_move(get_new_page, private,
  852. page, pass > 2, offlining,
  853. mode);
  854. switch(rc) {
  855. case -ENOMEM:
  856. goto out;
  857. case -EAGAIN:
  858. retry++;
  859. break;
  860. case 0:
  861. break;
  862. default:
  863. /* Permanent failure */
  864. nr_failed++;
  865. break;
  866. }
  867. }
  868. }
  869. rc = 0;
  870. out:
  871. if (!swapwrite)
  872. current->flags &= ~PF_SWAPWRITE;
  873. if (rc)
  874. return rc;
  875. return nr_failed + retry;
  876. }
  877. int migrate_huge_pages(struct list_head *from,
  878. new_page_t get_new_page, unsigned long private, bool offlining,
  879. enum migrate_mode mode)
  880. {
  881. int retry = 1;
  882. int nr_failed = 0;
  883. int pass = 0;
  884. struct page *page;
  885. struct page *page2;
  886. int rc;
  887. for (pass = 0; pass < 10 && retry; pass++) {
  888. retry = 0;
  889. list_for_each_entry_safe(page, page2, from, lru) {
  890. cond_resched();
  891. rc = unmap_and_move_huge_page(get_new_page,
  892. private, page, pass > 2, offlining,
  893. mode);
  894. switch(rc) {
  895. case -ENOMEM:
  896. goto out;
  897. case -EAGAIN:
  898. retry++;
  899. break;
  900. case 0:
  901. break;
  902. default:
  903. /* Permanent failure */
  904. nr_failed++;
  905. break;
  906. }
  907. }
  908. }
  909. rc = 0;
  910. out:
  911. if (rc)
  912. return rc;
  913. return nr_failed + retry;
  914. }
  915. #ifdef CONFIG_NUMA
  916. /*
  917. * Move a list of individual pages
  918. */
  919. struct page_to_node {
  920. unsigned long addr;
  921. struct page *page;
  922. int node;
  923. int status;
  924. };
  925. static struct page *new_page_node(struct page *p, unsigned long private,
  926. int **result)
  927. {
  928. struct page_to_node *pm = (struct page_to_node *)private;
  929. while (pm->node != MAX_NUMNODES && pm->page != p)
  930. pm++;
  931. if (pm->node == MAX_NUMNODES)
  932. return NULL;
  933. *result = &pm->status;
  934. return alloc_pages_exact_node(pm->node,
  935. GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
  936. }
  937. /*
  938. * Move a set of pages as indicated in the pm array. The addr
  939. * field must be set to the virtual address of the page to be moved
  940. * and the node number must contain a valid target node.
  941. * The pm array ends with node = MAX_NUMNODES.
  942. */
  943. static int do_move_page_to_node_array(struct mm_struct *mm,
  944. struct page_to_node *pm,
  945. int migrate_all)
  946. {
  947. int err;
  948. struct page_to_node *pp;
  949. LIST_HEAD(pagelist);
  950. down_read(&mm->mmap_sem);
  951. /*
  952. * Build a list of pages to migrate
  953. */
  954. for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
  955. struct vm_area_struct *vma;
  956. struct page *page;
  957. err = -EFAULT;
  958. vma = find_vma(mm, pp->addr);
  959. if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
  960. goto set_status;
  961. page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT);
  962. err = PTR_ERR(page);
  963. if (IS_ERR(page))
  964. goto set_status;
  965. err = -ENOENT;
  966. if (!page)
  967. goto set_status;
  968. /* Use PageReserved to check for zero page */
  969. if (PageReserved(page) || PageKsm(page))
  970. goto put_and_set;
  971. pp->page = page;
  972. err = page_to_nid(page);
  973. if (err == pp->node)
  974. /*
  975. * Node already in the right place
  976. */
  977. goto put_and_set;
  978. err = -EACCES;
  979. if (page_mapcount(page) > 1 &&
  980. !migrate_all)
  981. goto put_and_set;
  982. err = isolate_lru_page(page);
  983. if (!err) {
  984. list_add_tail(&page->lru, &pagelist);
  985. inc_zone_page_state(page, NR_ISOLATED_ANON +
  986. page_is_file_cache(page));
  987. }
  988. put_and_set:
  989. /*
  990. * Either remove the duplicate refcount from
  991. * isolate_lru_page() or drop the page ref if it was
  992. * not isolated.
  993. */
  994. put_page(page);
  995. set_status:
  996. pp->status = err;
  997. }
  998. err = 0;
  999. if (!list_empty(&pagelist)) {
  1000. err = migrate_pages(&pagelist, new_page_node,
  1001. (unsigned long)pm, 0, MIGRATE_SYNC);
  1002. if (err)
  1003. putback_lru_pages(&pagelist);
  1004. }
  1005. up_read(&mm->mmap_sem);
  1006. return err;
  1007. }
  1008. /*
  1009. * Migrate an array of page address onto an array of nodes and fill
  1010. * the corresponding array of status.
  1011. */
  1012. static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
  1013. unsigned long nr_pages,
  1014. const void __user * __user *pages,
  1015. const int __user *nodes,
  1016. int __user *status, int flags)
  1017. {
  1018. struct page_to_node *pm;
  1019. unsigned long chunk_nr_pages;
  1020. unsigned long chunk_start;
  1021. int err;
  1022. err = -ENOMEM;
  1023. pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
  1024. if (!pm)
  1025. goto out;
  1026. migrate_prep();
  1027. /*
  1028. * Store a chunk of page_to_node array in a page,
  1029. * but keep the last one as a marker
  1030. */
  1031. chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
  1032. for (chunk_start = 0;
  1033. chunk_start < nr_pages;
  1034. chunk_start += chunk_nr_pages) {
  1035. int j;
  1036. if (chunk_start + chunk_nr_pages > nr_pages)
  1037. chunk_nr_pages = nr_pages - chunk_start;
  1038. /* fill the chunk pm with addrs and nodes from user-space */
  1039. for (j = 0; j < chunk_nr_pages; j++) {
  1040. const void __user *p;
  1041. int node;
  1042. err = -EFAULT;
  1043. if (get_user(p, pages + j + chunk_start))
  1044. goto out_pm;
  1045. pm[j].addr = (unsigned long) p;
  1046. if (get_user(node, nodes + j + chunk_start))
  1047. goto out_pm;
  1048. err = -ENODEV;
  1049. if (node < 0 || node >= MAX_NUMNODES)
  1050. goto out_pm;
  1051. if (!node_state(node, N_HIGH_MEMORY))
  1052. goto out_pm;
  1053. err = -EACCES;
  1054. if (!node_isset(node, task_nodes))
  1055. goto out_pm;
  1056. pm[j].node = node;
  1057. }
  1058. /* End marker for this chunk */
  1059. pm[chunk_nr_pages].node = MAX_NUMNODES;
  1060. /* Migrate this chunk */
  1061. err = do_move_page_to_node_array(mm, pm,
  1062. flags & MPOL_MF_MOVE_ALL);
  1063. if (err < 0)
  1064. goto out_pm;
  1065. /* Return status information */
  1066. for (j = 0; j < chunk_nr_pages; j++)
  1067. if (put_user(pm[j].status, status + j + chunk_start)) {
  1068. err = -EFAULT;
  1069. goto out_pm;
  1070. }
  1071. }
  1072. err = 0;
  1073. out_pm:
  1074. free_page((unsigned long)pm);
  1075. out:
  1076. return err;
  1077. }
  1078. /*
  1079. * Determine the nodes of an array of pages and store it in an array of status.
  1080. */
  1081. static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
  1082. const void __user **pages, int *status)
  1083. {
  1084. unsigned long i;
  1085. down_read(&mm->mmap_sem);
  1086. for (i = 0; i < nr_pages; i++) {
  1087. unsigned long addr = (unsigned long)(*pages);
  1088. struct vm_area_struct *vma;
  1089. struct page *page;
  1090. int err = -EFAULT;
  1091. vma = find_vma(mm, addr);
  1092. if (!vma || addr < vma->vm_start)
  1093. goto set_status;
  1094. page = follow_page(vma, addr, 0);
  1095. err = PTR_ERR(page);
  1096. if (IS_ERR(page))
  1097. goto set_status;
  1098. err = -ENOENT;
  1099. /* Use PageReserved to check for zero page */
  1100. if (!page || PageReserved(page) || PageKsm(page))
  1101. goto set_status;
  1102. err = page_to_nid(page);
  1103. set_status:
  1104. *status = err;
  1105. pages++;
  1106. status++;
  1107. }
  1108. up_read(&mm->mmap_sem);
  1109. }
  1110. /*
  1111. * Determine the nodes of a user array of pages and store it in
  1112. * a user array of status.
  1113. */
  1114. static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
  1115. const void __user * __user *pages,
  1116. int __user *status)
  1117. {
  1118. #define DO_PAGES_STAT_CHUNK_NR 16
  1119. const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
  1120. int chunk_status[DO_PAGES_STAT_CHUNK_NR];
  1121. while (nr_pages) {
  1122. unsigned long chunk_nr;
  1123. chunk_nr = nr_pages;
  1124. if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
  1125. chunk_nr = DO_PAGES_STAT_CHUNK_NR;
  1126. if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
  1127. break;
  1128. do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
  1129. if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
  1130. break;
  1131. pages += chunk_nr;
  1132. status += chunk_nr;
  1133. nr_pages -= chunk_nr;
  1134. }
  1135. return nr_pages ? -EFAULT : 0;
  1136. }
  1137. /*
  1138. * Move a list of pages in the address space of the currently executing
  1139. * process.
  1140. */
  1141. SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
  1142. const void __user * __user *, pages,
  1143. const int __user *, nodes,
  1144. int __user *, status, int, flags)
  1145. {
  1146. const struct cred *cred = current_cred(), *tcred;
  1147. struct task_struct *task;
  1148. struct mm_struct *mm;
  1149. int err;
  1150. nodemask_t task_nodes;
  1151. /* Check flags */
  1152. if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
  1153. return -EINVAL;
  1154. if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
  1155. return -EPERM;
  1156. /* Find the mm_struct */
  1157. rcu_read_lock();
  1158. task = pid ? find_task_by_vpid(pid) : current;
  1159. if (!task) {
  1160. rcu_read_unlock();
  1161. return -ESRCH;
  1162. }
  1163. get_task_struct(task);
  1164. /*
  1165. * Check if this process has the right to modify the specified
  1166. * process. The right exists if the process has administrative
  1167. * capabilities, superuser privileges or the same
  1168. * userid as the target process.
  1169. */
  1170. tcred = __task_cred(task);
  1171. if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
  1172. cred->uid != tcred->suid && cred->uid != tcred->uid &&
  1173. !capable(CAP_SYS_NICE)) {
  1174. rcu_read_unlock();
  1175. err = -EPERM;
  1176. goto out;
  1177. }
  1178. rcu_read_unlock();
  1179. err = security_task_movememory(task);
  1180. if (err)
  1181. goto out;
  1182. task_nodes = cpuset_mems_allowed(task);
  1183. mm = get_task_mm(task);
  1184. put_task_struct(task);
  1185. if (mm) {
  1186. if (nodes)
  1187. err = do_pages_move(mm, task_nodes, nr_pages, pages,
  1188. nodes, status, flags);
  1189. else
  1190. err = do_pages_stat(mm, nr_pages, pages, status);
  1191. } else
  1192. err = -EINVAL;
  1193. mmput(mm);
  1194. return err;
  1195. out:
  1196. put_task_struct(task);
  1197. return err;
  1198. }
  1199. /*
  1200. * Call migration functions in the vma_ops that may prepare
  1201. * memory in a vm for migration. migration functions may perform
  1202. * the migration for vmas that do not have an underlying page struct.
  1203. */
  1204. int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
  1205. const nodemask_t *from, unsigned long flags)
  1206. {
  1207. struct vm_area_struct *vma;
  1208. int err = 0;
  1209. for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
  1210. if (vma->vm_ops && vma->vm_ops->migrate) {
  1211. err = vma->vm_ops->migrate(vma, to, from, flags);
  1212. if (err)
  1213. break;
  1214. }
  1215. }
  1216. return err;
  1217. }
  1218. #endif