migrate.c 31 KB

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