migrate.c 31 KB

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