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/export.h>
  16. #include <linux/swap.h>
  17. #include <linux/swapops.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/buffer_head.h>
  20. #include <linux/mm_inline.h>
  21. #include <linux/nsproxy.h>
  22. #include <linux/pagevec.h>
  23. #include <linux/ksm.h>
  24. #include <linux/rmap.h>
  25. #include <linux/topology.h>
  26. #include <linux/cpu.h>
  27. #include <linux/cpuset.h>
  28. #include <linux/writeback.h>
  29. #include <linux/mempolicy.h>
  30. #include <linux/vmalloc.h>
  31. #include <linux/security.h>
  32. #include <linux/memcontrol.h>
  33. #include <linux/syscalls.h>
  34. #include <linux/hugetlb.h>
  35. #include <linux/gfp.h>
  36. #include <asm/tlbflush.h>
  37. #include "internal.h"
  38. #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. void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
  161. unsigned long address)
  162. {
  163. pte_t *ptep, pte;
  164. spinlock_t *ptl;
  165. swp_entry_t entry;
  166. struct page *page;
  167. ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
  168. pte = *ptep;
  169. if (!is_swap_pte(pte))
  170. goto out;
  171. entry = pte_to_swp_entry(pte);
  172. if (!is_migration_entry(entry))
  173. goto out;
  174. page = migration_entry_to_page(entry);
  175. /*
  176. * Once radix-tree replacement of page migration started, page_count
  177. * *must* be zero. And, we don't want to call wait_on_page_locked()
  178. * against a page without get_page().
  179. * So, we use get_page_unless_zero(), here. Even failed, page fault
  180. * will occur again.
  181. */
  182. if (!get_page_unless_zero(page))
  183. goto out;
  184. pte_unmap_unlock(ptep, ptl);
  185. wait_on_page_locked(page);
  186. put_page(page);
  187. return;
  188. out:
  189. pte_unmap_unlock(ptep, ptl);
  190. }
  191. /*
  192. * Replace the page in the mapping.
  193. *
  194. * The number of remaining references must be:
  195. * 1 for anonymous pages without a mapping
  196. * 2 for pages with a mapping
  197. * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
  198. */
  199. static int migrate_page_move_mapping(struct address_space *mapping,
  200. struct page *newpage, struct page *page)
  201. {
  202. int expected_count;
  203. void **pslot;
  204. if (!mapping) {
  205. /* Anonymous page without mapping */
  206. if (page_count(page) != 1)
  207. return -EAGAIN;
  208. return 0;
  209. }
  210. spin_lock_irq(&mapping->tree_lock);
  211. pslot = radix_tree_lookup_slot(&mapping->page_tree,
  212. page_index(page));
  213. expected_count = 2 + page_has_private(page);
  214. if (page_count(page) != expected_count ||
  215. radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
  216. spin_unlock_irq(&mapping->tree_lock);
  217. return -EAGAIN;
  218. }
  219. if (!page_freeze_refs(page, expected_count)) {
  220. spin_unlock_irq(&mapping->tree_lock);
  221. return -EAGAIN;
  222. }
  223. /*
  224. * Now we know that no one else is looking at the page.
  225. */
  226. get_page(newpage); /* add cache reference */
  227. if (PageSwapCache(page)) {
  228. SetPageSwapCache(newpage);
  229. set_page_private(newpage, page_private(page));
  230. }
  231. radix_tree_replace_slot(pslot, newpage);
  232. /*
  233. * Drop cache reference from old page by unfreezing
  234. * to one less reference.
  235. * We know this isn't the last reference.
  236. */
  237. page_unfreeze_refs(page, expected_count - 1);
  238. /*
  239. * If moved to a different zone then also account
  240. * the page for that zone. Other VM counters will be
  241. * taken care of when we establish references to the
  242. * new page and drop references to the old page.
  243. *
  244. * Note that anonymous pages are accounted for
  245. * via NR_FILE_PAGES and NR_ANON_PAGES if they
  246. * are mapped to swap space.
  247. */
  248. __dec_zone_page_state(page, NR_FILE_PAGES);
  249. __inc_zone_page_state(newpage, NR_FILE_PAGES);
  250. if (!PageSwapCache(page) && PageSwapBacked(page)) {
  251. __dec_zone_page_state(page, NR_SHMEM);
  252. __inc_zone_page_state(newpage, NR_SHMEM);
  253. }
  254. spin_unlock_irq(&mapping->tree_lock);
  255. return 0;
  256. }
  257. /*
  258. * The expected number of remaining references is the same as that
  259. * of migrate_page_move_mapping().
  260. */
  261. int migrate_huge_page_move_mapping(struct address_space *mapping,
  262. struct page *newpage, struct page *page)
  263. {
  264. int expected_count;
  265. void **pslot;
  266. if (!mapping) {
  267. if (page_count(page) != 1)
  268. return -EAGAIN;
  269. return 0;
  270. }
  271. spin_lock_irq(&mapping->tree_lock);
  272. pslot = radix_tree_lookup_slot(&mapping->page_tree,
  273. page_index(page));
  274. expected_count = 2 + page_has_private(page);
  275. if (page_count(page) != expected_count ||
  276. radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
  277. spin_unlock_irq(&mapping->tree_lock);
  278. return -EAGAIN;
  279. }
  280. if (!page_freeze_refs(page, expected_count)) {
  281. spin_unlock_irq(&mapping->tree_lock);
  282. return -EAGAIN;
  283. }
  284. get_page(newpage);
  285. radix_tree_replace_slot(pslot, newpage);
  286. page_unfreeze_refs(page, expected_count - 1);
  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. * Whereas 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, bool sync)
  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 {
  494. /*
  495. * Do not writeback pages if !sync and migratepage is
  496. * not pointing to migrate_page() which is nonblocking
  497. * (swapcache/tmpfs uses migratepage = migrate_page).
  498. */
  499. if (PageDirty(page) && !sync &&
  500. mapping->a_ops->migratepage != migrate_page)
  501. rc = -EBUSY;
  502. else if (mapping->a_ops->migratepage)
  503. /*
  504. * Most pages have a mapping and most filesystems
  505. * should provide a migration function. Anonymous
  506. * pages are part of swap space which also has its
  507. * own migration function. This is the most common
  508. * path for page migration.
  509. */
  510. rc = mapping->a_ops->migratepage(mapping,
  511. newpage, page);
  512. else
  513. rc = fallback_migrate_page(mapping, newpage, page);
  514. }
  515. if (rc) {
  516. newpage->mapping = NULL;
  517. } else {
  518. if (remap_swapcache)
  519. remove_migration_ptes(page, newpage);
  520. }
  521. unlock_page(newpage);
  522. return rc;
  523. }
  524. static int __unmap_and_move(struct page *page, struct page *newpage,
  525. int force, bool offlining, bool sync)
  526. {
  527. int rc = -EAGAIN;
  528. int remap_swapcache = 1;
  529. int charge = 0;
  530. struct mem_cgroup *mem;
  531. struct anon_vma *anon_vma = NULL;
  532. if (!trylock_page(page)) {
  533. if (!force || !sync)
  534. goto out;
  535. /*
  536. * It's not safe for direct compaction to call lock_page.
  537. * For example, during page readahead pages are added locked
  538. * to the LRU. Later, when the IO completes the pages are
  539. * marked uptodate and unlocked. However, the queueing
  540. * could be merging multiple pages for one bio (e.g.
  541. * mpage_readpages). If an allocation happens for the
  542. * second or third page, the process can end up locking
  543. * the same page twice and deadlocking. Rather than
  544. * trying to be clever about what pages can be locked,
  545. * avoid the use of lock_page for direct compaction
  546. * altogether.
  547. */
  548. if (current->flags & PF_MEMALLOC)
  549. goto out;
  550. lock_page(page);
  551. }
  552. /*
  553. * Only memory hotplug's offline_pages() caller has locked out KSM,
  554. * and can safely migrate a KSM page. The other cases have skipped
  555. * PageKsm along with PageReserved - but it is only now when we have
  556. * the page lock that we can be certain it will not go KSM beneath us
  557. * (KSM will not upgrade a page from PageAnon to PageKsm when it sees
  558. * its pagecount raised, but only here do we take the page lock which
  559. * serializes that).
  560. */
  561. if (PageKsm(page) && !offlining) {
  562. rc = -EBUSY;
  563. goto unlock;
  564. }
  565. /* charge against new page */
  566. charge = mem_cgroup_prepare_migration(page, newpage, &mem, GFP_KERNEL);
  567. if (charge == -ENOMEM) {
  568. rc = -ENOMEM;
  569. goto unlock;
  570. }
  571. BUG_ON(charge);
  572. if (PageWriteback(page)) {
  573. /*
  574. * For !sync, there is no point retrying as the retry loop
  575. * is expected to be too short for PageWriteback to be cleared
  576. */
  577. if (!sync) {
  578. rc = -EBUSY;
  579. goto uncharge;
  580. }
  581. if (!force)
  582. goto uncharge;
  583. wait_on_page_writeback(page);
  584. }
  585. /*
  586. * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
  587. * we cannot notice that anon_vma is freed while we migrates a page.
  588. * This get_anon_vma() delays freeing anon_vma pointer until the end
  589. * of migration. File cache pages are no problem because of page_lock()
  590. * File Caches may use write_page() or lock_page() in migration, then,
  591. * just care Anon page here.
  592. */
  593. if (PageAnon(page)) {
  594. /*
  595. * Only page_lock_anon_vma() understands the subtleties of
  596. * getting a hold on an anon_vma from outside one of its mms.
  597. */
  598. anon_vma = page_get_anon_vma(page);
  599. if (anon_vma) {
  600. /*
  601. * Anon page
  602. */
  603. } else if (PageSwapCache(page)) {
  604. /*
  605. * We cannot be sure that the anon_vma of an unmapped
  606. * swapcache page is safe to use because we don't
  607. * know in advance if the VMA that this page belonged
  608. * to still exists. If the VMA and others sharing the
  609. * data have been freed, then the anon_vma could
  610. * already be invalid.
  611. *
  612. * To avoid this possibility, swapcache pages get
  613. * migrated but are not remapped when migration
  614. * completes
  615. */
  616. remap_swapcache = 0;
  617. } else {
  618. goto uncharge;
  619. }
  620. }
  621. /*
  622. * Corner case handling:
  623. * 1. When a new swap-cache page is read into, it is added to the LRU
  624. * and treated as swapcache but it has no rmap yet.
  625. * Calling try_to_unmap() against a page->mapping==NULL page will
  626. * trigger a BUG. So handle it here.
  627. * 2. An orphaned page (see truncate_complete_page) might have
  628. * fs-private metadata. The page can be picked up due to memory
  629. * offlining. Everywhere else except page reclaim, the page is
  630. * invisible to the vm, so the page can not be migrated. So try to
  631. * free the metadata, so the page can be freed.
  632. */
  633. if (!page->mapping) {
  634. VM_BUG_ON(PageAnon(page));
  635. if (page_has_private(page)) {
  636. try_to_free_buffers(page);
  637. goto uncharge;
  638. }
  639. goto skip_unmap;
  640. }
  641. /* Establish migration ptes or remove ptes */
  642. try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
  643. skip_unmap:
  644. if (!page_mapped(page))
  645. rc = move_to_new_page(newpage, page, remap_swapcache, sync);
  646. if (rc && remap_swapcache)
  647. remove_migration_ptes(page, page);
  648. /* Drop an anon_vma reference if we took one */
  649. if (anon_vma)
  650. put_anon_vma(anon_vma);
  651. uncharge:
  652. if (!charge)
  653. mem_cgroup_end_migration(mem, page, newpage, rc == 0);
  654. unlock:
  655. unlock_page(page);
  656. out:
  657. return rc;
  658. }
  659. /*
  660. * Obtain the lock on page, remove all ptes and migrate the page
  661. * to the newly allocated page in newpage.
  662. */
  663. static int unmap_and_move(new_page_t get_new_page, unsigned long private,
  664. struct page *page, int force, bool offlining, bool sync)
  665. {
  666. int rc = 0;
  667. int *result = NULL;
  668. struct page *newpage = get_new_page(page, private, &result);
  669. if (!newpage)
  670. return -ENOMEM;
  671. if (page_count(page) == 1) {
  672. /* page was freed from under us. So we are done. */
  673. goto out;
  674. }
  675. if (unlikely(PageTransHuge(page)))
  676. if (unlikely(split_huge_page(page)))
  677. goto out;
  678. rc = __unmap_and_move(page, newpage, force, offlining, sync);
  679. out:
  680. if (rc != -EAGAIN) {
  681. /*
  682. * A page that has been migrated has all references
  683. * removed and will be freed. A page that has not been
  684. * migrated will have kepts its references and be
  685. * restored.
  686. */
  687. list_del(&page->lru);
  688. dec_zone_page_state(page, NR_ISOLATED_ANON +
  689. page_is_file_cache(page));
  690. putback_lru_page(page);
  691. }
  692. /*
  693. * Move the new page to the LRU. If migration was not successful
  694. * then this will free the page.
  695. */
  696. putback_lru_page(newpage);
  697. if (result) {
  698. if (rc)
  699. *result = rc;
  700. else
  701. *result = page_to_nid(newpage);
  702. }
  703. return rc;
  704. }
  705. /*
  706. * Counterpart of unmap_and_move_page() for hugepage migration.
  707. *
  708. * This function doesn't wait the completion of hugepage I/O
  709. * because there is no race between I/O and migration for hugepage.
  710. * Note that currently hugepage I/O occurs only in direct I/O
  711. * where no lock is held and PG_writeback is irrelevant,
  712. * and writeback status of all subpages are counted in the reference
  713. * count of the head page (i.e. if all subpages of a 2MB hugepage are
  714. * under direct I/O, the reference of the head page is 512 and a bit more.)
  715. * This means that when we try to migrate hugepage whose subpages are
  716. * doing direct I/O, some references remain after try_to_unmap() and
  717. * hugepage migration fails without data corruption.
  718. *
  719. * There is also no race when direct I/O is issued on the page under migration,
  720. * because then pte is replaced with migration swap entry and direct I/O code
  721. * will wait in the page fault for migration to complete.
  722. */
  723. static int unmap_and_move_huge_page(new_page_t get_new_page,
  724. unsigned long private, struct page *hpage,
  725. int force, bool offlining, bool sync)
  726. {
  727. int rc = 0;
  728. int *result = NULL;
  729. struct page *new_hpage = get_new_page(hpage, private, &result);
  730. struct anon_vma *anon_vma = NULL;
  731. if (!new_hpage)
  732. return -ENOMEM;
  733. rc = -EAGAIN;
  734. if (!trylock_page(hpage)) {
  735. if (!force || !sync)
  736. goto out;
  737. lock_page(hpage);
  738. }
  739. if (PageAnon(hpage))
  740. anon_vma = page_get_anon_vma(hpage);
  741. try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
  742. if (!page_mapped(hpage))
  743. rc = move_to_new_page(new_hpage, hpage, 1, sync);
  744. if (rc)
  745. remove_migration_ptes(hpage, hpage);
  746. if (anon_vma)
  747. put_anon_vma(anon_vma);
  748. unlock_page(hpage);
  749. out:
  750. if (rc != -EAGAIN) {
  751. list_del(&hpage->lru);
  752. put_page(hpage);
  753. }
  754. put_page(new_hpage);
  755. if (result) {
  756. if (rc)
  757. *result = rc;
  758. else
  759. *result = page_to_nid(new_hpage);
  760. }
  761. return rc;
  762. }
  763. /*
  764. * migrate_pages
  765. *
  766. * The function takes one list of pages to migrate and a function
  767. * that determines from the page to be migrated and the private data
  768. * the target of the move and allocates the page.
  769. *
  770. * The function returns after 10 attempts or if no pages
  771. * are movable anymore because to has become empty
  772. * or no retryable pages exist anymore.
  773. * Caller should call putback_lru_pages to return pages to the LRU
  774. * or free list only if ret != 0.
  775. *
  776. * Return: Number of pages not migrated or error code.
  777. */
  778. int migrate_pages(struct list_head *from,
  779. new_page_t get_new_page, unsigned long private, bool offlining,
  780. bool sync)
  781. {
  782. int retry = 1;
  783. int nr_failed = 0;
  784. int pass = 0;
  785. struct page *page;
  786. struct page *page2;
  787. int swapwrite = current->flags & PF_SWAPWRITE;
  788. int rc;
  789. if (!swapwrite)
  790. current->flags |= PF_SWAPWRITE;
  791. for(pass = 0; pass < 10 && retry; pass++) {
  792. retry = 0;
  793. list_for_each_entry_safe(page, page2, from, lru) {
  794. cond_resched();
  795. rc = unmap_and_move(get_new_page, private,
  796. page, pass > 2, offlining,
  797. sync);
  798. switch(rc) {
  799. case -ENOMEM:
  800. goto out;
  801. case -EAGAIN:
  802. retry++;
  803. break;
  804. case 0:
  805. break;
  806. default:
  807. /* Permanent failure */
  808. nr_failed++;
  809. break;
  810. }
  811. }
  812. }
  813. rc = 0;
  814. out:
  815. if (!swapwrite)
  816. current->flags &= ~PF_SWAPWRITE;
  817. if (rc)
  818. return rc;
  819. return nr_failed + retry;
  820. }
  821. int migrate_huge_pages(struct list_head *from,
  822. new_page_t get_new_page, unsigned long private, bool offlining,
  823. bool sync)
  824. {
  825. int retry = 1;
  826. int nr_failed = 0;
  827. int pass = 0;
  828. struct page *page;
  829. struct page *page2;
  830. int rc;
  831. for (pass = 0; pass < 10 && retry; pass++) {
  832. retry = 0;
  833. list_for_each_entry_safe(page, page2, from, lru) {
  834. cond_resched();
  835. rc = unmap_and_move_huge_page(get_new_page,
  836. private, page, pass > 2, offlining,
  837. sync);
  838. switch(rc) {
  839. case -ENOMEM:
  840. goto out;
  841. case -EAGAIN:
  842. retry++;
  843. break;
  844. case 0:
  845. break;
  846. default:
  847. /* Permanent failure */
  848. nr_failed++;
  849. break;
  850. }
  851. }
  852. }
  853. rc = 0;
  854. out:
  855. if (rc)
  856. return rc;
  857. return nr_failed + retry;
  858. }
  859. #ifdef CONFIG_NUMA
  860. /*
  861. * Move a list of individual pages
  862. */
  863. struct page_to_node {
  864. unsigned long addr;
  865. struct page *page;
  866. int node;
  867. int status;
  868. };
  869. static struct page *new_page_node(struct page *p, unsigned long private,
  870. int **result)
  871. {
  872. struct page_to_node *pm = (struct page_to_node *)private;
  873. while (pm->node != MAX_NUMNODES && pm->page != p)
  874. pm++;
  875. if (pm->node == MAX_NUMNODES)
  876. return NULL;
  877. *result = &pm->status;
  878. return alloc_pages_exact_node(pm->node,
  879. GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
  880. }
  881. /*
  882. * Move a set of pages as indicated in the pm array. The addr
  883. * field must be set to the virtual address of the page to be moved
  884. * and the node number must contain a valid target node.
  885. * The pm array ends with node = MAX_NUMNODES.
  886. */
  887. static int do_move_page_to_node_array(struct mm_struct *mm,
  888. struct page_to_node *pm,
  889. int migrate_all)
  890. {
  891. int err;
  892. struct page_to_node *pp;
  893. LIST_HEAD(pagelist);
  894. down_read(&mm->mmap_sem);
  895. /*
  896. * Build a list of pages to migrate
  897. */
  898. for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
  899. struct vm_area_struct *vma;
  900. struct page *page;
  901. err = -EFAULT;
  902. vma = find_vma(mm, pp->addr);
  903. if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
  904. goto set_status;
  905. page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT);
  906. err = PTR_ERR(page);
  907. if (IS_ERR(page))
  908. goto set_status;
  909. err = -ENOENT;
  910. if (!page)
  911. goto set_status;
  912. /* Use PageReserved to check for zero page */
  913. if (PageReserved(page) || PageKsm(page))
  914. goto put_and_set;
  915. pp->page = page;
  916. err = page_to_nid(page);
  917. if (err == pp->node)
  918. /*
  919. * Node already in the right place
  920. */
  921. goto put_and_set;
  922. err = -EACCES;
  923. if (page_mapcount(page) > 1 &&
  924. !migrate_all)
  925. goto put_and_set;
  926. err = isolate_lru_page(page);
  927. if (!err) {
  928. list_add_tail(&page->lru, &pagelist);
  929. inc_zone_page_state(page, NR_ISOLATED_ANON +
  930. page_is_file_cache(page));
  931. }
  932. put_and_set:
  933. /*
  934. * Either remove the duplicate refcount from
  935. * isolate_lru_page() or drop the page ref if it was
  936. * not isolated.
  937. */
  938. put_page(page);
  939. set_status:
  940. pp->status = err;
  941. }
  942. err = 0;
  943. if (!list_empty(&pagelist)) {
  944. err = migrate_pages(&pagelist, new_page_node,
  945. (unsigned long)pm, 0, true);
  946. if (err)
  947. putback_lru_pages(&pagelist);
  948. }
  949. up_read(&mm->mmap_sem);
  950. return err;
  951. }
  952. /*
  953. * Migrate an array of page address onto an array of nodes and fill
  954. * the corresponding array of status.
  955. */
  956. static int do_pages_move(struct mm_struct *mm, struct task_struct *task,
  957. unsigned long nr_pages,
  958. const void __user * __user *pages,
  959. const int __user *nodes,
  960. int __user *status, int flags)
  961. {
  962. struct page_to_node *pm;
  963. nodemask_t task_nodes;
  964. unsigned long chunk_nr_pages;
  965. unsigned long chunk_start;
  966. int err;
  967. task_nodes = cpuset_mems_allowed(task);
  968. err = -ENOMEM;
  969. pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
  970. if (!pm)
  971. goto out;
  972. migrate_prep();
  973. /*
  974. * Store a chunk of page_to_node array in a page,
  975. * but keep the last one as a marker
  976. */
  977. chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
  978. for (chunk_start = 0;
  979. chunk_start < nr_pages;
  980. chunk_start += chunk_nr_pages) {
  981. int j;
  982. if (chunk_start + chunk_nr_pages > nr_pages)
  983. chunk_nr_pages = nr_pages - chunk_start;
  984. /* fill the chunk pm with addrs and nodes from user-space */
  985. for (j = 0; j < chunk_nr_pages; j++) {
  986. const void __user *p;
  987. int node;
  988. err = -EFAULT;
  989. if (get_user(p, pages + j + chunk_start))
  990. goto out_pm;
  991. pm[j].addr = (unsigned long) p;
  992. if (get_user(node, nodes + j + chunk_start))
  993. goto out_pm;
  994. err = -ENODEV;
  995. if (node < 0 || node >= MAX_NUMNODES)
  996. goto out_pm;
  997. if (!node_state(node, N_HIGH_MEMORY))
  998. goto out_pm;
  999. err = -EACCES;
  1000. if (!node_isset(node, task_nodes))
  1001. goto out_pm;
  1002. pm[j].node = node;
  1003. }
  1004. /* End marker for this chunk */
  1005. pm[chunk_nr_pages].node = MAX_NUMNODES;
  1006. /* Migrate this chunk */
  1007. err = do_move_page_to_node_array(mm, pm,
  1008. flags & MPOL_MF_MOVE_ALL);
  1009. if (err < 0)
  1010. goto out_pm;
  1011. /* Return status information */
  1012. for (j = 0; j < chunk_nr_pages; j++)
  1013. if (put_user(pm[j].status, status + j + chunk_start)) {
  1014. err = -EFAULT;
  1015. goto out_pm;
  1016. }
  1017. }
  1018. err = 0;
  1019. out_pm:
  1020. free_page((unsigned long)pm);
  1021. out:
  1022. return err;
  1023. }
  1024. /*
  1025. * Determine the nodes of an array of pages and store it in an array of status.
  1026. */
  1027. static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
  1028. const void __user **pages, int *status)
  1029. {
  1030. unsigned long i;
  1031. down_read(&mm->mmap_sem);
  1032. for (i = 0; i < nr_pages; i++) {
  1033. unsigned long addr = (unsigned long)(*pages);
  1034. struct vm_area_struct *vma;
  1035. struct page *page;
  1036. int err = -EFAULT;
  1037. vma = find_vma(mm, addr);
  1038. if (!vma || addr < vma->vm_start)
  1039. goto set_status;
  1040. page = follow_page(vma, addr, 0);
  1041. err = PTR_ERR(page);
  1042. if (IS_ERR(page))
  1043. goto set_status;
  1044. err = -ENOENT;
  1045. /* Use PageReserved to check for zero page */
  1046. if (!page || PageReserved(page) || PageKsm(page))
  1047. goto set_status;
  1048. err = page_to_nid(page);
  1049. set_status:
  1050. *status = err;
  1051. pages++;
  1052. status++;
  1053. }
  1054. up_read(&mm->mmap_sem);
  1055. }
  1056. /*
  1057. * Determine the nodes of a user array of pages and store it in
  1058. * a user array of status.
  1059. */
  1060. static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
  1061. const void __user * __user *pages,
  1062. int __user *status)
  1063. {
  1064. #define DO_PAGES_STAT_CHUNK_NR 16
  1065. const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
  1066. int chunk_status[DO_PAGES_STAT_CHUNK_NR];
  1067. while (nr_pages) {
  1068. unsigned long chunk_nr;
  1069. chunk_nr = nr_pages;
  1070. if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
  1071. chunk_nr = DO_PAGES_STAT_CHUNK_NR;
  1072. if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
  1073. break;
  1074. do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
  1075. if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
  1076. break;
  1077. pages += chunk_nr;
  1078. status += chunk_nr;
  1079. nr_pages -= chunk_nr;
  1080. }
  1081. return nr_pages ? -EFAULT : 0;
  1082. }
  1083. /*
  1084. * Move a list of pages in the address space of the currently executing
  1085. * process.
  1086. */
  1087. SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
  1088. const void __user * __user *, pages,
  1089. const int __user *, nodes,
  1090. int __user *, status, int, flags)
  1091. {
  1092. const struct cred *cred = current_cred(), *tcred;
  1093. struct task_struct *task;
  1094. struct mm_struct *mm;
  1095. int err;
  1096. /* Check flags */
  1097. if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
  1098. return -EINVAL;
  1099. if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
  1100. return -EPERM;
  1101. /* Find the mm_struct */
  1102. rcu_read_lock();
  1103. task = pid ? find_task_by_vpid(pid) : current;
  1104. if (!task) {
  1105. rcu_read_unlock();
  1106. return -ESRCH;
  1107. }
  1108. mm = get_task_mm(task);
  1109. rcu_read_unlock();
  1110. if (!mm)
  1111. return -EINVAL;
  1112. /*
  1113. * Check if this process has the right to modify the specified
  1114. * process. The right exists if the process has administrative
  1115. * capabilities, superuser privileges or the same
  1116. * userid as the target process.
  1117. */
  1118. rcu_read_lock();
  1119. tcred = __task_cred(task);
  1120. if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
  1121. cred->uid != tcred->suid && cred->uid != tcred->uid &&
  1122. !capable(CAP_SYS_NICE)) {
  1123. rcu_read_unlock();
  1124. err = -EPERM;
  1125. goto out;
  1126. }
  1127. rcu_read_unlock();
  1128. err = security_task_movememory(task);
  1129. if (err)
  1130. goto out;
  1131. if (nodes) {
  1132. err = do_pages_move(mm, task, nr_pages, pages, nodes, status,
  1133. flags);
  1134. } else {
  1135. err = do_pages_stat(mm, nr_pages, pages, status);
  1136. }
  1137. out:
  1138. mmput(mm);
  1139. return err;
  1140. }
  1141. /*
  1142. * Call migration functions in the vma_ops that may prepare
  1143. * memory in a vm for migration. migration functions may perform
  1144. * the migration for vmas that do not have an underlying page struct.
  1145. */
  1146. int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
  1147. const nodemask_t *from, unsigned long flags)
  1148. {
  1149. struct vm_area_struct *vma;
  1150. int err = 0;
  1151. for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
  1152. if (vma->vm_ops && vma->vm_ops->migrate) {
  1153. err = vma->vm_ops->migrate(vma, to, from, flags);
  1154. if (err)
  1155. break;
  1156. }
  1157. }
  1158. return err;
  1159. }
  1160. #endif