migrate.c 32 KB

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