migrate.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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/ksm.h>
  24. #include <linux/rmap.h>
  25. #include <linux/topology.h>
  26. #include <linux/cpu.h>
  27. #include <linux/cpuset.h>
  28. #include <linux/writeback.h>
  29. #include <linux/mempolicy.h>
  30. #include <linux/vmalloc.h>
  31. #include <linux/security.h>
  32. #include <linux/memcontrol.h>
  33. #include <linux/syscalls.h>
  34. #include <linux/gfp.h>
  35. #include "internal.h"
  36. #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
  37. /*
  38. * migrate_prep() needs to be called before we start compiling a list of pages
  39. * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is
  40. * undesirable, use migrate_prep_local()
  41. */
  42. int migrate_prep(void)
  43. {
  44. /*
  45. * Clear the LRU lists so pages can be isolated.
  46. * Note that pages may be moved off the LRU after we have
  47. * drained them. Those pages will fail to migrate like other
  48. * pages that may be busy.
  49. */
  50. lru_add_drain_all();
  51. return 0;
  52. }
  53. /* Do the necessary work of migrate_prep but not if it involves other CPUs */
  54. int migrate_prep_local(void)
  55. {
  56. lru_add_drain();
  57. return 0;
  58. }
  59. /*
  60. * Add isolated pages on the list back to the LRU under page lock
  61. * to avoid leaking evictable pages back onto unevictable list.
  62. */
  63. void putback_lru_pages(struct list_head *l)
  64. {
  65. struct page *page;
  66. struct page *page2;
  67. list_for_each_entry_safe(page, page2, l, lru) {
  68. list_del(&page->lru);
  69. dec_zone_page_state(page, NR_ISOLATED_ANON +
  70. page_is_file_cache(page));
  71. putback_lru_page(page);
  72. }
  73. }
  74. /*
  75. * Restore a potential migration pte to a working pte entry
  76. */
  77. static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,
  78. unsigned long addr, void *old)
  79. {
  80. struct mm_struct *mm = vma->vm_mm;
  81. swp_entry_t entry;
  82. pgd_t *pgd;
  83. pud_t *pud;
  84. pmd_t *pmd;
  85. pte_t *ptep, pte;
  86. spinlock_t *ptl;
  87. pgd = pgd_offset(mm, addr);
  88. if (!pgd_present(*pgd))
  89. goto out;
  90. pud = pud_offset(pgd, addr);
  91. if (!pud_present(*pud))
  92. goto out;
  93. pmd = pmd_offset(pud, addr);
  94. if (!pmd_present(*pmd))
  95. goto out;
  96. ptep = pte_offset_map(pmd, addr);
  97. if (!is_swap_pte(*ptep)) {
  98. pte_unmap(ptep);
  99. goto out;
  100. }
  101. ptl = pte_lockptr(mm, pmd);
  102. spin_lock(ptl);
  103. pte = *ptep;
  104. if (!is_swap_pte(pte))
  105. goto unlock;
  106. entry = pte_to_swp_entry(pte);
  107. if (!is_migration_entry(entry) ||
  108. migration_entry_to_page(entry) != old)
  109. goto unlock;
  110. get_page(new);
  111. pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
  112. if (is_write_migration_entry(entry))
  113. pte = pte_mkwrite(pte);
  114. flush_cache_page(vma, addr, pte_pfn(pte));
  115. set_pte_at(mm, addr, ptep, pte);
  116. if (PageAnon(new))
  117. page_add_anon_rmap(new, vma, addr);
  118. else
  119. page_add_file_rmap(new);
  120. /* No need to invalidate - it was non-present before */
  121. update_mmu_cache(vma, addr, ptep);
  122. unlock:
  123. pte_unmap_unlock(ptep, ptl);
  124. out:
  125. return SWAP_AGAIN;
  126. }
  127. /*
  128. * Get rid of all migration entries and replace them by
  129. * references to the indicated page.
  130. */
  131. static void remove_migration_ptes(struct page *old, struct page *new)
  132. {
  133. rmap_walk(new, remove_migration_pte, old);
  134. }
  135. /*
  136. * Something used the pte of a page under migration. We need to
  137. * get to the page and wait until migration is finished.
  138. * When we return from this function the fault will be retried.
  139. *
  140. * This function is called from do_swap_page().
  141. */
  142. void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
  143. unsigned long address)
  144. {
  145. pte_t *ptep, pte;
  146. spinlock_t *ptl;
  147. swp_entry_t entry;
  148. struct page *page;
  149. ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
  150. pte = *ptep;
  151. if (!is_swap_pte(pte))
  152. goto out;
  153. entry = pte_to_swp_entry(pte);
  154. if (!is_migration_entry(entry))
  155. goto out;
  156. page = migration_entry_to_page(entry);
  157. /*
  158. * Once radix-tree replacement of page migration started, page_count
  159. * *must* be zero. And, we don't want to call wait_on_page_locked()
  160. * against a page without get_page().
  161. * So, we use get_page_unless_zero(), here. Even failed, page fault
  162. * will occur again.
  163. */
  164. if (!get_page_unless_zero(page))
  165. goto out;
  166. pte_unmap_unlock(ptep, ptl);
  167. wait_on_page_locked(page);
  168. put_page(page);
  169. return;
  170. out:
  171. pte_unmap_unlock(ptep, ptl);
  172. }
  173. /*
  174. * Replace the page in the mapping.
  175. *
  176. * The number of remaining references must be:
  177. * 1 for anonymous pages without a mapping
  178. * 2 for pages with a mapping
  179. * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
  180. */
  181. static int migrate_page_move_mapping(struct address_space *mapping,
  182. struct page *newpage, struct page *page)
  183. {
  184. int expected_count;
  185. void **pslot;
  186. if (!mapping) {
  187. /* Anonymous page without mapping */
  188. if (page_count(page) != 1)
  189. return -EAGAIN;
  190. return 0;
  191. }
  192. spin_lock_irq(&mapping->tree_lock);
  193. pslot = radix_tree_lookup_slot(&mapping->page_tree,
  194. page_index(page));
  195. expected_count = 2 + page_has_private(page);
  196. if (page_count(page) != expected_count ||
  197. (struct page *)radix_tree_deref_slot(pslot) != page) {
  198. spin_unlock_irq(&mapping->tree_lock);
  199. return -EAGAIN;
  200. }
  201. if (!page_freeze_refs(page, expected_count)) {
  202. spin_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. get_page(newpage); /* add cache reference */
  209. if (PageSwapCache(page)) {
  210. SetPageSwapCache(newpage);
  211. set_page_private(newpage, page_private(page));
  212. }
  213. radix_tree_replace_slot(pslot, newpage);
  214. page_unfreeze_refs(page, expected_count);
  215. /*
  216. * Drop cache reference from old page.
  217. * We know this isn't the last reference.
  218. */
  219. __put_page(page);
  220. /*
  221. * If moved to a different zone then also account
  222. * the page for that zone. Other VM counters will be
  223. * taken care of when we establish references to the
  224. * new page and drop references to the old page.
  225. *
  226. * Note that anonymous pages are accounted for
  227. * via NR_FILE_PAGES and NR_ANON_PAGES if they
  228. * are mapped to swap space.
  229. */
  230. __dec_zone_page_state(page, NR_FILE_PAGES);
  231. __inc_zone_page_state(newpage, NR_FILE_PAGES);
  232. if (PageSwapBacked(page)) {
  233. __dec_zone_page_state(page, NR_SHMEM);
  234. __inc_zone_page_state(newpage, NR_SHMEM);
  235. }
  236. spin_unlock_irq(&mapping->tree_lock);
  237. return 0;
  238. }
  239. /*
  240. * Copy the page to its new location
  241. */
  242. static void migrate_page_copy(struct page *newpage, struct page *page)
  243. {
  244. copy_highpage(newpage, page);
  245. if (PageError(page))
  246. SetPageError(newpage);
  247. if (PageReferenced(page))
  248. SetPageReferenced(newpage);
  249. if (PageUptodate(page))
  250. SetPageUptodate(newpage);
  251. if (TestClearPageActive(page)) {
  252. VM_BUG_ON(PageUnevictable(page));
  253. SetPageActive(newpage);
  254. } else if (TestClearPageUnevictable(page))
  255. SetPageUnevictable(newpage);
  256. if (PageChecked(page))
  257. SetPageChecked(newpage);
  258. if (PageMappedToDisk(page))
  259. SetPageMappedToDisk(newpage);
  260. if (PageDirty(page)) {
  261. clear_page_dirty_for_io(page);
  262. /*
  263. * Want to mark the page and the radix tree as dirty, and
  264. * redo the accounting that clear_page_dirty_for_io undid,
  265. * but we can't use set_page_dirty because that function
  266. * is actually a signal that all of the page has become dirty.
  267. * Wheras only part of our page may be dirty.
  268. */
  269. __set_page_dirty_nobuffers(newpage);
  270. }
  271. mlock_migrate_page(newpage, page);
  272. ksm_migrate_page(newpage, page);
  273. ClearPageSwapCache(page);
  274. ClearPagePrivate(page);
  275. set_page_private(page, 0);
  276. page->mapping = NULL;
  277. /*
  278. * If any waiters have accumulated on the new page then
  279. * wake them up.
  280. */
  281. if (PageWriteback(newpage))
  282. end_page_writeback(newpage);
  283. }
  284. /************************************************************
  285. * Migration functions
  286. ***********************************************************/
  287. /* Always fail migration. Used for mappings that are not movable */
  288. int fail_migrate_page(struct address_space *mapping,
  289. struct page *newpage, struct page *page)
  290. {
  291. return -EIO;
  292. }
  293. EXPORT_SYMBOL(fail_migrate_page);
  294. /*
  295. * Common logic to directly migrate a single page suitable for
  296. * pages that do not use PagePrivate/PagePrivate2.
  297. *
  298. * Pages are locked upon entry and exit.
  299. */
  300. int migrate_page(struct address_space *mapping,
  301. struct page *newpage, struct page *page)
  302. {
  303. int rc;
  304. BUG_ON(PageWriteback(page)); /* Writeback must be complete */
  305. rc = migrate_page_move_mapping(mapping, newpage, page);
  306. if (rc)
  307. return rc;
  308. migrate_page_copy(newpage, page);
  309. return 0;
  310. }
  311. EXPORT_SYMBOL(migrate_page);
  312. #ifdef CONFIG_BLOCK
  313. /*
  314. * Migration function for pages with buffers. This function can only be used
  315. * if the underlying filesystem guarantees that no other references to "page"
  316. * exist.
  317. */
  318. int buffer_migrate_page(struct address_space *mapping,
  319. struct page *newpage, struct page *page)
  320. {
  321. struct buffer_head *bh, *head;
  322. int rc;
  323. if (!page_has_buffers(page))
  324. return migrate_page(mapping, newpage, page);
  325. head = page_buffers(page);
  326. rc = migrate_page_move_mapping(mapping, newpage, page);
  327. if (rc)
  328. return rc;
  329. bh = head;
  330. do {
  331. get_bh(bh);
  332. lock_buffer(bh);
  333. bh = bh->b_this_page;
  334. } while (bh != head);
  335. ClearPagePrivate(page);
  336. set_page_private(newpage, page_private(page));
  337. set_page_private(page, 0);
  338. put_page(page);
  339. get_page(newpage);
  340. bh = head;
  341. do {
  342. set_bh_page(bh, newpage, bh_offset(bh));
  343. bh = bh->b_this_page;
  344. } while (bh != head);
  345. SetPagePrivate(newpage);
  346. migrate_page_copy(newpage, page);
  347. bh = head;
  348. do {
  349. unlock_buffer(bh);
  350. put_bh(bh);
  351. bh = bh->b_this_page;
  352. } while (bh != head);
  353. return 0;
  354. }
  355. EXPORT_SYMBOL(buffer_migrate_page);
  356. #endif
  357. /*
  358. * Writeback a page to clean the dirty state
  359. */
  360. static int writeout(struct address_space *mapping, struct page *page)
  361. {
  362. struct writeback_control wbc = {
  363. .sync_mode = WB_SYNC_NONE,
  364. .nr_to_write = 1,
  365. .range_start = 0,
  366. .range_end = LLONG_MAX,
  367. .nonblocking = 1,
  368. .for_reclaim = 1
  369. };
  370. int rc;
  371. if (!mapping->a_ops->writepage)
  372. /* No write method for the address space */
  373. return -EINVAL;
  374. if (!clear_page_dirty_for_io(page))
  375. /* Someone else already triggered a write */
  376. return -EAGAIN;
  377. /*
  378. * A dirty page may imply that the underlying filesystem has
  379. * the page on some queue. So the page must be clean for
  380. * migration. Writeout may mean we loose the lock and the
  381. * page state is no longer what we checked for earlier.
  382. * At this point we know that the migration attempt cannot
  383. * be successful.
  384. */
  385. remove_migration_ptes(page, page);
  386. rc = mapping->a_ops->writepage(page, &wbc);
  387. if (rc != AOP_WRITEPAGE_ACTIVATE)
  388. /* unlocked. Relock */
  389. lock_page(page);
  390. return (rc < 0) ? -EIO : -EAGAIN;
  391. }
  392. /*
  393. * Default handling if a filesystem does not provide a migration function.
  394. */
  395. static int fallback_migrate_page(struct address_space *mapping,
  396. struct page *newpage, struct page *page)
  397. {
  398. if (PageDirty(page))
  399. return writeout(mapping, page);
  400. /*
  401. * Buffers may be managed in a filesystem specific way.
  402. * We must have no buffers or drop them.
  403. */
  404. if (page_has_private(page) &&
  405. !try_to_release_page(page, GFP_KERNEL))
  406. return -EAGAIN;
  407. return migrate_page(mapping, newpage, page);
  408. }
  409. /*
  410. * Move a page to a newly allocated page
  411. * The page is locked and all ptes have been successfully removed.
  412. *
  413. * The new page will have replaced the old page if this function
  414. * is successful.
  415. *
  416. * Return value:
  417. * < 0 - error code
  418. * == 0 - success
  419. */
  420. static int move_to_new_page(struct page *newpage, struct page *page,
  421. int remap_swapcache)
  422. {
  423. struct address_space *mapping;
  424. int rc;
  425. /*
  426. * Block others from accessing the page when we get around to
  427. * establishing additional references. We are the only one
  428. * holding a reference to the new page at this point.
  429. */
  430. if (!trylock_page(newpage))
  431. BUG();
  432. /* Prepare mapping for the new page.*/
  433. newpage->index = page->index;
  434. newpage->mapping = page->mapping;
  435. if (PageSwapBacked(page))
  436. SetPageSwapBacked(newpage);
  437. mapping = page_mapping(page);
  438. if (!mapping)
  439. rc = migrate_page(mapping, newpage, page);
  440. else if (mapping->a_ops->migratepage)
  441. /*
  442. * Most pages have a mapping and most filesystems
  443. * should provide a migration function. Anonymous
  444. * pages are part of swap space which also has its
  445. * own migration function. This is the most common
  446. * path for page migration.
  447. */
  448. rc = mapping->a_ops->migratepage(mapping,
  449. newpage, page);
  450. else
  451. rc = fallback_migrate_page(mapping, newpage, page);
  452. if (rc) {
  453. newpage->mapping = NULL;
  454. } else {
  455. if (remap_swapcache)
  456. remove_migration_ptes(page, newpage);
  457. }
  458. unlock_page(newpage);
  459. return rc;
  460. }
  461. /*
  462. * Obtain the lock on page, remove all ptes and migrate the page
  463. * to the newly allocated page in newpage.
  464. */
  465. static int unmap_and_move(new_page_t get_new_page, unsigned long private,
  466. struct page *page, int force, int offlining)
  467. {
  468. int rc = 0;
  469. int *result = NULL;
  470. struct page *newpage = get_new_page(page, private, &result);
  471. int remap_swapcache = 1;
  472. int rcu_locked = 0;
  473. int charge = 0;
  474. struct mem_cgroup *mem = NULL;
  475. struct anon_vma *anon_vma = NULL;
  476. if (!newpage)
  477. return -ENOMEM;
  478. if (page_count(page) == 1) {
  479. /* page was freed from under us. So we are done. */
  480. goto move_newpage;
  481. }
  482. /* prepare cgroup just returns 0 or -ENOMEM */
  483. rc = -EAGAIN;
  484. if (!trylock_page(page)) {
  485. if (!force)
  486. goto move_newpage;
  487. lock_page(page);
  488. }
  489. /*
  490. * Only memory hotplug's offline_pages() caller has locked out KSM,
  491. * and can safely migrate a KSM page. The other cases have skipped
  492. * PageKsm along with PageReserved - but it is only now when we have
  493. * the page lock that we can be certain it will not go KSM beneath us
  494. * (KSM will not upgrade a page from PageAnon to PageKsm when it sees
  495. * its pagecount raised, but only here do we take the page lock which
  496. * serializes that).
  497. */
  498. if (PageKsm(page) && !offlining) {
  499. rc = -EBUSY;
  500. goto unlock;
  501. }
  502. /* charge against new page */
  503. charge = mem_cgroup_prepare_migration(page, newpage, &mem);
  504. if (charge == -ENOMEM) {
  505. rc = -ENOMEM;
  506. goto unlock;
  507. }
  508. BUG_ON(charge);
  509. if (PageWriteback(page)) {
  510. if (!force)
  511. goto uncharge;
  512. wait_on_page_writeback(page);
  513. }
  514. /*
  515. * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
  516. * we cannot notice that anon_vma is freed while we migrates a page.
  517. * This rcu_read_lock() delays freeing anon_vma pointer until the end
  518. * of migration. File cache pages are no problem because of page_lock()
  519. * File Caches may use write_page() or lock_page() in migration, then,
  520. * just care Anon page here.
  521. */
  522. if (PageAnon(page)) {
  523. rcu_read_lock();
  524. rcu_locked = 1;
  525. /* Determine how to safely use anon_vma */
  526. if (!page_mapped(page)) {
  527. if (!PageSwapCache(page))
  528. goto rcu_unlock;
  529. /*
  530. * We cannot be sure that the anon_vma of an unmapped
  531. * swapcache page is safe to use because we don't
  532. * know in advance if the VMA that this page belonged
  533. * to still exists. If the VMA and others sharing the
  534. * data have been freed, then the anon_vma could
  535. * already be invalid.
  536. *
  537. * To avoid this possibility, swapcache pages get
  538. * migrated but are not remapped when migration
  539. * completes
  540. */
  541. remap_swapcache = 0;
  542. } else {
  543. /*
  544. * Take a reference count on the anon_vma if the
  545. * page is mapped so that it is guaranteed to
  546. * exist when the page is remapped later
  547. */
  548. anon_vma = page_anon_vma(page);
  549. get_anon_vma(anon_vma);
  550. }
  551. }
  552. /*
  553. * Corner case handling:
  554. * 1. When a new swap-cache page is read into, it is added to the LRU
  555. * and treated as swapcache but it has no rmap yet.
  556. * Calling try_to_unmap() against a page->mapping==NULL page will
  557. * trigger a BUG. So handle it here.
  558. * 2. An orphaned page (see truncate_complete_page) might have
  559. * fs-private metadata. The page can be picked up due to memory
  560. * offlining. Everywhere else except page reclaim, the page is
  561. * invisible to the vm, so the page can not be migrated. So try to
  562. * free the metadata, so the page can be freed.
  563. */
  564. if (!page->mapping) {
  565. if (!PageAnon(page) && page_has_private(page)) {
  566. /*
  567. * Go direct to try_to_free_buffers() here because
  568. * a) that's what try_to_release_page() would do anyway
  569. * b) we may be under rcu_read_lock() here, so we can't
  570. * use GFP_KERNEL which is what try_to_release_page()
  571. * needs to be effective.
  572. */
  573. try_to_free_buffers(page);
  574. goto rcu_unlock;
  575. }
  576. goto skip_unmap;
  577. }
  578. /* Establish migration ptes or remove ptes */
  579. try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
  580. skip_unmap:
  581. if (!page_mapped(page))
  582. rc = move_to_new_page(newpage, page, remap_swapcache);
  583. if (rc && remap_swapcache)
  584. remove_migration_ptes(page, page);
  585. rcu_unlock:
  586. /* Drop an anon_vma reference if we took one */
  587. if (anon_vma)
  588. drop_anon_vma(anon_vma);
  589. if (rcu_locked)
  590. rcu_read_unlock();
  591. uncharge:
  592. if (!charge)
  593. mem_cgroup_end_migration(mem, page, newpage);
  594. unlock:
  595. unlock_page(page);
  596. if (rc != -EAGAIN) {
  597. /*
  598. * A page that has been migrated has all references
  599. * removed and will be freed. A page that has not been
  600. * migrated will have kepts its references and be
  601. * restored.
  602. */
  603. list_del(&page->lru);
  604. dec_zone_page_state(page, NR_ISOLATED_ANON +
  605. page_is_file_cache(page));
  606. putback_lru_page(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. putback_lru_page(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, int offlining)
  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, offlining);
  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_exact_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. * The pm array ends with node = MAX_NUMNODES.
  705. */
  706. static int do_move_page_to_node_array(struct mm_struct *mm,
  707. struct page_to_node *pm,
  708. int migrate_all)
  709. {
  710. int err;
  711. struct page_to_node *pp;
  712. LIST_HEAD(pagelist);
  713. down_read(&mm->mmap_sem);
  714. /*
  715. * Build a list of pages to migrate
  716. */
  717. for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
  718. struct vm_area_struct *vma;
  719. struct page *page;
  720. err = -EFAULT;
  721. vma = find_vma(mm, pp->addr);
  722. if (!vma || !vma_migratable(vma))
  723. goto set_status;
  724. page = follow_page(vma, pp->addr, FOLL_GET);
  725. err = PTR_ERR(page);
  726. if (IS_ERR(page))
  727. goto set_status;
  728. err = -ENOENT;
  729. if (!page)
  730. goto set_status;
  731. /* Use PageReserved to check for zero page */
  732. if (PageReserved(page) || PageKsm(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);
  746. if (!err) {
  747. list_add_tail(&page->lru, &pagelist);
  748. inc_zone_page_state(page, NR_ISOLATED_ANON +
  749. page_is_file_cache(page));
  750. }
  751. put_and_set:
  752. /*
  753. * Either remove the duplicate refcount from
  754. * isolate_lru_page() or drop the page ref if it was
  755. * not isolated.
  756. */
  757. put_page(page);
  758. set_status:
  759. pp->status = err;
  760. }
  761. err = 0;
  762. if (!list_empty(&pagelist))
  763. err = migrate_pages(&pagelist, new_page_node,
  764. (unsigned long)pm, 0);
  765. up_read(&mm->mmap_sem);
  766. return err;
  767. }
  768. /*
  769. * Migrate an array of page address onto an array of nodes and fill
  770. * the corresponding array of status.
  771. */
  772. static int do_pages_move(struct mm_struct *mm, struct task_struct *task,
  773. unsigned long nr_pages,
  774. const void __user * __user *pages,
  775. const int __user *nodes,
  776. int __user *status, int flags)
  777. {
  778. struct page_to_node *pm;
  779. nodemask_t task_nodes;
  780. unsigned long chunk_nr_pages;
  781. unsigned long chunk_start;
  782. int err;
  783. task_nodes = cpuset_mems_allowed(task);
  784. err = -ENOMEM;
  785. pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
  786. if (!pm)
  787. goto out;
  788. migrate_prep();
  789. /*
  790. * Store a chunk of page_to_node array in a page,
  791. * but keep the last one as a marker
  792. */
  793. chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
  794. for (chunk_start = 0;
  795. chunk_start < nr_pages;
  796. chunk_start += chunk_nr_pages) {
  797. int j;
  798. if (chunk_start + chunk_nr_pages > nr_pages)
  799. chunk_nr_pages = nr_pages - chunk_start;
  800. /* fill the chunk pm with addrs and nodes from user-space */
  801. for (j = 0; j < chunk_nr_pages; j++) {
  802. const void __user *p;
  803. int node;
  804. err = -EFAULT;
  805. if (get_user(p, pages + j + chunk_start))
  806. goto out_pm;
  807. pm[j].addr = (unsigned long) p;
  808. if (get_user(node, nodes + j + chunk_start))
  809. goto out_pm;
  810. err = -ENODEV;
  811. if (node < 0 || node >= MAX_NUMNODES)
  812. goto out_pm;
  813. if (!node_state(node, N_HIGH_MEMORY))
  814. goto out_pm;
  815. err = -EACCES;
  816. if (!node_isset(node, task_nodes))
  817. goto out_pm;
  818. pm[j].node = node;
  819. }
  820. /* End marker for this chunk */
  821. pm[chunk_nr_pages].node = MAX_NUMNODES;
  822. /* Migrate this chunk */
  823. err = do_move_page_to_node_array(mm, pm,
  824. flags & MPOL_MF_MOVE_ALL);
  825. if (err < 0)
  826. goto out_pm;
  827. /* Return status information */
  828. for (j = 0; j < chunk_nr_pages; j++)
  829. if (put_user(pm[j].status, status + j + chunk_start)) {
  830. err = -EFAULT;
  831. goto out_pm;
  832. }
  833. }
  834. err = 0;
  835. out_pm:
  836. free_page((unsigned long)pm);
  837. out:
  838. return err;
  839. }
  840. /*
  841. * Determine the nodes of an array of pages and store it in an array of status.
  842. */
  843. static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
  844. const void __user **pages, int *status)
  845. {
  846. unsigned long i;
  847. down_read(&mm->mmap_sem);
  848. for (i = 0; i < nr_pages; i++) {
  849. unsigned long addr = (unsigned long)(*pages);
  850. struct vm_area_struct *vma;
  851. struct page *page;
  852. int err = -EFAULT;
  853. vma = find_vma(mm, addr);
  854. if (!vma)
  855. goto set_status;
  856. page = follow_page(vma, addr, 0);
  857. err = PTR_ERR(page);
  858. if (IS_ERR(page))
  859. goto set_status;
  860. err = -ENOENT;
  861. /* Use PageReserved to check for zero page */
  862. if (!page || PageReserved(page) || PageKsm(page))
  863. goto set_status;
  864. err = page_to_nid(page);
  865. set_status:
  866. *status = err;
  867. pages++;
  868. status++;
  869. }
  870. up_read(&mm->mmap_sem);
  871. }
  872. /*
  873. * Determine the nodes of a user array of pages and store it in
  874. * a user array of status.
  875. */
  876. static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
  877. const void __user * __user *pages,
  878. int __user *status)
  879. {
  880. #define DO_PAGES_STAT_CHUNK_NR 16
  881. const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
  882. int chunk_status[DO_PAGES_STAT_CHUNK_NR];
  883. while (nr_pages) {
  884. unsigned long chunk_nr;
  885. chunk_nr = nr_pages;
  886. if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
  887. chunk_nr = DO_PAGES_STAT_CHUNK_NR;
  888. if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
  889. break;
  890. do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
  891. if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
  892. break;
  893. pages += chunk_nr;
  894. status += chunk_nr;
  895. nr_pages -= chunk_nr;
  896. }
  897. return nr_pages ? -EFAULT : 0;
  898. }
  899. /*
  900. * Move a list of pages in the address space of the currently executing
  901. * process.
  902. */
  903. SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
  904. const void __user * __user *, pages,
  905. const int __user *, nodes,
  906. int __user *, status, int, flags)
  907. {
  908. const struct cred *cred = current_cred(), *tcred;
  909. struct task_struct *task;
  910. struct mm_struct *mm;
  911. int err;
  912. /* Check flags */
  913. if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
  914. return -EINVAL;
  915. if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
  916. return -EPERM;
  917. /* Find the mm_struct */
  918. read_lock(&tasklist_lock);
  919. task = pid ? find_task_by_vpid(pid) : current;
  920. if (!task) {
  921. read_unlock(&tasklist_lock);
  922. return -ESRCH;
  923. }
  924. mm = get_task_mm(task);
  925. read_unlock(&tasklist_lock);
  926. if (!mm)
  927. return -EINVAL;
  928. /*
  929. * Check if this process has the right to modify the specified
  930. * process. The right exists if the process has administrative
  931. * capabilities, superuser privileges or the same
  932. * userid as the target process.
  933. */
  934. rcu_read_lock();
  935. tcred = __task_cred(task);
  936. if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
  937. cred->uid != tcred->suid && cred->uid != tcred->uid &&
  938. !capable(CAP_SYS_NICE)) {
  939. rcu_read_unlock();
  940. err = -EPERM;
  941. goto out;
  942. }
  943. rcu_read_unlock();
  944. err = security_task_movememory(task);
  945. if (err)
  946. goto out;
  947. if (nodes) {
  948. err = do_pages_move(mm, task, nr_pages, pages, nodes, status,
  949. flags);
  950. } else {
  951. err = do_pages_stat(mm, nr_pages, pages, status);
  952. }
  953. out:
  954. mmput(mm);
  955. return err;
  956. }
  957. /*
  958. * Call migration functions in the vma_ops that may prepare
  959. * memory in a vm for migration. migration functions may perform
  960. * the migration for vmas that do not have an underlying page struct.
  961. */
  962. int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
  963. const nodemask_t *from, unsigned long flags)
  964. {
  965. struct vm_area_struct *vma;
  966. int err = 0;
  967. for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
  968. if (vma->vm_ops && vma->vm_ops->migrate) {
  969. err = vma->vm_ops->migrate(vma, to, from, flags);
  970. if (err)
  971. break;
  972. }
  973. }
  974. return err;
  975. }
  976. #endif