migrate.c 24 KB

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