migrate.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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/pagevec.h>
  22. #include <linux/rmap.h>
  23. #include <linux/topology.h>
  24. #include <linux/cpu.h>
  25. #include <linux/cpuset.h>
  26. #include "internal.h"
  27. /* The maximum number of pages to take off the LRU for migration */
  28. #define MIGRATE_CHUNK_SIZE 256
  29. #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
  30. /*
  31. * Isolate one page from the LRU lists. If successful put it onto
  32. * the indicated list with elevated page count.
  33. *
  34. * Result:
  35. * -EBUSY: page not on LRU list
  36. * 0: page removed from LRU list and added to the specified list.
  37. */
  38. int isolate_lru_page(struct page *page, struct list_head *pagelist)
  39. {
  40. int ret = -EBUSY;
  41. if (PageLRU(page)) {
  42. struct zone *zone = page_zone(page);
  43. spin_lock_irq(&zone->lru_lock);
  44. if (PageLRU(page)) {
  45. ret = 0;
  46. get_page(page);
  47. ClearPageLRU(page);
  48. if (PageActive(page))
  49. del_page_from_active_list(zone, page);
  50. else
  51. del_page_from_inactive_list(zone, page);
  52. list_add_tail(&page->lru, pagelist);
  53. }
  54. spin_unlock_irq(&zone->lru_lock);
  55. }
  56. return ret;
  57. }
  58. /*
  59. * migrate_prep() needs to be called after we have compiled the list of pages
  60. * to be migrated using isolate_lru_page() but before we begin a series of calls
  61. * to migrate_pages().
  62. */
  63. int migrate_prep(void)
  64. {
  65. /* Must have swap device for migration */
  66. if (nr_swap_pages <= 0)
  67. return -ENODEV;
  68. /*
  69. * Clear the LRU lists so pages can be isolated.
  70. * Note that pages may be moved off the LRU after we have
  71. * drained them. Those pages will fail to migrate like other
  72. * pages that may be busy.
  73. */
  74. lru_add_drain_all();
  75. return 0;
  76. }
  77. static inline void move_to_lru(struct page *page)
  78. {
  79. list_del(&page->lru);
  80. if (PageActive(page)) {
  81. /*
  82. * lru_cache_add_active checks that
  83. * the PG_active bit is off.
  84. */
  85. ClearPageActive(page);
  86. lru_cache_add_active(page);
  87. } else {
  88. lru_cache_add(page);
  89. }
  90. put_page(page);
  91. }
  92. /*
  93. * Add isolated pages on the list back to the LRU.
  94. *
  95. * returns the number of pages put back.
  96. */
  97. int putback_lru_pages(struct list_head *l)
  98. {
  99. struct page *page;
  100. struct page *page2;
  101. int count = 0;
  102. list_for_each_entry_safe(page, page2, l, lru) {
  103. move_to_lru(page);
  104. count++;
  105. }
  106. return count;
  107. }
  108. static inline int is_swap_pte(pte_t pte)
  109. {
  110. return !pte_none(pte) && !pte_present(pte) && !pte_file(pte);
  111. }
  112. /*
  113. * Restore a potential migration pte to a working pte entry
  114. */
  115. static void remove_migration_pte(struct vm_area_struct *vma, unsigned long addr,
  116. struct page *old, struct page *new)
  117. {
  118. struct mm_struct *mm = vma->vm_mm;
  119. swp_entry_t entry;
  120. pgd_t *pgd;
  121. pud_t *pud;
  122. pmd_t *pmd;
  123. pte_t *ptep, pte;
  124. spinlock_t *ptl;
  125. pgd = pgd_offset(mm, addr);
  126. if (!pgd_present(*pgd))
  127. return;
  128. pud = pud_offset(pgd, addr);
  129. if (!pud_present(*pud))
  130. return;
  131. pmd = pmd_offset(pud, addr);
  132. if (!pmd_present(*pmd))
  133. return;
  134. ptep = pte_offset_map(pmd, addr);
  135. if (!is_swap_pte(*ptep)) {
  136. pte_unmap(ptep);
  137. return;
  138. }
  139. ptl = pte_lockptr(mm, pmd);
  140. spin_lock(ptl);
  141. pte = *ptep;
  142. if (!is_swap_pte(pte))
  143. goto out;
  144. entry = pte_to_swp_entry(pte);
  145. if (!is_migration_entry(entry) || migration_entry_to_page(entry) != old)
  146. goto out;
  147. inc_mm_counter(mm, anon_rss);
  148. get_page(new);
  149. pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
  150. if (is_write_migration_entry(entry))
  151. pte = pte_mkwrite(pte);
  152. set_pte_at(mm, addr, ptep, pte);
  153. page_add_anon_rmap(new, vma, addr);
  154. out:
  155. pte_unmap_unlock(ptep, ptl);
  156. }
  157. /*
  158. * Get rid of all migration entries and replace them by
  159. * references to the indicated page.
  160. *
  161. * Must hold mmap_sem lock on at least one of the vmas containing
  162. * the page so that the anon_vma cannot vanish.
  163. */
  164. static void remove_migration_ptes(struct page *old, struct page *new)
  165. {
  166. struct anon_vma *anon_vma;
  167. struct vm_area_struct *vma;
  168. unsigned long mapping;
  169. mapping = (unsigned long)new->mapping;
  170. if (!mapping || (mapping & PAGE_MAPPING_ANON) == 0)
  171. return;
  172. /*
  173. * We hold the mmap_sem lock. So no need to call page_lock_anon_vma.
  174. */
  175. anon_vma = (struct anon_vma *) (mapping - PAGE_MAPPING_ANON);
  176. spin_lock(&anon_vma->lock);
  177. list_for_each_entry(vma, &anon_vma->head, anon_vma_node)
  178. remove_migration_pte(vma, page_address_in_vma(new, vma),
  179. old, new);
  180. spin_unlock(&anon_vma->lock);
  181. }
  182. /*
  183. * Something used the pte of a page under migration. We need to
  184. * get to the page and wait until migration is finished.
  185. * When we return from this function the fault will be retried.
  186. *
  187. * This function is called from do_swap_page().
  188. */
  189. void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
  190. unsigned long address)
  191. {
  192. pte_t *ptep, pte;
  193. spinlock_t *ptl;
  194. swp_entry_t entry;
  195. struct page *page;
  196. ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
  197. pte = *ptep;
  198. if (!is_swap_pte(pte))
  199. goto out;
  200. entry = pte_to_swp_entry(pte);
  201. if (!is_migration_entry(entry))
  202. goto out;
  203. page = migration_entry_to_page(entry);
  204. get_page(page);
  205. pte_unmap_unlock(ptep, ptl);
  206. wait_on_page_locked(page);
  207. put_page(page);
  208. return;
  209. out:
  210. pte_unmap_unlock(ptep, ptl);
  211. }
  212. /*
  213. * swapout a single page
  214. * page is locked upon entry, unlocked on exit
  215. */
  216. static int swap_page(struct page *page)
  217. {
  218. struct address_space *mapping = page_mapping(page);
  219. if (page_mapped(page) && mapping)
  220. if (try_to_unmap(page, 1) != SWAP_SUCCESS)
  221. goto unlock_retry;
  222. if (PageDirty(page)) {
  223. /* Page is dirty, try to write it out here */
  224. switch(pageout(page, mapping)) {
  225. case PAGE_KEEP:
  226. case PAGE_ACTIVATE:
  227. goto unlock_retry;
  228. case PAGE_SUCCESS:
  229. goto retry;
  230. case PAGE_CLEAN:
  231. ; /* try to free the page below */
  232. }
  233. }
  234. if (PagePrivate(page)) {
  235. if (!try_to_release_page(page, GFP_KERNEL) ||
  236. (!mapping && page_count(page) == 1))
  237. goto unlock_retry;
  238. }
  239. if (remove_mapping(mapping, page)) {
  240. /* Success */
  241. unlock_page(page);
  242. return 0;
  243. }
  244. unlock_retry:
  245. unlock_page(page);
  246. retry:
  247. return -EAGAIN;
  248. }
  249. /*
  250. * Replace the page in the mapping.
  251. *
  252. * The number of remaining references must be:
  253. * 1 for anonymous pages without a mapping
  254. * 2 for pages with a mapping
  255. * 3 for pages with a mapping and PagePrivate set.
  256. */
  257. static int migrate_page_move_mapping(struct address_space *mapping,
  258. struct page *newpage, struct page *page)
  259. {
  260. struct page **radix_pointer;
  261. write_lock_irq(&mapping->tree_lock);
  262. radix_pointer = (struct page **)radix_tree_lookup_slot(
  263. &mapping->page_tree,
  264. page_index(page));
  265. if (!page_mapping(page) ||
  266. page_count(page) != 2 + !!PagePrivate(page) ||
  267. *radix_pointer != page) {
  268. write_unlock_irq(&mapping->tree_lock);
  269. return -EAGAIN;
  270. }
  271. /*
  272. * Now we know that no one else is looking at the page.
  273. */
  274. get_page(newpage);
  275. if (PageSwapCache(page)) {
  276. SetPageSwapCache(newpage);
  277. set_page_private(newpage, page_private(page));
  278. }
  279. *radix_pointer = newpage;
  280. __put_page(page);
  281. write_unlock_irq(&mapping->tree_lock);
  282. return 0;
  283. }
  284. /*
  285. * Copy the page to its new location
  286. */
  287. static void migrate_page_copy(struct page *newpage, struct page *page)
  288. {
  289. copy_highpage(newpage, page);
  290. if (PageError(page))
  291. SetPageError(newpage);
  292. if (PageReferenced(page))
  293. SetPageReferenced(newpage);
  294. if (PageUptodate(page))
  295. SetPageUptodate(newpage);
  296. if (PageActive(page))
  297. SetPageActive(newpage);
  298. if (PageChecked(page))
  299. SetPageChecked(newpage);
  300. if (PageMappedToDisk(page))
  301. SetPageMappedToDisk(newpage);
  302. if (PageDirty(page)) {
  303. clear_page_dirty_for_io(page);
  304. set_page_dirty(newpage);
  305. }
  306. ClearPageSwapCache(page);
  307. ClearPageActive(page);
  308. ClearPagePrivate(page);
  309. set_page_private(page, 0);
  310. page->mapping = NULL;
  311. /*
  312. * If any waiters have accumulated on the new page then
  313. * wake them up.
  314. */
  315. if (PageWriteback(newpage))
  316. end_page_writeback(newpage);
  317. }
  318. /************************************************************
  319. * Migration functions
  320. ***********************************************************/
  321. /* Always fail migration. Used for mappings that are not movable */
  322. int fail_migrate_page(struct address_space *mapping,
  323. struct page *newpage, struct page *page)
  324. {
  325. return -EIO;
  326. }
  327. EXPORT_SYMBOL(fail_migrate_page);
  328. /*
  329. * Common logic to directly migrate a single page suitable for
  330. * pages that do not use PagePrivate.
  331. *
  332. * Pages are locked upon entry and exit.
  333. */
  334. int migrate_page(struct address_space *mapping,
  335. struct page *newpage, struct page *page)
  336. {
  337. int rc;
  338. BUG_ON(PageWriteback(page)); /* Writeback must be complete */
  339. rc = migrate_page_move_mapping(mapping, newpage, page);
  340. if (rc)
  341. return rc;
  342. migrate_page_copy(newpage, page);
  343. /*
  344. * Remove auxiliary swap entries and replace
  345. * them with real ptes.
  346. *
  347. * Note that a real pte entry will allow processes that are not
  348. * waiting on the page lock to use the new page via the page tables
  349. * before the new page is unlocked.
  350. */
  351. remove_from_swap(newpage);
  352. return 0;
  353. }
  354. EXPORT_SYMBOL(migrate_page);
  355. /*
  356. * Migration function for pages with buffers. This function can only be used
  357. * if the underlying filesystem guarantees that no other references to "page"
  358. * exist.
  359. */
  360. int buffer_migrate_page(struct address_space *mapping,
  361. struct page *newpage, struct page *page)
  362. {
  363. struct buffer_head *bh, *head;
  364. int rc;
  365. if (!page_has_buffers(page))
  366. return migrate_page(mapping, newpage, page);
  367. head = page_buffers(page);
  368. rc = migrate_page_move_mapping(mapping, newpage, page);
  369. if (rc)
  370. return rc;
  371. bh = head;
  372. do {
  373. get_bh(bh);
  374. lock_buffer(bh);
  375. bh = bh->b_this_page;
  376. } while (bh != head);
  377. ClearPagePrivate(page);
  378. set_page_private(newpage, page_private(page));
  379. set_page_private(page, 0);
  380. put_page(page);
  381. get_page(newpage);
  382. bh = head;
  383. do {
  384. set_bh_page(bh, newpage, bh_offset(bh));
  385. bh = bh->b_this_page;
  386. } while (bh != head);
  387. SetPagePrivate(newpage);
  388. migrate_page_copy(newpage, page);
  389. bh = head;
  390. do {
  391. unlock_buffer(bh);
  392. put_bh(bh);
  393. bh = bh->b_this_page;
  394. } while (bh != head);
  395. return 0;
  396. }
  397. EXPORT_SYMBOL(buffer_migrate_page);
  398. static int fallback_migrate_page(struct address_space *mapping,
  399. struct page *newpage, struct page *page)
  400. {
  401. /*
  402. * Default handling if a filesystem does not provide
  403. * a migration function. We can only migrate clean
  404. * pages so try to write out any dirty pages first.
  405. */
  406. if (PageDirty(page)) {
  407. switch (pageout(page, mapping)) {
  408. case PAGE_KEEP:
  409. case PAGE_ACTIVATE:
  410. return -EAGAIN;
  411. case PAGE_SUCCESS:
  412. /* Relock since we lost the lock */
  413. lock_page(page);
  414. /* Must retry since page state may have changed */
  415. return -EAGAIN;
  416. case PAGE_CLEAN:
  417. ; /* try to migrate the page below */
  418. }
  419. }
  420. /*
  421. * Buffers may be managed in a filesystem specific way.
  422. * We must have no buffers or drop them.
  423. */
  424. if (page_has_buffers(page) &&
  425. !try_to_release_page(page, GFP_KERNEL))
  426. return -EAGAIN;
  427. return migrate_page(mapping, newpage, page);
  428. }
  429. /*
  430. * migrate_pages
  431. *
  432. * Two lists are passed to this function. The first list
  433. * contains the pages isolated from the LRU to be migrated.
  434. * The second list contains new pages that the pages isolated
  435. * can be moved to. If the second list is NULL then all
  436. * pages are swapped out.
  437. *
  438. * The function returns after 10 attempts or if no pages
  439. * are movable anymore because to has become empty
  440. * or no retryable pages exist anymore.
  441. *
  442. * Return: Number of pages not migrated when "to" ran empty.
  443. */
  444. int migrate_pages(struct list_head *from, struct list_head *to,
  445. struct list_head *moved, struct list_head *failed)
  446. {
  447. int retry;
  448. int nr_failed = 0;
  449. int pass = 0;
  450. struct page *page;
  451. struct page *page2;
  452. int swapwrite = current->flags & PF_SWAPWRITE;
  453. int rc;
  454. if (!swapwrite)
  455. current->flags |= PF_SWAPWRITE;
  456. redo:
  457. retry = 0;
  458. list_for_each_entry_safe(page, page2, from, lru) {
  459. struct page *newpage = NULL;
  460. struct address_space *mapping;
  461. cond_resched();
  462. rc = 0;
  463. if (page_count(page) == 1)
  464. /* page was freed from under us. So we are done. */
  465. goto next;
  466. if (to && list_empty(to))
  467. break;
  468. /*
  469. * Skip locked pages during the first two passes to give the
  470. * functions holding the lock time to release the page. Later we
  471. * use lock_page() to have a higher chance of acquiring the
  472. * lock.
  473. */
  474. rc = -EAGAIN;
  475. if (pass > 2)
  476. lock_page(page);
  477. else
  478. if (TestSetPageLocked(page))
  479. goto next;
  480. /*
  481. * Only wait on writeback if we have already done a pass where
  482. * we we may have triggered writeouts for lots of pages.
  483. */
  484. if (pass > 0) {
  485. wait_on_page_writeback(page);
  486. } else {
  487. if (PageWriteback(page))
  488. goto unlock_page;
  489. }
  490. /*
  491. * Anonymous pages must have swap cache references otherwise
  492. * the information contained in the page maps cannot be
  493. * preserved.
  494. */
  495. if (PageAnon(page) && !PageSwapCache(page)) {
  496. if (!add_to_swap(page, GFP_KERNEL)) {
  497. rc = -ENOMEM;
  498. goto unlock_page;
  499. }
  500. }
  501. if (!to) {
  502. rc = swap_page(page);
  503. goto next;
  504. }
  505. /*
  506. * Establish swap ptes for anonymous pages or destroy pte
  507. * maps for files.
  508. *
  509. * In order to reestablish file backed mappings the fault handlers
  510. * will take the radix tree_lock which may then be used to stop
  511. * processses from accessing this page until the new page is ready.
  512. *
  513. * A process accessing via a swap pte (an anonymous page) will take a
  514. * page_lock on the old page which will block the process until the
  515. * migration attempt is complete. At that time the PageSwapCache bit
  516. * will be examined. If the page was migrated then the PageSwapCache
  517. * bit will be clear and the operation to retrieve the page will be
  518. * retried which will find the new page in the radix tree. Then a new
  519. * direct mapping may be generated based on the radix tree contents.
  520. *
  521. * If the page was not migrated then the PageSwapCache bit
  522. * is still set and the operation may continue.
  523. */
  524. rc = -EPERM;
  525. if (try_to_unmap(page, 1) == SWAP_FAIL)
  526. /* A vma has VM_LOCKED set -> permanent failure */
  527. goto unlock_page;
  528. rc = -EAGAIN;
  529. if (page_mapped(page))
  530. goto unlock_page;
  531. newpage = lru_to_page(to);
  532. lock_page(newpage);
  533. /* Prepare mapping for the new page.*/
  534. newpage->index = page->index;
  535. newpage->mapping = page->mapping;
  536. /*
  537. * Pages are properly locked and writeback is complete.
  538. * Try to migrate the page.
  539. */
  540. mapping = page_mapping(page);
  541. if (!mapping)
  542. goto unlock_both;
  543. if (mapping->a_ops->migratepage)
  544. /*
  545. * Most pages have a mapping and most filesystems
  546. * should provide a migration function. Anonymous
  547. * pages are part of swap space which also has its
  548. * own migration function. This is the most common
  549. * path for page migration.
  550. */
  551. rc = mapping->a_ops->migratepage(mapping,
  552. newpage, page);
  553. else
  554. rc = fallback_migrate_page(mapping, newpage, page);
  555. unlock_both:
  556. unlock_page(newpage);
  557. unlock_page:
  558. unlock_page(page);
  559. next:
  560. if (rc) {
  561. if (newpage)
  562. newpage->mapping = NULL;
  563. if (rc == -EAGAIN)
  564. retry++;
  565. else {
  566. /* Permanent failure */
  567. list_move(&page->lru, failed);
  568. nr_failed++;
  569. }
  570. } else {
  571. if (newpage) {
  572. /* Successful migration. Return page to LRU */
  573. move_to_lru(newpage);
  574. }
  575. list_move(&page->lru, moved);
  576. }
  577. }
  578. if (retry && pass++ < 10)
  579. goto redo;
  580. if (!swapwrite)
  581. current->flags &= ~PF_SWAPWRITE;
  582. return nr_failed + retry;
  583. }
  584. /*
  585. * Migrate the list 'pagelist' of pages to a certain destination.
  586. *
  587. * Specify destination with either non-NULL vma or dest_node >= 0
  588. * Return the number of pages not migrated or error code
  589. */
  590. int migrate_pages_to(struct list_head *pagelist,
  591. struct vm_area_struct *vma, int dest)
  592. {
  593. LIST_HEAD(newlist);
  594. LIST_HEAD(moved);
  595. LIST_HEAD(failed);
  596. int err = 0;
  597. unsigned long offset = 0;
  598. int nr_pages;
  599. struct page *page;
  600. struct list_head *p;
  601. redo:
  602. nr_pages = 0;
  603. list_for_each(p, pagelist) {
  604. if (vma) {
  605. /*
  606. * The address passed to alloc_page_vma is used to
  607. * generate the proper interleave behavior. We fake
  608. * the address here by an increasing offset in order
  609. * to get the proper distribution of pages.
  610. *
  611. * No decision has been made as to which page
  612. * a certain old page is moved to so we cannot
  613. * specify the correct address.
  614. */
  615. page = alloc_page_vma(GFP_HIGHUSER, vma,
  616. offset + vma->vm_start);
  617. offset += PAGE_SIZE;
  618. }
  619. else
  620. page = alloc_pages_node(dest, GFP_HIGHUSER, 0);
  621. if (!page) {
  622. err = -ENOMEM;
  623. goto out;
  624. }
  625. list_add_tail(&page->lru, &newlist);
  626. nr_pages++;
  627. if (nr_pages > MIGRATE_CHUNK_SIZE)
  628. break;
  629. }
  630. err = migrate_pages(pagelist, &newlist, &moved, &failed);
  631. putback_lru_pages(&moved); /* Call release pages instead ?? */
  632. if (err >= 0 && list_empty(&newlist) && !list_empty(pagelist))
  633. goto redo;
  634. out:
  635. /* Return leftover allocated pages */
  636. while (!list_empty(&newlist)) {
  637. page = list_entry(newlist.next, struct page, lru);
  638. list_del(&page->lru);
  639. __free_page(page);
  640. }
  641. list_splice(&failed, pagelist);
  642. if (err < 0)
  643. return err;
  644. /* Calculate number of leftover pages */
  645. nr_pages = 0;
  646. list_for_each(p, pagelist)
  647. nr_pages++;
  648. return nr_pages;
  649. }