migrate.c 15 KB

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