migrate.c 31 KB

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