migrate.c 27 KB

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