migrate.c 16 KB

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