migrate.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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/nsproxy.h>
  22. #include <linux/pagevec.h>
  23. #include <linux/rmap.h>
  24. #include <linux/topology.h>
  25. #include <linux/cpu.h>
  26. #include <linux/cpuset.h>
  27. #include <linux/writeback.h>
  28. #include <linux/mempolicy.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/security.h>
  31. #include <linux/memcontrol.h>
  32. #include "internal.h"
  33. #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
  34. /*
  35. * Isolate one page from the LRU lists. If successful put it onto
  36. * the indicated list with elevated page count.
  37. *
  38. * Result:
  39. * -EBUSY: page not on LRU list
  40. * 0: page removed from LRU list and added to the specified list.
  41. */
  42. int isolate_lru_page(struct page *page, struct list_head *pagelist)
  43. {
  44. int ret = -EBUSY;
  45. if (PageLRU(page)) {
  46. struct zone *zone = page_zone(page);
  47. spin_lock_irq(&zone->lru_lock);
  48. if (PageLRU(page) && get_page_unless_zero(page)) {
  49. ret = 0;
  50. ClearPageLRU(page);
  51. if (PageActive(page))
  52. del_page_from_active_list(zone, page);
  53. else
  54. del_page_from_inactive_list(zone, page);
  55. list_add_tail(&page->lru, pagelist);
  56. }
  57. spin_unlock_irq(&zone->lru_lock);
  58. }
  59. return ret;
  60. }
  61. /*
  62. * migrate_prep() needs to be called before we start compiling a list of pages
  63. * to be migrated using isolate_lru_page().
  64. */
  65. int migrate_prep(void)
  66. {
  67. /*
  68. * Clear the LRU lists so pages can be isolated.
  69. * Note that pages may be moved off the LRU after we have
  70. * drained them. Those pages will fail to migrate like other
  71. * pages that may be busy.
  72. */
  73. lru_add_drain_all();
  74. return 0;
  75. }
  76. static inline void move_to_lru(struct page *page)
  77. {
  78. if (PageActive(page)) {
  79. /*
  80. * lru_cache_add_active checks that
  81. * the PG_active bit is off.
  82. */
  83. ClearPageActive(page);
  84. lru_cache_add_active(page);
  85. } else {
  86. lru_cache_add(page);
  87. }
  88. put_page(page);
  89. }
  90. /*
  91. * Add isolated pages on the list back to the LRU.
  92. *
  93. * returns the number of pages put back.
  94. */
  95. int putback_lru_pages(struct list_head *l)
  96. {
  97. struct page *page;
  98. struct page *page2;
  99. int count = 0;
  100. list_for_each_entry_safe(page, page2, l, lru) {
  101. list_del(&page->lru);
  102. move_to_lru(page);
  103. count++;
  104. }
  105. return count;
  106. }
  107. /*
  108. * Restore a potential migration pte to a working pte entry
  109. */
  110. static void remove_migration_pte(struct vm_area_struct *vma,
  111. struct page *old, struct page *new)
  112. {
  113. struct mm_struct *mm = vma->vm_mm;
  114. swp_entry_t entry;
  115. pgd_t *pgd;
  116. pud_t *pud;
  117. pmd_t *pmd;
  118. pte_t *ptep, pte;
  119. spinlock_t *ptl;
  120. unsigned long addr = page_address_in_vma(new, vma);
  121. if (addr == -EFAULT)
  122. return;
  123. pgd = pgd_offset(mm, addr);
  124. if (!pgd_present(*pgd))
  125. return;
  126. pud = pud_offset(pgd, addr);
  127. if (!pud_present(*pud))
  128. return;
  129. pmd = pmd_offset(pud, addr);
  130. if (!pmd_present(*pmd))
  131. return;
  132. ptep = pte_offset_map(pmd, addr);
  133. if (!is_swap_pte(*ptep)) {
  134. pte_unmap(ptep);
  135. return;
  136. }
  137. ptl = pte_lockptr(mm, pmd);
  138. spin_lock(ptl);
  139. pte = *ptep;
  140. if (!is_swap_pte(pte))
  141. goto out;
  142. entry = pte_to_swp_entry(pte);
  143. if (!is_migration_entry(entry) || migration_entry_to_page(entry) != old)
  144. goto out;
  145. /*
  146. * Yes, ignore the return value from a GFP_ATOMIC mem_cgroup_charge.
  147. * Failure is not an option here: we're now expected to remove every
  148. * migration pte, and will cause crashes otherwise. Normally this
  149. * is not an issue: mem_cgroup_prepare_migration bumped up the old
  150. * page_cgroup count for safety, that's now attached to the new page,
  151. * so this charge should just be another incrementation of the count,
  152. * to keep in balance with rmap.c's mem_cgroup_uncharging. But if
  153. * there's been a force_empty, those reference counts may no longer
  154. * be reliable, and this charge can actually fail: oh well, we don't
  155. * make the situation any worse by proceeding as if it had succeeded.
  156. */
  157. mem_cgroup_charge(new, mm, GFP_ATOMIC);
  158. get_page(new);
  159. pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
  160. if (is_write_migration_entry(entry))
  161. pte = pte_mkwrite(pte);
  162. flush_cache_page(vma, addr, pte_pfn(pte));
  163. set_pte_at(mm, addr, ptep, pte);
  164. if (PageAnon(new))
  165. page_add_anon_rmap(new, vma, addr);
  166. else
  167. page_add_file_rmap(new);
  168. /* No need to invalidate - it was non-present before */
  169. update_mmu_cache(vma, addr, pte);
  170. out:
  171. pte_unmap_unlock(ptep, ptl);
  172. }
  173. /*
  174. * Note that remove_file_migration_ptes will only work on regular mappings,
  175. * Nonlinear mappings do not use migration entries.
  176. */
  177. static void remove_file_migration_ptes(struct page *old, struct page *new)
  178. {
  179. struct vm_area_struct *vma;
  180. struct address_space *mapping = page_mapping(new);
  181. struct prio_tree_iter iter;
  182. pgoff_t pgoff = new->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  183. if (!mapping)
  184. return;
  185. spin_lock(&mapping->i_mmap_lock);
  186. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff)
  187. remove_migration_pte(vma, old, new);
  188. spin_unlock(&mapping->i_mmap_lock);
  189. }
  190. /*
  191. * Must hold mmap_sem lock on at least one of the vmas containing
  192. * the page so that the anon_vma cannot vanish.
  193. */
  194. static void remove_anon_migration_ptes(struct page *old, struct page *new)
  195. {
  196. struct anon_vma *anon_vma;
  197. struct vm_area_struct *vma;
  198. unsigned long mapping;
  199. mapping = (unsigned long)new->mapping;
  200. if (!mapping || (mapping & PAGE_MAPPING_ANON) == 0)
  201. return;
  202. /*
  203. * We hold the mmap_sem lock. So no need to call page_lock_anon_vma.
  204. */
  205. anon_vma = (struct anon_vma *) (mapping - PAGE_MAPPING_ANON);
  206. spin_lock(&anon_vma->lock);
  207. list_for_each_entry(vma, &anon_vma->head, anon_vma_node)
  208. remove_migration_pte(vma, old, new);
  209. spin_unlock(&anon_vma->lock);
  210. }
  211. /*
  212. * Get rid of all migration entries and replace them by
  213. * references to the indicated page.
  214. */
  215. static void remove_migration_ptes(struct page *old, struct page *new)
  216. {
  217. if (PageAnon(new))
  218. remove_anon_migration_ptes(old, new);
  219. else
  220. remove_file_migration_ptes(old, new);
  221. }
  222. /*
  223. * Something used the pte of a page under migration. We need to
  224. * get to the page and wait until migration is finished.
  225. * When we return from this function the fault will be retried.
  226. *
  227. * This function is called from do_swap_page().
  228. */
  229. void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
  230. unsigned long address)
  231. {
  232. pte_t *ptep, pte;
  233. spinlock_t *ptl;
  234. swp_entry_t entry;
  235. struct page *page;
  236. ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
  237. pte = *ptep;
  238. if (!is_swap_pte(pte))
  239. goto out;
  240. entry = pte_to_swp_entry(pte);
  241. if (!is_migration_entry(entry))
  242. goto out;
  243. page = migration_entry_to_page(entry);
  244. get_page(page);
  245. pte_unmap_unlock(ptep, ptl);
  246. wait_on_page_locked(page);
  247. put_page(page);
  248. return;
  249. out:
  250. pte_unmap_unlock(ptep, ptl);
  251. }
  252. /*
  253. * Replace the page in the mapping.
  254. *
  255. * The number of remaining references must be:
  256. * 1 for anonymous pages without a mapping
  257. * 2 for pages with a mapping
  258. * 3 for pages with a mapping and PagePrivate set.
  259. */
  260. static int migrate_page_move_mapping(struct address_space *mapping,
  261. struct page *newpage, struct page *page)
  262. {
  263. void **pslot;
  264. if (!mapping) {
  265. /* Anonymous page without mapping */
  266. if (page_count(page) != 1)
  267. return -EAGAIN;
  268. return 0;
  269. }
  270. write_lock_irq(&mapping->tree_lock);
  271. pslot = radix_tree_lookup_slot(&mapping->page_tree,
  272. page_index(page));
  273. if (page_count(page) != 2 + !!PagePrivate(page) ||
  274. (struct page *)radix_tree_deref_slot(pslot) != page) {
  275. write_unlock_irq(&mapping->tree_lock);
  276. return -EAGAIN;
  277. }
  278. /*
  279. * Now we know that no one else is looking at the page.
  280. */
  281. get_page(newpage); /* add cache reference */
  282. #ifdef CONFIG_SWAP
  283. if (PageSwapCache(page)) {
  284. SetPageSwapCache(newpage);
  285. set_page_private(newpage, page_private(page));
  286. }
  287. #endif
  288. radix_tree_replace_slot(pslot, newpage);
  289. /*
  290. * Drop cache reference from old page.
  291. * We know this isn't the last reference.
  292. */
  293. __put_page(page);
  294. /*
  295. * If moved to a different zone then also account
  296. * the page for that zone. Other VM counters will be
  297. * taken care of when we establish references to the
  298. * new page and drop references to the old page.
  299. *
  300. * Note that anonymous pages are accounted for
  301. * via NR_FILE_PAGES and NR_ANON_PAGES if they
  302. * are mapped to swap space.
  303. */
  304. __dec_zone_page_state(page, NR_FILE_PAGES);
  305. __inc_zone_page_state(newpage, NR_FILE_PAGES);
  306. write_unlock_irq(&mapping->tree_lock);
  307. return 0;
  308. }
  309. /*
  310. * Copy the page to its new location
  311. */
  312. static void migrate_page_copy(struct page *newpage, struct page *page)
  313. {
  314. copy_highpage(newpage, page);
  315. if (PageError(page))
  316. SetPageError(newpage);
  317. if (PageReferenced(page))
  318. SetPageReferenced(newpage);
  319. if (PageUptodate(page))
  320. SetPageUptodate(newpage);
  321. if (PageActive(page))
  322. SetPageActive(newpage);
  323. if (PageChecked(page))
  324. SetPageChecked(newpage);
  325. if (PageMappedToDisk(page))
  326. SetPageMappedToDisk(newpage);
  327. if (PageDirty(page)) {
  328. clear_page_dirty_for_io(page);
  329. set_page_dirty(newpage);
  330. }
  331. #ifdef CONFIG_SWAP
  332. ClearPageSwapCache(page);
  333. #endif
  334. ClearPageActive(page);
  335. ClearPagePrivate(page);
  336. set_page_private(page, 0);
  337. page->mapping = NULL;
  338. /*
  339. * If any waiters have accumulated on the new page then
  340. * wake them up.
  341. */
  342. if (PageWriteback(newpage))
  343. end_page_writeback(newpage);
  344. }
  345. /************************************************************
  346. * Migration functions
  347. ***********************************************************/
  348. /* Always fail migration. Used for mappings that are not movable */
  349. int fail_migrate_page(struct address_space *mapping,
  350. struct page *newpage, struct page *page)
  351. {
  352. return -EIO;
  353. }
  354. EXPORT_SYMBOL(fail_migrate_page);
  355. /*
  356. * Common logic to directly migrate a single page suitable for
  357. * pages that do not use PagePrivate.
  358. *
  359. * Pages are locked upon entry and exit.
  360. */
  361. int migrate_page(struct address_space *mapping,
  362. struct page *newpage, struct page *page)
  363. {
  364. int rc;
  365. BUG_ON(PageWriteback(page)); /* Writeback must be complete */
  366. rc = migrate_page_move_mapping(mapping, newpage, page);
  367. if (rc)
  368. return rc;
  369. migrate_page_copy(newpage, page);
  370. return 0;
  371. }
  372. EXPORT_SYMBOL(migrate_page);
  373. #ifdef CONFIG_BLOCK
  374. /*
  375. * Migration function for pages with buffers. This function can only be used
  376. * if the underlying filesystem guarantees that no other references to "page"
  377. * exist.
  378. */
  379. int buffer_migrate_page(struct address_space *mapping,
  380. struct page *newpage, struct page *page)
  381. {
  382. struct buffer_head *bh, *head;
  383. int rc;
  384. if (!page_has_buffers(page))
  385. return migrate_page(mapping, newpage, page);
  386. head = page_buffers(page);
  387. rc = migrate_page_move_mapping(mapping, newpage, page);
  388. if (rc)
  389. return rc;
  390. bh = head;
  391. do {
  392. get_bh(bh);
  393. lock_buffer(bh);
  394. bh = bh->b_this_page;
  395. } while (bh != head);
  396. ClearPagePrivate(page);
  397. set_page_private(newpage, page_private(page));
  398. set_page_private(page, 0);
  399. put_page(page);
  400. get_page(newpage);
  401. bh = head;
  402. do {
  403. set_bh_page(bh, newpage, bh_offset(bh));
  404. bh = bh->b_this_page;
  405. } while (bh != head);
  406. SetPagePrivate(newpage);
  407. migrate_page_copy(newpage, page);
  408. bh = head;
  409. do {
  410. unlock_buffer(bh);
  411. put_bh(bh);
  412. bh = bh->b_this_page;
  413. } while (bh != head);
  414. return 0;
  415. }
  416. EXPORT_SYMBOL(buffer_migrate_page);
  417. #endif
  418. /*
  419. * Writeback a page to clean the dirty state
  420. */
  421. static int writeout(struct address_space *mapping, struct page *page)
  422. {
  423. struct writeback_control wbc = {
  424. .sync_mode = WB_SYNC_NONE,
  425. .nr_to_write = 1,
  426. .range_start = 0,
  427. .range_end = LLONG_MAX,
  428. .nonblocking = 1,
  429. .for_reclaim = 1
  430. };
  431. int rc;
  432. if (!mapping->a_ops->writepage)
  433. /* No write method for the address space */
  434. return -EINVAL;
  435. if (!clear_page_dirty_for_io(page))
  436. /* Someone else already triggered a write */
  437. return -EAGAIN;
  438. /*
  439. * A dirty page may imply that the underlying filesystem has
  440. * the page on some queue. So the page must be clean for
  441. * migration. Writeout may mean we loose the lock and the
  442. * page state is no longer what we checked for earlier.
  443. * At this point we know that the migration attempt cannot
  444. * be successful.
  445. */
  446. remove_migration_ptes(page, page);
  447. rc = mapping->a_ops->writepage(page, &wbc);
  448. if (rc < 0)
  449. /* I/O Error writing */
  450. return -EIO;
  451. if (rc != AOP_WRITEPAGE_ACTIVATE)
  452. /* unlocked. Relock */
  453. lock_page(page);
  454. return -EAGAIN;
  455. }
  456. /*
  457. * Default handling if a filesystem does not provide a migration function.
  458. */
  459. static int fallback_migrate_page(struct address_space *mapping,
  460. struct page *newpage, struct page *page)
  461. {
  462. if (PageDirty(page))
  463. return writeout(mapping, page);
  464. /*
  465. * Buffers may be managed in a filesystem specific way.
  466. * We must have no buffers or drop them.
  467. */
  468. if (PagePrivate(page) &&
  469. !try_to_release_page(page, GFP_KERNEL))
  470. return -EAGAIN;
  471. return migrate_page(mapping, newpage, page);
  472. }
  473. /*
  474. * Move a page to a newly allocated page
  475. * The page is locked and all ptes have been successfully removed.
  476. *
  477. * The new page will have replaced the old page if this function
  478. * is successful.
  479. */
  480. static int move_to_new_page(struct page *newpage, struct page *page)
  481. {
  482. struct address_space *mapping;
  483. int rc;
  484. /*
  485. * Block others from accessing the page when we get around to
  486. * establishing additional references. We are the only one
  487. * holding a reference to the new page at this point.
  488. */
  489. if (TestSetPageLocked(newpage))
  490. BUG();
  491. /* Prepare mapping for the new page.*/
  492. newpage->index = page->index;
  493. newpage->mapping = page->mapping;
  494. mapping = page_mapping(page);
  495. if (!mapping)
  496. rc = migrate_page(mapping, newpage, page);
  497. else if (mapping->a_ops->migratepage)
  498. /*
  499. * Most pages have a mapping and most filesystems
  500. * should provide a migration function. Anonymous
  501. * pages are part of swap space which also has its
  502. * own migration function. This is the most common
  503. * path for page migration.
  504. */
  505. rc = mapping->a_ops->migratepage(mapping,
  506. newpage, page);
  507. else
  508. rc = fallback_migrate_page(mapping, newpage, page);
  509. if (!rc) {
  510. mem_cgroup_page_migration(page, newpage);
  511. remove_migration_ptes(page, newpage);
  512. } else
  513. newpage->mapping = NULL;
  514. unlock_page(newpage);
  515. return rc;
  516. }
  517. /*
  518. * Obtain the lock on page, remove all ptes and migrate the page
  519. * to the newly allocated page in newpage.
  520. */
  521. static int unmap_and_move(new_page_t get_new_page, unsigned long private,
  522. struct page *page, int force)
  523. {
  524. int rc = 0;
  525. int *result = NULL;
  526. struct page *newpage = get_new_page(page, private, &result);
  527. int rcu_locked = 0;
  528. int charge = 0;
  529. if (!newpage)
  530. return -ENOMEM;
  531. if (page_count(page) == 1)
  532. /* page was freed from under us. So we are done. */
  533. goto move_newpage;
  534. rc = -EAGAIN;
  535. if (TestSetPageLocked(page)) {
  536. if (!force)
  537. goto move_newpage;
  538. lock_page(page);
  539. }
  540. if (PageWriteback(page)) {
  541. if (!force)
  542. goto unlock;
  543. wait_on_page_writeback(page);
  544. }
  545. /*
  546. * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
  547. * we cannot notice that anon_vma is freed while we migrates a page.
  548. * This rcu_read_lock() delays freeing anon_vma pointer until the end
  549. * of migration. File cache pages are no problem because of page_lock()
  550. * File Caches may use write_page() or lock_page() in migration, then,
  551. * just care Anon page here.
  552. */
  553. if (PageAnon(page)) {
  554. rcu_read_lock();
  555. rcu_locked = 1;
  556. }
  557. /*
  558. * Corner case handling:
  559. * 1. When a new swap-cache page is read into, it is added to the LRU
  560. * and treated as swapcache but it has no rmap yet.
  561. * Calling try_to_unmap() against a page->mapping==NULL page will
  562. * trigger a BUG. So handle it here.
  563. * 2. An orphaned page (see truncate_complete_page) might have
  564. * fs-private metadata. The page can be picked up due to memory
  565. * offlining. Everywhere else except page reclaim, the page is
  566. * invisible to the vm, so the page can not be migrated. So try to
  567. * free the metadata, so the page can be freed.
  568. */
  569. if (!page->mapping) {
  570. if (!PageAnon(page) && PagePrivate(page)) {
  571. /*
  572. * Go direct to try_to_free_buffers() here because
  573. * a) that's what try_to_release_page() would do anyway
  574. * b) we may be under rcu_read_lock() here, so we can't
  575. * use GFP_KERNEL which is what try_to_release_page()
  576. * needs to be effective.
  577. */
  578. try_to_free_buffers(page);
  579. }
  580. goto rcu_unlock;
  581. }
  582. charge = mem_cgroup_prepare_migration(page);
  583. /* Establish migration ptes or remove ptes */
  584. try_to_unmap(page, 1);
  585. if (!page_mapped(page))
  586. rc = move_to_new_page(newpage, page);
  587. if (rc) {
  588. remove_migration_ptes(page, page);
  589. if (charge)
  590. mem_cgroup_end_migration(page);
  591. } else if (charge)
  592. mem_cgroup_end_migration(newpage);
  593. rcu_unlock:
  594. if (rcu_locked)
  595. rcu_read_unlock();
  596. unlock:
  597. unlock_page(page);
  598. if (rc != -EAGAIN) {
  599. /*
  600. * A page that has been migrated has all references
  601. * removed and will be freed. A page that has not been
  602. * migrated will have kepts its references and be
  603. * restored.
  604. */
  605. list_del(&page->lru);
  606. move_to_lru(page);
  607. }
  608. move_newpage:
  609. /*
  610. * Move the new page to the LRU. If migration was not successful
  611. * then this will free the page.
  612. */
  613. move_to_lru(newpage);
  614. if (result) {
  615. if (rc)
  616. *result = rc;
  617. else
  618. *result = page_to_nid(newpage);
  619. }
  620. return rc;
  621. }
  622. /*
  623. * migrate_pages
  624. *
  625. * The function takes one list of pages to migrate and a function
  626. * that determines from the page to be migrated and the private data
  627. * the target of the move and allocates the page.
  628. *
  629. * The function returns after 10 attempts or if no pages
  630. * are movable anymore because to has become empty
  631. * or no retryable pages exist anymore. All pages will be
  632. * returned to the LRU or freed.
  633. *
  634. * Return: Number of pages not migrated or error code.
  635. */
  636. int migrate_pages(struct list_head *from,
  637. new_page_t get_new_page, unsigned long private)
  638. {
  639. int retry = 1;
  640. int nr_failed = 0;
  641. int pass = 0;
  642. struct page *page;
  643. struct page *page2;
  644. int swapwrite = current->flags & PF_SWAPWRITE;
  645. int rc;
  646. if (!swapwrite)
  647. current->flags |= PF_SWAPWRITE;
  648. for(pass = 0; pass < 10 && retry; pass++) {
  649. retry = 0;
  650. list_for_each_entry_safe(page, page2, from, lru) {
  651. cond_resched();
  652. rc = unmap_and_move(get_new_page, private,
  653. page, pass > 2);
  654. switch(rc) {
  655. case -ENOMEM:
  656. goto out;
  657. case -EAGAIN:
  658. retry++;
  659. break;
  660. case 0:
  661. break;
  662. default:
  663. /* Permanent failure */
  664. nr_failed++;
  665. break;
  666. }
  667. }
  668. }
  669. rc = 0;
  670. out:
  671. if (!swapwrite)
  672. current->flags &= ~PF_SWAPWRITE;
  673. putback_lru_pages(from);
  674. if (rc)
  675. return rc;
  676. return nr_failed + retry;
  677. }
  678. #ifdef CONFIG_NUMA
  679. /*
  680. * Move a list of individual pages
  681. */
  682. struct page_to_node {
  683. unsigned long addr;
  684. struct page *page;
  685. int node;
  686. int status;
  687. };
  688. static struct page *new_page_node(struct page *p, unsigned long private,
  689. int **result)
  690. {
  691. struct page_to_node *pm = (struct page_to_node *)private;
  692. while (pm->node != MAX_NUMNODES && pm->page != p)
  693. pm++;
  694. if (pm->node == MAX_NUMNODES)
  695. return NULL;
  696. *result = &pm->status;
  697. return alloc_pages_node(pm->node,
  698. GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
  699. }
  700. /*
  701. * Move a set of pages as indicated in the pm array. The addr
  702. * field must be set to the virtual address of the page to be moved
  703. * and the node number must contain a valid target node.
  704. */
  705. static int do_move_pages(struct mm_struct *mm, struct page_to_node *pm,
  706. int migrate_all)
  707. {
  708. int err;
  709. struct page_to_node *pp;
  710. LIST_HEAD(pagelist);
  711. down_read(&mm->mmap_sem);
  712. /*
  713. * Build a list of pages to migrate
  714. */
  715. migrate_prep();
  716. for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
  717. struct vm_area_struct *vma;
  718. struct page *page;
  719. /*
  720. * A valid page pointer that will not match any of the
  721. * pages that will be moved.
  722. */
  723. pp->page = ZERO_PAGE(0);
  724. err = -EFAULT;
  725. vma = find_vma(mm, pp->addr);
  726. if (!vma || !vma_migratable(vma))
  727. goto set_status;
  728. page = follow_page(vma, pp->addr, FOLL_GET);
  729. err = -ENOENT;
  730. if (!page)
  731. goto set_status;
  732. if (PageReserved(page)) /* Check for zero page */
  733. goto put_and_set;
  734. pp->page = page;
  735. err = page_to_nid(page);
  736. if (err == pp->node)
  737. /*
  738. * Node already in the right place
  739. */
  740. goto put_and_set;
  741. err = -EACCES;
  742. if (page_mapcount(page) > 1 &&
  743. !migrate_all)
  744. goto put_and_set;
  745. err = isolate_lru_page(page, &pagelist);
  746. put_and_set:
  747. /*
  748. * Either remove the duplicate refcount from
  749. * isolate_lru_page() or drop the page ref if it was
  750. * not isolated.
  751. */
  752. put_page(page);
  753. set_status:
  754. pp->status = err;
  755. }
  756. if (!list_empty(&pagelist))
  757. err = migrate_pages(&pagelist, new_page_node,
  758. (unsigned long)pm);
  759. else
  760. err = -ENOENT;
  761. up_read(&mm->mmap_sem);
  762. return err;
  763. }
  764. /*
  765. * Determine the nodes of a list of pages. The addr in the pm array
  766. * must have been set to the virtual address of which we want to determine
  767. * the node number.
  768. */
  769. static int do_pages_stat(struct mm_struct *mm, struct page_to_node *pm)
  770. {
  771. down_read(&mm->mmap_sem);
  772. for ( ; pm->node != MAX_NUMNODES; pm++) {
  773. struct vm_area_struct *vma;
  774. struct page *page;
  775. int err;
  776. err = -EFAULT;
  777. vma = find_vma(mm, pm->addr);
  778. if (!vma)
  779. goto set_status;
  780. page = follow_page(vma, pm->addr, 0);
  781. err = -ENOENT;
  782. /* Use PageReserved to check for zero page */
  783. if (!page || PageReserved(page))
  784. goto set_status;
  785. err = page_to_nid(page);
  786. set_status:
  787. pm->status = err;
  788. }
  789. up_read(&mm->mmap_sem);
  790. return 0;
  791. }
  792. /*
  793. * Move a list of pages in the address space of the currently executing
  794. * process.
  795. */
  796. asmlinkage long sys_move_pages(pid_t pid, unsigned long nr_pages,
  797. const void __user * __user *pages,
  798. const int __user *nodes,
  799. int __user *status, int flags)
  800. {
  801. int err = 0;
  802. int i;
  803. struct task_struct *task;
  804. nodemask_t task_nodes;
  805. struct mm_struct *mm;
  806. struct page_to_node *pm = NULL;
  807. /* Check flags */
  808. if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
  809. return -EINVAL;
  810. if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
  811. return -EPERM;
  812. /* Find the mm_struct */
  813. read_lock(&tasklist_lock);
  814. task = pid ? find_task_by_vpid(pid) : current;
  815. if (!task) {
  816. read_unlock(&tasklist_lock);
  817. return -ESRCH;
  818. }
  819. mm = get_task_mm(task);
  820. read_unlock(&tasklist_lock);
  821. if (!mm)
  822. return -EINVAL;
  823. /*
  824. * Check if this process has the right to modify the specified
  825. * process. The right exists if the process has administrative
  826. * capabilities, superuser privileges or the same
  827. * userid as the target process.
  828. */
  829. if ((current->euid != task->suid) && (current->euid != task->uid) &&
  830. (current->uid != task->suid) && (current->uid != task->uid) &&
  831. !capable(CAP_SYS_NICE)) {
  832. err = -EPERM;
  833. goto out2;
  834. }
  835. err = security_task_movememory(task);
  836. if (err)
  837. goto out2;
  838. task_nodes = cpuset_mems_allowed(task);
  839. /* Limit nr_pages so that the multiplication may not overflow */
  840. if (nr_pages >= ULONG_MAX / sizeof(struct page_to_node) - 1) {
  841. err = -E2BIG;
  842. goto out2;
  843. }
  844. pm = vmalloc((nr_pages + 1) * sizeof(struct page_to_node));
  845. if (!pm) {
  846. err = -ENOMEM;
  847. goto out2;
  848. }
  849. /*
  850. * Get parameters from user space and initialize the pm
  851. * array. Return various errors if the user did something wrong.
  852. */
  853. for (i = 0; i < nr_pages; i++) {
  854. const void __user *p;
  855. err = -EFAULT;
  856. if (get_user(p, pages + i))
  857. goto out;
  858. pm[i].addr = (unsigned long)p;
  859. if (nodes) {
  860. int node;
  861. if (get_user(node, nodes + i))
  862. goto out;
  863. err = -ENODEV;
  864. if (!node_state(node, N_HIGH_MEMORY))
  865. goto out;
  866. err = -EACCES;
  867. if (!node_isset(node, task_nodes))
  868. goto out;
  869. pm[i].node = node;
  870. } else
  871. pm[i].node = 0; /* anything to not match MAX_NUMNODES */
  872. }
  873. /* End marker */
  874. pm[nr_pages].node = MAX_NUMNODES;
  875. if (nodes)
  876. err = do_move_pages(mm, pm, flags & MPOL_MF_MOVE_ALL);
  877. else
  878. err = do_pages_stat(mm, pm);
  879. if (err >= 0)
  880. /* Return status information */
  881. for (i = 0; i < nr_pages; i++)
  882. if (put_user(pm[i].status, status + i))
  883. err = -EFAULT;
  884. out:
  885. vfree(pm);
  886. out2:
  887. mmput(mm);
  888. return err;
  889. }
  890. #endif
  891. /*
  892. * Call migration functions in the vma_ops that may prepare
  893. * memory in a vm for migration. migration functions may perform
  894. * the migration for vmas that do not have an underlying page struct.
  895. */
  896. int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
  897. const nodemask_t *from, unsigned long flags)
  898. {
  899. struct vm_area_struct *vma;
  900. int err = 0;
  901. for(vma = mm->mmap; vma->vm_next && !err; vma = vma->vm_next) {
  902. if (vma->vm_ops && vma->vm_ops->migrate) {
  903. err = vma->vm_ops->migrate(vma, to, from, flags);
  904. if (err)
  905. break;
  906. }
  907. }
  908. return err;
  909. }