migrate.c 15 KB

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