migrate.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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 <linux/writeback.h>
  27. #include "internal.h"
  28. #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
  29. /*
  30. * Isolate one page from the LRU lists. If successful put it onto
  31. * the indicated list with elevated page count.
  32. *
  33. * Result:
  34. * -EBUSY: page not on LRU list
  35. * 0: page removed from LRU list and added to the specified list.
  36. */
  37. int isolate_lru_page(struct page *page, struct list_head *pagelist)
  38. {
  39. int ret = -EBUSY;
  40. if (PageLRU(page)) {
  41. struct zone *zone = page_zone(page);
  42. spin_lock_irq(&zone->lru_lock);
  43. if (PageLRU(page)) {
  44. ret = 0;
  45. get_page(page);
  46. ClearPageLRU(page);
  47. if (PageActive(page))
  48. del_page_from_active_list(zone, page);
  49. else
  50. del_page_from_inactive_list(zone, page);
  51. list_add_tail(&page->lru, pagelist);
  52. }
  53. spin_unlock_irq(&zone->lru_lock);
  54. }
  55. return ret;
  56. }
  57. /*
  58. * migrate_prep() needs to be called after we have compiled the list of pages
  59. * to be migrated using isolate_lru_page() but before we begin a series of calls
  60. * to migrate_pages().
  61. */
  62. int migrate_prep(void)
  63. {
  64. /*
  65. * Clear the LRU lists so pages can be isolated.
  66. * Note that pages may be moved off the LRU after we have
  67. * drained them. Those pages will fail to migrate like other
  68. * pages that may be busy.
  69. */
  70. lru_add_drain_all();
  71. return 0;
  72. }
  73. static inline void move_to_lru(struct page *page)
  74. {
  75. if (PageActive(page)) {
  76. /*
  77. * lru_cache_add_active checks that
  78. * the PG_active bit is off.
  79. */
  80. ClearPageActive(page);
  81. lru_cache_add_active(page);
  82. } else {
  83. lru_cache_add(page);
  84. }
  85. put_page(page);
  86. }
  87. /*
  88. * Add isolated pages on the list back to the LRU.
  89. *
  90. * returns the number of pages put back.
  91. */
  92. int putback_lru_pages(struct list_head *l)
  93. {
  94. struct page *page;
  95. struct page *page2;
  96. int count = 0;
  97. list_for_each_entry_safe(page, page2, l, lru) {
  98. list_del(&page->lru);
  99. move_to_lru(page);
  100. count++;
  101. }
  102. return count;
  103. }
  104. static inline int is_swap_pte(pte_t pte)
  105. {
  106. return !pte_none(pte) && !pte_present(pte) && !pte_file(pte);
  107. }
  108. /*
  109. * Restore a potential migration pte to a working pte entry
  110. */
  111. static void remove_migration_pte(struct vm_area_struct *vma,
  112. struct page *old, struct page *new)
  113. {
  114. struct mm_struct *mm = vma->vm_mm;
  115. swp_entry_t entry;
  116. pgd_t *pgd;
  117. pud_t *pud;
  118. pmd_t *pmd;
  119. pte_t *ptep, pte;
  120. spinlock_t *ptl;
  121. unsigned long addr = page_address_in_vma(new, vma);
  122. if (addr == -EFAULT)
  123. return;
  124. pgd = pgd_offset(mm, addr);
  125. if (!pgd_present(*pgd))
  126. return;
  127. pud = pud_offset(pgd, addr);
  128. if (!pud_present(*pud))
  129. return;
  130. pmd = pmd_offset(pud, addr);
  131. if (!pmd_present(*pmd))
  132. return;
  133. ptep = pte_offset_map(pmd, addr);
  134. if (!is_swap_pte(*ptep)) {
  135. pte_unmap(ptep);
  136. return;
  137. }
  138. ptl = pte_lockptr(mm, pmd);
  139. spin_lock(ptl);
  140. pte = *ptep;
  141. if (!is_swap_pte(pte))
  142. goto out;
  143. entry = pte_to_swp_entry(pte);
  144. if (!is_migration_entry(entry) || migration_entry_to_page(entry) != old)
  145. goto out;
  146. get_page(new);
  147. pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
  148. if (is_write_migration_entry(entry))
  149. pte = pte_mkwrite(pte);
  150. set_pte_at(mm, addr, ptep, pte);
  151. if (PageAnon(new))
  152. page_add_anon_rmap(new, vma, addr);
  153. else
  154. page_add_file_rmap(new);
  155. /* No need to invalidate - it was non-present before */
  156. update_mmu_cache(vma, addr, pte);
  157. lazy_mmu_prot_update(pte);
  158. out:
  159. pte_unmap_unlock(ptep, ptl);
  160. }
  161. /*
  162. * Note that remove_file_migration_ptes will only work on regular mappings,
  163. * Nonlinear mappings do not use migration entries.
  164. */
  165. static void remove_file_migration_ptes(struct page *old, struct page *new)
  166. {
  167. struct vm_area_struct *vma;
  168. struct address_space *mapping = page_mapping(new);
  169. struct prio_tree_iter iter;
  170. pgoff_t pgoff = new->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  171. if (!mapping)
  172. return;
  173. spin_lock(&mapping->i_mmap_lock);
  174. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff)
  175. remove_migration_pte(vma, old, new);
  176. spin_unlock(&mapping->i_mmap_lock);
  177. }
  178. /*
  179. * Must hold mmap_sem lock on at least one of the vmas containing
  180. * the page so that the anon_vma cannot vanish.
  181. */
  182. static void remove_anon_migration_ptes(struct page *old, struct page *new)
  183. {
  184. struct anon_vma *anon_vma;
  185. struct vm_area_struct *vma;
  186. unsigned long mapping;
  187. mapping = (unsigned long)new->mapping;
  188. if (!mapping || (mapping & PAGE_MAPPING_ANON) == 0)
  189. return;
  190. /*
  191. * We hold the mmap_sem lock. So no need to call page_lock_anon_vma.
  192. */
  193. anon_vma = (struct anon_vma *) (mapping - PAGE_MAPPING_ANON);
  194. spin_lock(&anon_vma->lock);
  195. list_for_each_entry(vma, &anon_vma->head, anon_vma_node)
  196. remove_migration_pte(vma, old, new);
  197. spin_unlock(&anon_vma->lock);
  198. }
  199. /*
  200. * Get rid of all migration entries and replace them by
  201. * references to the indicated page.
  202. */
  203. static void remove_migration_ptes(struct page *old, struct page *new)
  204. {
  205. if (PageAnon(new))
  206. remove_anon_migration_ptes(old, new);
  207. else
  208. remove_file_migration_ptes(old, new);
  209. }
  210. /*
  211. * Something used the pte of a page under migration. We need to
  212. * get to the page and wait until migration is finished.
  213. * When we return from this function the fault will be retried.
  214. *
  215. * This function is called from do_swap_page().
  216. */
  217. void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
  218. unsigned long address)
  219. {
  220. pte_t *ptep, pte;
  221. spinlock_t *ptl;
  222. swp_entry_t entry;
  223. struct page *page;
  224. ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
  225. pte = *ptep;
  226. if (!is_swap_pte(pte))
  227. goto out;
  228. entry = pte_to_swp_entry(pte);
  229. if (!is_migration_entry(entry))
  230. goto out;
  231. page = migration_entry_to_page(entry);
  232. get_page(page);
  233. pte_unmap_unlock(ptep, ptl);
  234. wait_on_page_locked(page);
  235. put_page(page);
  236. return;
  237. out:
  238. pte_unmap_unlock(ptep, ptl);
  239. }
  240. /*
  241. * Replace the page in the mapping.
  242. *
  243. * The number of remaining references must be:
  244. * 1 for anonymous pages without a mapping
  245. * 2 for pages with a mapping
  246. * 3 for pages with a mapping and PagePrivate set.
  247. */
  248. static int migrate_page_move_mapping(struct address_space *mapping,
  249. struct page *newpage, struct page *page)
  250. {
  251. struct page **radix_pointer;
  252. if (!mapping) {
  253. /* Anonymous page */
  254. if (page_count(page) != 1)
  255. return -EAGAIN;
  256. return 0;
  257. }
  258. write_lock_irq(&mapping->tree_lock);
  259. radix_pointer = (struct page **)radix_tree_lookup_slot(
  260. &mapping->page_tree,
  261. page_index(page));
  262. if (page_count(page) != 2 + !!PagePrivate(page) ||
  263. *radix_pointer != page) {
  264. write_unlock_irq(&mapping->tree_lock);
  265. return -EAGAIN;
  266. }
  267. /*
  268. * Now we know that no one else is looking at the page.
  269. */
  270. get_page(newpage);
  271. #ifdef CONFIG_SWAP
  272. if (PageSwapCache(page)) {
  273. SetPageSwapCache(newpage);
  274. set_page_private(newpage, page_private(page));
  275. }
  276. #endif
  277. *radix_pointer = newpage;
  278. __put_page(page);
  279. write_unlock_irq(&mapping->tree_lock);
  280. return 0;
  281. }
  282. /*
  283. * Copy the page to its new location
  284. */
  285. static void migrate_page_copy(struct page *newpage, struct page *page)
  286. {
  287. copy_highpage(newpage, page);
  288. if (PageError(page))
  289. SetPageError(newpage);
  290. if (PageReferenced(page))
  291. SetPageReferenced(newpage);
  292. if (PageUptodate(page))
  293. SetPageUptodate(newpage);
  294. if (PageActive(page))
  295. SetPageActive(newpage);
  296. if (PageChecked(page))
  297. SetPageChecked(newpage);
  298. if (PageMappedToDisk(page))
  299. SetPageMappedToDisk(newpage);
  300. if (PageDirty(page)) {
  301. clear_page_dirty_for_io(page);
  302. set_page_dirty(newpage);
  303. }
  304. #ifdef CONFIG_SWAP
  305. ClearPageSwapCache(page);
  306. #endif
  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. return 0;
  344. }
  345. EXPORT_SYMBOL(migrate_page);
  346. /*
  347. * Migration function for pages with buffers. This function can only be used
  348. * if the underlying filesystem guarantees that no other references to "page"
  349. * exist.
  350. */
  351. int buffer_migrate_page(struct address_space *mapping,
  352. struct page *newpage, struct page *page)
  353. {
  354. struct buffer_head *bh, *head;
  355. int rc;
  356. if (!page_has_buffers(page))
  357. return migrate_page(mapping, newpage, page);
  358. head = page_buffers(page);
  359. rc = migrate_page_move_mapping(mapping, newpage, page);
  360. if (rc)
  361. return rc;
  362. bh = head;
  363. do {
  364. get_bh(bh);
  365. lock_buffer(bh);
  366. bh = bh->b_this_page;
  367. } while (bh != head);
  368. ClearPagePrivate(page);
  369. set_page_private(newpage, page_private(page));
  370. set_page_private(page, 0);
  371. put_page(page);
  372. get_page(newpage);
  373. bh = head;
  374. do {
  375. set_bh_page(bh, newpage, bh_offset(bh));
  376. bh = bh->b_this_page;
  377. } while (bh != head);
  378. SetPagePrivate(newpage);
  379. migrate_page_copy(newpage, page);
  380. bh = head;
  381. do {
  382. unlock_buffer(bh);
  383. put_bh(bh);
  384. bh = bh->b_this_page;
  385. } while (bh != head);
  386. return 0;
  387. }
  388. EXPORT_SYMBOL(buffer_migrate_page);
  389. /*
  390. * Writeback a page to clean the dirty state
  391. */
  392. static int writeout(struct address_space *mapping, struct page *page)
  393. {
  394. struct writeback_control wbc = {
  395. .sync_mode = WB_SYNC_NONE,
  396. .nr_to_write = 1,
  397. .range_start = 0,
  398. .range_end = LLONG_MAX,
  399. .nonblocking = 1,
  400. .for_reclaim = 1
  401. };
  402. int rc;
  403. if (!mapping->a_ops->writepage)
  404. /* No write method for the address space */
  405. return -EINVAL;
  406. if (!clear_page_dirty_for_io(page))
  407. /* Someone else already triggered a write */
  408. return -EAGAIN;
  409. /*
  410. * A dirty page may imply that the underlying filesystem has
  411. * the page on some queue. So the page must be clean for
  412. * migration. Writeout may mean we loose the lock and the
  413. * page state is no longer what we checked for earlier.
  414. * At this point we know that the migration attempt cannot
  415. * be successful.
  416. */
  417. remove_migration_ptes(page, page);
  418. rc = mapping->a_ops->writepage(page, &wbc);
  419. if (rc < 0)
  420. /* I/O Error writing */
  421. return -EIO;
  422. if (rc != AOP_WRITEPAGE_ACTIVATE)
  423. /* unlocked. Relock */
  424. lock_page(page);
  425. return -EAGAIN;
  426. }
  427. /*
  428. * Default handling if a filesystem does not provide a migration function.
  429. */
  430. static int fallback_migrate_page(struct address_space *mapping,
  431. struct page *newpage, struct page *page)
  432. {
  433. if (PageDirty(page))
  434. return writeout(mapping, page);
  435. /*
  436. * Buffers may be managed in a filesystem specific way.
  437. * We must have no buffers or drop them.
  438. */
  439. if (page_has_buffers(page) &&
  440. !try_to_release_page(page, GFP_KERNEL))
  441. return -EAGAIN;
  442. return migrate_page(mapping, newpage, page);
  443. }
  444. /*
  445. * Move a page to a newly allocated page
  446. * The page is locked and all ptes have been successfully removed.
  447. *
  448. * The new page will have replaced the old page if this function
  449. * is successful.
  450. */
  451. static int move_to_new_page(struct page *newpage, struct page *page)
  452. {
  453. struct address_space *mapping;
  454. int rc;
  455. /*
  456. * Block others from accessing the page when we get around to
  457. * establishing additional references. We are the only one
  458. * holding a reference to the new page at this point.
  459. */
  460. if (TestSetPageLocked(newpage))
  461. BUG();
  462. /* Prepare mapping for the new page.*/
  463. newpage->index = page->index;
  464. newpage->mapping = page->mapping;
  465. mapping = page_mapping(page);
  466. if (!mapping)
  467. rc = migrate_page(mapping, newpage, page);
  468. else if (mapping->a_ops->migratepage)
  469. /*
  470. * Most pages have a mapping and most filesystems
  471. * should provide a migration function. Anonymous
  472. * pages are part of swap space which also has its
  473. * own migration function. This is the most common
  474. * path for page migration.
  475. */
  476. rc = mapping->a_ops->migratepage(mapping,
  477. newpage, page);
  478. else
  479. rc = fallback_migrate_page(mapping, newpage, page);
  480. if (!rc)
  481. remove_migration_ptes(page, newpage);
  482. else
  483. newpage->mapping = NULL;
  484. unlock_page(newpage);
  485. return rc;
  486. }
  487. /*
  488. * Obtain the lock on page, remove all ptes and migrate the page
  489. * to the newly allocated page in newpage.
  490. */
  491. static int unmap_and_move(new_page_t get_new_page, unsigned long private,
  492. struct page *page, int force)
  493. {
  494. int rc = 0;
  495. struct page *newpage = get_new_page(page, private);
  496. if (!newpage)
  497. return -ENOMEM;
  498. if (page_count(page) == 1)
  499. /* page was freed from under us. So we are done. */
  500. goto move_newpage;
  501. rc = -EAGAIN;
  502. if (TestSetPageLocked(page)) {
  503. if (!force)
  504. goto move_newpage;
  505. lock_page(page);
  506. }
  507. if (PageWriteback(page)) {
  508. if (!force)
  509. goto unlock;
  510. wait_on_page_writeback(page);
  511. }
  512. /*
  513. * Establish migration ptes or remove ptes
  514. */
  515. if (try_to_unmap(page, 1) != SWAP_FAIL) {
  516. if (!page_mapped(page))
  517. rc = move_to_new_page(newpage, page);
  518. } else
  519. /* A vma has VM_LOCKED set -> permanent failure */
  520. rc = -EPERM;
  521. if (rc)
  522. remove_migration_ptes(page, page);
  523. unlock:
  524. unlock_page(page);
  525. if (rc != -EAGAIN) {
  526. /*
  527. * A page that has been migrated has all references
  528. * removed and will be freed. A page that has not been
  529. * migrated will have kepts its references and be
  530. * restored.
  531. */
  532. list_del(&page->lru);
  533. move_to_lru(page);
  534. }
  535. move_newpage:
  536. /*
  537. * Move the new page to the LRU. If migration was not successful
  538. * then this will free the page.
  539. */
  540. move_to_lru(newpage);
  541. return rc;
  542. }
  543. /*
  544. * migrate_pages
  545. *
  546. * The function takes one list of pages to migrate and a function
  547. * that determines from the page to be migrated and the private data
  548. * the target of the move and allocates the page.
  549. *
  550. * The function returns after 10 attempts or if no pages
  551. * are movable anymore because to has become empty
  552. * or no retryable pages exist anymore. All pages will be
  553. * retruned to the LRU or freed.
  554. *
  555. * Return: Number of pages not migrated or error code.
  556. */
  557. int migrate_pages(struct list_head *from,
  558. new_page_t get_new_page, unsigned long private)
  559. {
  560. int retry = 1;
  561. int nr_failed = 0;
  562. int pass = 0;
  563. struct page *page;
  564. struct page *page2;
  565. int swapwrite = current->flags & PF_SWAPWRITE;
  566. int rc;
  567. if (!swapwrite)
  568. current->flags |= PF_SWAPWRITE;
  569. for(pass = 0; pass < 10 && retry; pass++) {
  570. retry = 0;
  571. list_for_each_entry_safe(page, page2, from, lru) {
  572. cond_resched();
  573. rc = unmap_and_move(get_new_page, private,
  574. page, pass > 2);
  575. switch(rc) {
  576. case -ENOMEM:
  577. goto out;
  578. case -EAGAIN:
  579. retry++;
  580. break;
  581. case 0:
  582. break;
  583. default:
  584. /* Permanent failure */
  585. nr_failed++;
  586. break;
  587. }
  588. }
  589. }
  590. rc = 0;
  591. out:
  592. if (!swapwrite)
  593. current->flags &= ~PF_SWAPWRITE;
  594. putback_lru_pages(from);
  595. if (rc)
  596. return rc;
  597. return nr_failed + retry;
  598. }