migrate.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  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. {
  415. struct address_space *mapping;
  416. int rc;
  417. /*
  418. * Block others from accessing the page when we get around to
  419. * establishing additional references. We are the only one
  420. * holding a reference to the new page at this point.
  421. */
  422. if (!trylock_page(newpage))
  423. BUG();
  424. /* Prepare mapping for the new page.*/
  425. newpage->index = page->index;
  426. newpage->mapping = page->mapping;
  427. if (PageSwapBacked(page))
  428. SetPageSwapBacked(newpage);
  429. mapping = page_mapping(page);
  430. if (!mapping)
  431. rc = migrate_page(mapping, newpage, page);
  432. else if (mapping->a_ops->migratepage)
  433. /*
  434. * Most pages have a mapping and most filesystems
  435. * should provide a migration function. Anonymous
  436. * pages are part of swap space which also has its
  437. * own migration function. This is the most common
  438. * path for page migration.
  439. */
  440. rc = mapping->a_ops->migratepage(mapping,
  441. newpage, page);
  442. else
  443. rc = fallback_migrate_page(mapping, newpage, page);
  444. if (!rc)
  445. remove_migration_ptes(page, newpage);
  446. else
  447. newpage->mapping = NULL;
  448. unlock_page(newpage);
  449. return rc;
  450. }
  451. /*
  452. * Obtain the lock on page, remove all ptes and migrate the page
  453. * to the newly allocated page in newpage.
  454. */
  455. static int unmap_and_move(new_page_t get_new_page, unsigned long private,
  456. struct page *page, int force, int offlining)
  457. {
  458. int rc = 0;
  459. int *result = NULL;
  460. struct page *newpage = get_new_page(page, private, &result);
  461. int rcu_locked = 0;
  462. int charge = 0;
  463. struct mem_cgroup *mem = NULL;
  464. if (!newpage)
  465. return -ENOMEM;
  466. if (page_count(page) == 1) {
  467. /* page was freed from under us. So we are done. */
  468. goto move_newpage;
  469. }
  470. /* prepare cgroup just returns 0 or -ENOMEM */
  471. rc = -EAGAIN;
  472. if (!trylock_page(page)) {
  473. if (!force)
  474. goto move_newpage;
  475. lock_page(page);
  476. }
  477. /*
  478. * Only memory hotplug's offline_pages() caller has locked out KSM,
  479. * and can safely migrate a KSM page. The other cases have skipped
  480. * PageKsm along with PageReserved - but it is only now when we have
  481. * the page lock that we can be certain it will not go KSM beneath us
  482. * (KSM will not upgrade a page from PageAnon to PageKsm when it sees
  483. * its pagecount raised, but only here do we take the page lock which
  484. * serializes that).
  485. */
  486. if (PageKsm(page) && !offlining) {
  487. rc = -EBUSY;
  488. goto unlock;
  489. }
  490. /* charge against new page */
  491. charge = mem_cgroup_prepare_migration(page, &mem);
  492. if (charge == -ENOMEM) {
  493. rc = -ENOMEM;
  494. goto unlock;
  495. }
  496. BUG_ON(charge);
  497. if (PageWriteback(page)) {
  498. if (!force)
  499. goto uncharge;
  500. wait_on_page_writeback(page);
  501. }
  502. /*
  503. * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
  504. * we cannot notice that anon_vma is freed while we migrates a page.
  505. * This rcu_read_lock() delays freeing anon_vma pointer until the end
  506. * of migration. File cache pages are no problem because of page_lock()
  507. * File Caches may use write_page() or lock_page() in migration, then,
  508. * just care Anon page here.
  509. */
  510. if (PageAnon(page)) {
  511. rcu_read_lock();
  512. rcu_locked = 1;
  513. }
  514. /*
  515. * Corner case handling:
  516. * 1. When a new swap-cache page is read into, it is added to the LRU
  517. * and treated as swapcache but it has no rmap yet.
  518. * Calling try_to_unmap() against a page->mapping==NULL page will
  519. * trigger a BUG. So handle it here.
  520. * 2. An orphaned page (see truncate_complete_page) might have
  521. * fs-private metadata. The page can be picked up due to memory
  522. * offlining. Everywhere else except page reclaim, the page is
  523. * invisible to the vm, so the page can not be migrated. So try to
  524. * free the metadata, so the page can be freed.
  525. */
  526. if (!page->mapping) {
  527. if (!PageAnon(page) && page_has_private(page)) {
  528. /*
  529. * Go direct to try_to_free_buffers() here because
  530. * a) that's what try_to_release_page() would do anyway
  531. * b) we may be under rcu_read_lock() here, so we can't
  532. * use GFP_KERNEL which is what try_to_release_page()
  533. * needs to be effective.
  534. */
  535. try_to_free_buffers(page);
  536. goto rcu_unlock;
  537. }
  538. goto skip_unmap;
  539. }
  540. /* Establish migration ptes or remove ptes */
  541. try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
  542. skip_unmap:
  543. if (!page_mapped(page))
  544. rc = move_to_new_page(newpage, page);
  545. if (rc)
  546. remove_migration_ptes(page, page);
  547. rcu_unlock:
  548. if (rcu_locked)
  549. rcu_read_unlock();
  550. uncharge:
  551. if (!charge)
  552. mem_cgroup_end_migration(mem, page, newpage);
  553. unlock:
  554. unlock_page(page);
  555. if (rc != -EAGAIN) {
  556. /*
  557. * A page that has been migrated has all references
  558. * removed and will be freed. A page that has not been
  559. * migrated will have kepts its references and be
  560. * restored.
  561. */
  562. list_del(&page->lru);
  563. dec_zone_page_state(page, NR_ISOLATED_ANON +
  564. page_is_file_cache(page));
  565. putback_lru_page(page);
  566. }
  567. move_newpage:
  568. /*
  569. * Move the new page to the LRU. If migration was not successful
  570. * then this will free the page.
  571. */
  572. putback_lru_page(newpage);
  573. if (result) {
  574. if (rc)
  575. *result = rc;
  576. else
  577. *result = page_to_nid(newpage);
  578. }
  579. return rc;
  580. }
  581. /*
  582. * migrate_pages
  583. *
  584. * The function takes one list of pages to migrate and a function
  585. * that determines from the page to be migrated and the private data
  586. * the target of the move and allocates the page.
  587. *
  588. * The function returns after 10 attempts or if no pages
  589. * are movable anymore because to has become empty
  590. * or no retryable pages exist anymore. All pages will be
  591. * returned to the LRU or freed.
  592. *
  593. * Return: Number of pages not migrated or error code.
  594. */
  595. int migrate_pages(struct list_head *from,
  596. new_page_t get_new_page, unsigned long private, int offlining)
  597. {
  598. int retry = 1;
  599. int nr_failed = 0;
  600. int pass = 0;
  601. struct page *page;
  602. struct page *page2;
  603. int swapwrite = current->flags & PF_SWAPWRITE;
  604. int rc;
  605. if (!swapwrite)
  606. current->flags |= PF_SWAPWRITE;
  607. for(pass = 0; pass < 10 && retry; pass++) {
  608. retry = 0;
  609. list_for_each_entry_safe(page, page2, from, lru) {
  610. cond_resched();
  611. rc = unmap_and_move(get_new_page, private,
  612. page, pass > 2, offlining);
  613. switch(rc) {
  614. case -ENOMEM:
  615. goto out;
  616. case -EAGAIN:
  617. retry++;
  618. break;
  619. case 0:
  620. break;
  621. default:
  622. /* Permanent failure */
  623. nr_failed++;
  624. break;
  625. }
  626. }
  627. }
  628. rc = 0;
  629. out:
  630. if (!swapwrite)
  631. current->flags &= ~PF_SWAPWRITE;
  632. putback_lru_pages(from);
  633. if (rc)
  634. return rc;
  635. return nr_failed + retry;
  636. }
  637. #ifdef CONFIG_NUMA
  638. /*
  639. * Move a list of individual pages
  640. */
  641. struct page_to_node {
  642. unsigned long addr;
  643. struct page *page;
  644. int node;
  645. int status;
  646. };
  647. static struct page *new_page_node(struct page *p, unsigned long private,
  648. int **result)
  649. {
  650. struct page_to_node *pm = (struct page_to_node *)private;
  651. while (pm->node != MAX_NUMNODES && pm->page != p)
  652. pm++;
  653. if (pm->node == MAX_NUMNODES)
  654. return NULL;
  655. *result = &pm->status;
  656. return alloc_pages_exact_node(pm->node,
  657. GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
  658. }
  659. /*
  660. * Move a set of pages as indicated in the pm array. The addr
  661. * field must be set to the virtual address of the page to be moved
  662. * and the node number must contain a valid target node.
  663. * The pm array ends with node = MAX_NUMNODES.
  664. */
  665. static int do_move_page_to_node_array(struct mm_struct *mm,
  666. struct page_to_node *pm,
  667. int migrate_all)
  668. {
  669. int err;
  670. struct page_to_node *pp;
  671. LIST_HEAD(pagelist);
  672. down_read(&mm->mmap_sem);
  673. /*
  674. * Build a list of pages to migrate
  675. */
  676. for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
  677. struct vm_area_struct *vma;
  678. struct page *page;
  679. err = -EFAULT;
  680. vma = find_vma(mm, pp->addr);
  681. if (!vma || !vma_migratable(vma))
  682. goto set_status;
  683. page = follow_page(vma, pp->addr, FOLL_GET);
  684. err = PTR_ERR(page);
  685. if (IS_ERR(page))
  686. goto set_status;
  687. err = -ENOENT;
  688. if (!page)
  689. goto set_status;
  690. /* Use PageReserved to check for zero page */
  691. if (PageReserved(page) || PageKsm(page))
  692. goto put_and_set;
  693. pp->page = page;
  694. err = page_to_nid(page);
  695. if (err == pp->node)
  696. /*
  697. * Node already in the right place
  698. */
  699. goto put_and_set;
  700. err = -EACCES;
  701. if (page_mapcount(page) > 1 &&
  702. !migrate_all)
  703. goto put_and_set;
  704. err = isolate_lru_page(page);
  705. if (!err) {
  706. list_add_tail(&page->lru, &pagelist);
  707. inc_zone_page_state(page, NR_ISOLATED_ANON +
  708. page_is_file_cache(page));
  709. }
  710. put_and_set:
  711. /*
  712. * Either remove the duplicate refcount from
  713. * isolate_lru_page() or drop the page ref if it was
  714. * not isolated.
  715. */
  716. put_page(page);
  717. set_status:
  718. pp->status = err;
  719. }
  720. err = 0;
  721. if (!list_empty(&pagelist))
  722. err = migrate_pages(&pagelist, new_page_node,
  723. (unsigned long)pm, 0);
  724. up_read(&mm->mmap_sem);
  725. return err;
  726. }
  727. /*
  728. * Migrate an array of page address onto an array of nodes and fill
  729. * the corresponding array of status.
  730. */
  731. static int do_pages_move(struct mm_struct *mm, struct task_struct *task,
  732. unsigned long nr_pages,
  733. const void __user * __user *pages,
  734. const int __user *nodes,
  735. int __user *status, int flags)
  736. {
  737. struct page_to_node *pm;
  738. nodemask_t task_nodes;
  739. unsigned long chunk_nr_pages;
  740. unsigned long chunk_start;
  741. int err;
  742. task_nodes = cpuset_mems_allowed(task);
  743. err = -ENOMEM;
  744. pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
  745. if (!pm)
  746. goto out;
  747. migrate_prep();
  748. /*
  749. * Store a chunk of page_to_node array in a page,
  750. * but keep the last one as a marker
  751. */
  752. chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
  753. for (chunk_start = 0;
  754. chunk_start < nr_pages;
  755. chunk_start += chunk_nr_pages) {
  756. int j;
  757. if (chunk_start + chunk_nr_pages > nr_pages)
  758. chunk_nr_pages = nr_pages - chunk_start;
  759. /* fill the chunk pm with addrs and nodes from user-space */
  760. for (j = 0; j < chunk_nr_pages; j++) {
  761. const void __user *p;
  762. int node;
  763. err = -EFAULT;
  764. if (get_user(p, pages + j + chunk_start))
  765. goto out_pm;
  766. pm[j].addr = (unsigned long) p;
  767. if (get_user(node, nodes + j + chunk_start))
  768. goto out_pm;
  769. err = -ENODEV;
  770. if (node < 0 || node >= MAX_NUMNODES)
  771. goto out_pm;
  772. if (!node_state(node, N_HIGH_MEMORY))
  773. goto out_pm;
  774. err = -EACCES;
  775. if (!node_isset(node, task_nodes))
  776. goto out_pm;
  777. pm[j].node = node;
  778. }
  779. /* End marker for this chunk */
  780. pm[chunk_nr_pages].node = MAX_NUMNODES;
  781. /* Migrate this chunk */
  782. err = do_move_page_to_node_array(mm, pm,
  783. flags & MPOL_MF_MOVE_ALL);
  784. if (err < 0)
  785. goto out_pm;
  786. /* Return status information */
  787. for (j = 0; j < chunk_nr_pages; j++)
  788. if (put_user(pm[j].status, status + j + chunk_start)) {
  789. err = -EFAULT;
  790. goto out_pm;
  791. }
  792. }
  793. err = 0;
  794. out_pm:
  795. free_page((unsigned long)pm);
  796. out:
  797. return err;
  798. }
  799. /*
  800. * Determine the nodes of an array of pages and store it in an array of status.
  801. */
  802. static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
  803. const void __user **pages, int *status)
  804. {
  805. unsigned long i;
  806. down_read(&mm->mmap_sem);
  807. for (i = 0; i < nr_pages; i++) {
  808. unsigned long addr = (unsigned long)(*pages);
  809. struct vm_area_struct *vma;
  810. struct page *page;
  811. int err = -EFAULT;
  812. vma = find_vma(mm, addr);
  813. if (!vma)
  814. goto set_status;
  815. page = follow_page(vma, addr, 0);
  816. err = PTR_ERR(page);
  817. if (IS_ERR(page))
  818. goto set_status;
  819. err = -ENOENT;
  820. /* Use PageReserved to check for zero page */
  821. if (!page || PageReserved(page) || PageKsm(page))
  822. goto set_status;
  823. err = page_to_nid(page);
  824. set_status:
  825. *status = err;
  826. pages++;
  827. status++;
  828. }
  829. up_read(&mm->mmap_sem);
  830. }
  831. /*
  832. * Determine the nodes of a user array of pages and store it in
  833. * a user array of status.
  834. */
  835. static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
  836. const void __user * __user *pages,
  837. int __user *status)
  838. {
  839. #define DO_PAGES_STAT_CHUNK_NR 16
  840. const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
  841. int chunk_status[DO_PAGES_STAT_CHUNK_NR];
  842. while (nr_pages) {
  843. unsigned long chunk_nr;
  844. chunk_nr = nr_pages;
  845. if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
  846. chunk_nr = DO_PAGES_STAT_CHUNK_NR;
  847. if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
  848. break;
  849. do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
  850. if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
  851. break;
  852. pages += chunk_nr;
  853. status += chunk_nr;
  854. nr_pages -= chunk_nr;
  855. }
  856. return nr_pages ? -EFAULT : 0;
  857. }
  858. /*
  859. * Move a list of pages in the address space of the currently executing
  860. * process.
  861. */
  862. SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
  863. const void __user * __user *, pages,
  864. const int __user *, nodes,
  865. int __user *, status, int, flags)
  866. {
  867. const struct cred *cred = current_cred(), *tcred;
  868. struct task_struct *task;
  869. struct mm_struct *mm;
  870. int err;
  871. /* Check flags */
  872. if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
  873. return -EINVAL;
  874. if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
  875. return -EPERM;
  876. /* Find the mm_struct */
  877. read_lock(&tasklist_lock);
  878. task = pid ? find_task_by_vpid(pid) : current;
  879. if (!task) {
  880. read_unlock(&tasklist_lock);
  881. return -ESRCH;
  882. }
  883. mm = get_task_mm(task);
  884. read_unlock(&tasklist_lock);
  885. if (!mm)
  886. return -EINVAL;
  887. /*
  888. * Check if this process has the right to modify the specified
  889. * process. The right exists if the process has administrative
  890. * capabilities, superuser privileges or the same
  891. * userid as the target process.
  892. */
  893. rcu_read_lock();
  894. tcred = __task_cred(task);
  895. if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
  896. cred->uid != tcred->suid && cred->uid != tcred->uid &&
  897. !capable(CAP_SYS_NICE)) {
  898. rcu_read_unlock();
  899. err = -EPERM;
  900. goto out;
  901. }
  902. rcu_read_unlock();
  903. err = security_task_movememory(task);
  904. if (err)
  905. goto out;
  906. if (nodes) {
  907. err = do_pages_move(mm, task, nr_pages, pages, nodes, status,
  908. flags);
  909. } else {
  910. err = do_pages_stat(mm, nr_pages, pages, status);
  911. }
  912. out:
  913. mmput(mm);
  914. return err;
  915. }
  916. /*
  917. * Call migration functions in the vma_ops that may prepare
  918. * memory in a vm for migration. migration functions may perform
  919. * the migration for vmas that do not have an underlying page struct.
  920. */
  921. int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
  922. const nodemask_t *from, unsigned long flags)
  923. {
  924. struct vm_area_struct *vma;
  925. int err = 0;
  926. for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
  927. if (vma->vm_ops && vma->vm_ops->migrate) {
  928. err = vma->vm_ops->migrate(vma, to, from, flags);
  929. if (err)
  930. break;
  931. }
  932. }
  933. return err;
  934. }
  935. #endif