migrate.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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/pagemap.h>
  18. #include <linux/buffer_head.h>
  19. #include <linux/mm_inline.h>
  20. #include <linux/pagevec.h>
  21. #include <linux/rmap.h>
  22. #include <linux/topology.h>
  23. #include <linux/cpu.h>
  24. #include <linux/cpuset.h>
  25. #include <linux/swapops.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. /*
  109. * swapout a single page
  110. * page is locked upon entry, unlocked on exit
  111. */
  112. static int swap_page(struct page *page)
  113. {
  114. struct address_space *mapping = page_mapping(page);
  115. if (page_mapped(page) && mapping)
  116. if (try_to_unmap(page, 1) != SWAP_SUCCESS)
  117. goto unlock_retry;
  118. if (PageDirty(page)) {
  119. /* Page is dirty, try to write it out here */
  120. switch(pageout(page, mapping)) {
  121. case PAGE_KEEP:
  122. case PAGE_ACTIVATE:
  123. goto unlock_retry;
  124. case PAGE_SUCCESS:
  125. goto retry;
  126. case PAGE_CLEAN:
  127. ; /* try to free the page below */
  128. }
  129. }
  130. if (PagePrivate(page)) {
  131. if (!try_to_release_page(page, GFP_KERNEL) ||
  132. (!mapping && page_count(page) == 1))
  133. goto unlock_retry;
  134. }
  135. if (remove_mapping(mapping, page)) {
  136. /* Success */
  137. unlock_page(page);
  138. return 0;
  139. }
  140. unlock_retry:
  141. unlock_page(page);
  142. retry:
  143. return -EAGAIN;
  144. }
  145. /*
  146. * Remove references for a page and establish the new page with the correct
  147. * basic settings to be able to stop accesses to the page.
  148. *
  149. * The number of remaining references must be:
  150. * 1 for anonymous pages without a mapping
  151. * 2 for pages with a mapping
  152. * 3 for pages with a mapping and PagePrivate set.
  153. */
  154. static int migrate_page_remove_references(struct page *newpage,
  155. struct page *page)
  156. {
  157. struct address_space *mapping = page_mapping(page);
  158. struct page **radix_pointer;
  159. if (!mapping)
  160. return -EAGAIN;
  161. /*
  162. * Establish swap ptes for anonymous pages or destroy pte
  163. * maps for files.
  164. *
  165. * In order to reestablish file backed mappings the fault handlers
  166. * will take the radix tree_lock which may then be used to stop
  167. * processses from accessing this page until the new page is ready.
  168. *
  169. * A process accessing via a swap pte (an anonymous page) will take a
  170. * page_lock on the old page which will block the process until the
  171. * migration attempt is complete. At that time the PageSwapCache bit
  172. * will be examined. If the page was migrated then the PageSwapCache
  173. * bit will be clear and the operation to retrieve the page will be
  174. * retried which will find the new page in the radix tree. Then a new
  175. * direct mapping may be generated based on the radix tree contents.
  176. *
  177. * If the page was not migrated then the PageSwapCache bit
  178. * is still set and the operation may continue.
  179. */
  180. if (try_to_unmap(page, 1) == SWAP_FAIL)
  181. /* A vma has VM_LOCKED set -> permanent failure */
  182. return -EPERM;
  183. /*
  184. * Give up if we were unable to remove all mappings.
  185. */
  186. if (page_mapcount(page))
  187. return -EAGAIN;
  188. write_lock_irq(&mapping->tree_lock);
  189. radix_pointer = (struct page **)radix_tree_lookup_slot(
  190. &mapping->page_tree,
  191. page_index(page));
  192. if (!page_mapping(page) ||
  193. page_count(page) != 2 + !!PagePrivate(page) ||
  194. *radix_pointer != page) {
  195. write_unlock_irq(&mapping->tree_lock);
  196. return -EAGAIN;
  197. }
  198. /*
  199. * Now we know that no one else is looking at the page.
  200. *
  201. * Certain minimal information about a page must be available
  202. * in order for other subsystems to properly handle the page if they
  203. * find it through the radix tree update before we are finished
  204. * copying the page.
  205. */
  206. get_page(newpage);
  207. newpage->index = page->index;
  208. newpage->mapping = page->mapping;
  209. if (PageSwapCache(page)) {
  210. SetPageSwapCache(newpage);
  211. set_page_private(newpage, page_private(page));
  212. }
  213. *radix_pointer = newpage;
  214. __put_page(page);
  215. write_unlock_irq(&mapping->tree_lock);
  216. return 0;
  217. }
  218. /*
  219. * Copy the page to its new location
  220. */
  221. static void migrate_page_copy(struct page *newpage, struct page *page)
  222. {
  223. copy_highpage(newpage, page);
  224. if (PageError(page))
  225. SetPageError(newpage);
  226. if (PageReferenced(page))
  227. SetPageReferenced(newpage);
  228. if (PageUptodate(page))
  229. SetPageUptodate(newpage);
  230. if (PageActive(page))
  231. SetPageActive(newpage);
  232. if (PageChecked(page))
  233. SetPageChecked(newpage);
  234. if (PageMappedToDisk(page))
  235. SetPageMappedToDisk(newpage);
  236. if (PageDirty(page)) {
  237. clear_page_dirty_for_io(page);
  238. set_page_dirty(newpage);
  239. }
  240. ClearPageSwapCache(page);
  241. ClearPageActive(page);
  242. ClearPagePrivate(page);
  243. set_page_private(page, 0);
  244. page->mapping = NULL;
  245. /*
  246. * If any waiters have accumulated on the new page then
  247. * wake them up.
  248. */
  249. if (PageWriteback(newpage))
  250. end_page_writeback(newpage);
  251. }
  252. /************************************************************
  253. * Migration functions
  254. ***********************************************************/
  255. /* Always fail migration. Used for mappings that are not movable */
  256. int fail_migrate_page(struct page *newpage, struct page *page)
  257. {
  258. return -EIO;
  259. }
  260. EXPORT_SYMBOL(fail_migrate_page);
  261. /*
  262. * Common logic to directly migrate a single page suitable for
  263. * pages that do not use PagePrivate.
  264. *
  265. * Pages are locked upon entry and exit.
  266. */
  267. int migrate_page(struct page *newpage, struct page *page)
  268. {
  269. int rc;
  270. BUG_ON(PageWriteback(page)); /* Writeback must be complete */
  271. rc = migrate_page_remove_references(newpage, page);
  272. if (rc)
  273. return rc;
  274. migrate_page_copy(newpage, page);
  275. /*
  276. * Remove auxiliary swap entries and replace
  277. * them with real ptes.
  278. *
  279. * Note that a real pte entry will allow processes that are not
  280. * waiting on the page lock to use the new page via the page tables
  281. * before the new page is unlocked.
  282. */
  283. remove_from_swap(newpage);
  284. return 0;
  285. }
  286. EXPORT_SYMBOL(migrate_page);
  287. /*
  288. * Migration function for pages with buffers. This function can only be used
  289. * if the underlying filesystem guarantees that no other references to "page"
  290. * exist.
  291. */
  292. int buffer_migrate_page(struct page *newpage, struct page *page)
  293. {
  294. struct address_space *mapping = page->mapping;
  295. struct buffer_head *bh, *head;
  296. int rc;
  297. if (!mapping)
  298. return -EAGAIN;
  299. if (!page_has_buffers(page))
  300. return migrate_page(newpage, page);
  301. head = page_buffers(page);
  302. rc = migrate_page_remove_references(newpage, page);
  303. if (rc)
  304. return rc;
  305. bh = head;
  306. do {
  307. get_bh(bh);
  308. lock_buffer(bh);
  309. bh = bh->b_this_page;
  310. } while (bh != head);
  311. ClearPagePrivate(page);
  312. set_page_private(newpage, page_private(page));
  313. set_page_private(page, 0);
  314. put_page(page);
  315. get_page(newpage);
  316. bh = head;
  317. do {
  318. set_bh_page(bh, newpage, bh_offset(bh));
  319. bh = bh->b_this_page;
  320. } while (bh != head);
  321. SetPagePrivate(newpage);
  322. migrate_page_copy(newpage, page);
  323. bh = head;
  324. do {
  325. unlock_buffer(bh);
  326. put_bh(bh);
  327. bh = bh->b_this_page;
  328. } while (bh != head);
  329. return 0;
  330. }
  331. EXPORT_SYMBOL(buffer_migrate_page);
  332. /*
  333. * migrate_pages
  334. *
  335. * Two lists are passed to this function. The first list
  336. * contains the pages isolated from the LRU to be migrated.
  337. * The second list contains new pages that the pages isolated
  338. * can be moved to. If the second list is NULL then all
  339. * pages are swapped out.
  340. *
  341. * The function returns after 10 attempts or if no pages
  342. * are movable anymore because to has become empty
  343. * or no retryable pages exist anymore.
  344. *
  345. * Return: Number of pages not migrated when "to" ran empty.
  346. */
  347. int migrate_pages(struct list_head *from, struct list_head *to,
  348. struct list_head *moved, struct list_head *failed)
  349. {
  350. int retry;
  351. int nr_failed = 0;
  352. int pass = 0;
  353. struct page *page;
  354. struct page *page2;
  355. int swapwrite = current->flags & PF_SWAPWRITE;
  356. int rc;
  357. if (!swapwrite)
  358. current->flags |= PF_SWAPWRITE;
  359. redo:
  360. retry = 0;
  361. list_for_each_entry_safe(page, page2, from, lru) {
  362. struct page *newpage = NULL;
  363. struct address_space *mapping;
  364. cond_resched();
  365. rc = 0;
  366. if (page_count(page) == 1)
  367. /* page was freed from under us. So we are done. */
  368. goto next;
  369. if (to && list_empty(to))
  370. break;
  371. /*
  372. * Skip locked pages during the first two passes to give the
  373. * functions holding the lock time to release the page. Later we
  374. * use lock_page() to have a higher chance of acquiring the
  375. * lock.
  376. */
  377. rc = -EAGAIN;
  378. if (pass > 2)
  379. lock_page(page);
  380. else
  381. if (TestSetPageLocked(page))
  382. goto next;
  383. /*
  384. * Only wait on writeback if we have already done a pass where
  385. * we we may have triggered writeouts for lots of pages.
  386. */
  387. if (pass > 0) {
  388. wait_on_page_writeback(page);
  389. } else {
  390. if (PageWriteback(page))
  391. goto unlock_page;
  392. }
  393. /*
  394. * Anonymous pages must have swap cache references otherwise
  395. * the information contained in the page maps cannot be
  396. * preserved.
  397. */
  398. if (PageAnon(page) && !PageSwapCache(page)) {
  399. if (!add_to_swap(page, GFP_KERNEL)) {
  400. rc = -ENOMEM;
  401. goto unlock_page;
  402. }
  403. }
  404. if (!to) {
  405. rc = swap_page(page);
  406. goto next;
  407. }
  408. newpage = lru_to_page(to);
  409. lock_page(newpage);
  410. /*
  411. * Pages are properly locked and writeback is complete.
  412. * Try to migrate the page.
  413. */
  414. mapping = page_mapping(page);
  415. if (!mapping)
  416. goto unlock_both;
  417. if (mapping->a_ops->migratepage) {
  418. /*
  419. * Most pages have a mapping and most filesystems
  420. * should provide a migration function. Anonymous
  421. * pages are part of swap space which also has its
  422. * own migration function. This is the most common
  423. * path for page migration.
  424. */
  425. rc = mapping->a_ops->migratepage(newpage, page);
  426. goto unlock_both;
  427. }
  428. /* Make sure the dirty bit is up to date */
  429. if (try_to_unmap(page, 1) == SWAP_FAIL) {
  430. rc = -EPERM;
  431. goto unlock_both;
  432. }
  433. if (page_mapcount(page)) {
  434. rc = -EAGAIN;
  435. goto unlock_both;
  436. }
  437. /*
  438. * Default handling if a filesystem does not provide
  439. * a migration function. We can only migrate clean
  440. * pages so try to write out any dirty pages first.
  441. */
  442. if (PageDirty(page)) {
  443. switch (pageout(page, mapping)) {
  444. case PAGE_KEEP:
  445. case PAGE_ACTIVATE:
  446. goto unlock_both;
  447. case PAGE_SUCCESS:
  448. unlock_page(newpage);
  449. goto next;
  450. case PAGE_CLEAN:
  451. ; /* try to migrate the page below */
  452. }
  453. }
  454. /*
  455. * Buffers are managed in a filesystem specific way.
  456. * We must have no buffers or drop them.
  457. */
  458. if (!page_has_buffers(page) ||
  459. try_to_release_page(page, GFP_KERNEL)) {
  460. rc = migrate_page(newpage, page);
  461. goto unlock_both;
  462. }
  463. /*
  464. * On early passes with mapped pages simply
  465. * retry. There may be a lock held for some
  466. * buffers that may go away. Later
  467. * swap them out.
  468. */
  469. if (pass > 4) {
  470. /*
  471. * Persistently unable to drop buffers..... As a
  472. * measure of last resort we fall back to
  473. * swap_page().
  474. */
  475. unlock_page(newpage);
  476. newpage = NULL;
  477. rc = swap_page(page);
  478. goto next;
  479. }
  480. unlock_both:
  481. unlock_page(newpage);
  482. unlock_page:
  483. unlock_page(page);
  484. next:
  485. if (rc == -EAGAIN) {
  486. retry++;
  487. } else if (rc) {
  488. /* Permanent failure */
  489. list_move(&page->lru, failed);
  490. nr_failed++;
  491. } else {
  492. if (newpage) {
  493. /* Successful migration. Return page to LRU */
  494. move_to_lru(newpage);
  495. }
  496. list_move(&page->lru, moved);
  497. }
  498. }
  499. if (retry && pass++ < 10)
  500. goto redo;
  501. if (!swapwrite)
  502. current->flags &= ~PF_SWAPWRITE;
  503. return nr_failed + retry;
  504. }
  505. /*
  506. * Migrate the list 'pagelist' of pages to a certain destination.
  507. *
  508. * Specify destination with either non-NULL vma or dest_node >= 0
  509. * Return the number of pages not migrated or error code
  510. */
  511. int migrate_pages_to(struct list_head *pagelist,
  512. struct vm_area_struct *vma, int dest)
  513. {
  514. LIST_HEAD(newlist);
  515. LIST_HEAD(moved);
  516. LIST_HEAD(failed);
  517. int err = 0;
  518. unsigned long offset = 0;
  519. int nr_pages;
  520. struct page *page;
  521. struct list_head *p;
  522. redo:
  523. nr_pages = 0;
  524. list_for_each(p, pagelist) {
  525. if (vma) {
  526. /*
  527. * The address passed to alloc_page_vma is used to
  528. * generate the proper interleave behavior. We fake
  529. * the address here by an increasing offset in order
  530. * to get the proper distribution of pages.
  531. *
  532. * No decision has been made as to which page
  533. * a certain old page is moved to so we cannot
  534. * specify the correct address.
  535. */
  536. page = alloc_page_vma(GFP_HIGHUSER, vma,
  537. offset + vma->vm_start);
  538. offset += PAGE_SIZE;
  539. }
  540. else
  541. page = alloc_pages_node(dest, GFP_HIGHUSER, 0);
  542. if (!page) {
  543. err = -ENOMEM;
  544. goto out;
  545. }
  546. list_add_tail(&page->lru, &newlist);
  547. nr_pages++;
  548. if (nr_pages > MIGRATE_CHUNK_SIZE)
  549. break;
  550. }
  551. err = migrate_pages(pagelist, &newlist, &moved, &failed);
  552. putback_lru_pages(&moved); /* Call release pages instead ?? */
  553. if (err >= 0 && list_empty(&newlist) && !list_empty(pagelist))
  554. goto redo;
  555. out:
  556. /* Return leftover allocated pages */
  557. while (!list_empty(&newlist)) {
  558. page = list_entry(newlist.next, struct page, lru);
  559. list_del(&page->lru);
  560. __free_page(page);
  561. }
  562. list_splice(&failed, pagelist);
  563. if (err < 0)
  564. return err;
  565. /* Calculate number of leftover pages */
  566. nr_pages = 0;
  567. list_for_each(p, pagelist)
  568. nr_pages++;
  569. return nr_pages;
  570. }