migrate.c 27 KB

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