migrate.c 26 KB

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