migrate.c 25 KB

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