migrate.c 14 KB

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