migrate.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  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 <asm/tlbflush.h>
  37. #include "internal.h"
  38. #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
  39. /*
  40. * migrate_prep() needs to be called before we start compiling a list of pages
  41. * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is
  42. * undesirable, use migrate_prep_local()
  43. */
  44. int migrate_prep(void)
  45. {
  46. /*
  47. * Clear the LRU lists so pages can be isolated.
  48. * Note that pages may be moved off the LRU after we have
  49. * drained them. Those pages will fail to migrate like other
  50. * pages that may be busy.
  51. */
  52. lru_add_drain_all();
  53. return 0;
  54. }
  55. /* Do the necessary work of migrate_prep but not if it involves other CPUs */
  56. int migrate_prep_local(void)
  57. {
  58. lru_add_drain();
  59. return 0;
  60. }
  61. /*
  62. * Add isolated pages on the list back to the LRU under page lock
  63. * to avoid leaking evictable pages back onto unevictable list.
  64. */
  65. void putback_lru_pages(struct list_head *l)
  66. {
  67. struct page *page;
  68. struct page *page2;
  69. list_for_each_entry_safe(page, page2, l, lru) {
  70. list_del(&page->lru);
  71. dec_zone_page_state(page, NR_ISOLATED_ANON +
  72. page_is_file_cache(page));
  73. putback_lru_page(page);
  74. }
  75. }
  76. /*
  77. * Restore a potential migration pte to a working pte entry
  78. */
  79. static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,
  80. unsigned long addr, void *old)
  81. {
  82. struct mm_struct *mm = vma->vm_mm;
  83. swp_entry_t entry;
  84. pgd_t *pgd;
  85. pud_t *pud;
  86. pmd_t *pmd;
  87. pte_t *ptep, pte;
  88. spinlock_t *ptl;
  89. if (unlikely(PageHuge(new))) {
  90. ptep = huge_pte_offset(mm, addr);
  91. if (!ptep)
  92. goto out;
  93. ptl = &mm->page_table_lock;
  94. } else {
  95. pgd = pgd_offset(mm, addr);
  96. if (!pgd_present(*pgd))
  97. goto out;
  98. pud = pud_offset(pgd, addr);
  99. if (!pud_present(*pud))
  100. goto out;
  101. pmd = pmd_offset(pud, addr);
  102. if (!pmd_present(*pmd))
  103. goto out;
  104. ptep = pte_offset_map(pmd, addr);
  105. if (!is_swap_pte(*ptep)) {
  106. pte_unmap(ptep);
  107. goto out;
  108. }
  109. ptl = pte_lockptr(mm, pmd);
  110. }
  111. spin_lock(ptl);
  112. pte = *ptep;
  113. if (!is_swap_pte(pte))
  114. goto unlock;
  115. entry = pte_to_swp_entry(pte);
  116. if (!is_migration_entry(entry) ||
  117. migration_entry_to_page(entry) != old)
  118. goto unlock;
  119. get_page(new);
  120. pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
  121. if (is_write_migration_entry(entry))
  122. pte = pte_mkwrite(pte);
  123. #ifdef CONFIG_HUGETLB_PAGE
  124. if (PageHuge(new))
  125. pte = pte_mkhuge(pte);
  126. #endif
  127. flush_cache_page(vma, addr, pte_pfn(pte));
  128. set_pte_at(mm, addr, ptep, pte);
  129. if (PageHuge(new)) {
  130. if (PageAnon(new))
  131. hugepage_add_anon_rmap(new, vma, addr);
  132. else
  133. page_dup_rmap(new);
  134. } else if (PageAnon(new))
  135. page_add_anon_rmap(new, vma, addr);
  136. else
  137. page_add_file_rmap(new);
  138. /* No need to invalidate - it was non-present before */
  139. update_mmu_cache(vma, addr, ptep);
  140. unlock:
  141. pte_unmap_unlock(ptep, ptl);
  142. out:
  143. return SWAP_AGAIN;
  144. }
  145. /*
  146. * Get rid of all migration entries and replace them by
  147. * references to the indicated page.
  148. */
  149. static void remove_migration_ptes(struct page *old, struct page *new)
  150. {
  151. rmap_walk(new, remove_migration_pte, old);
  152. }
  153. /*
  154. * Something used the pte of a page under migration. We need to
  155. * get to the page and wait until migration is finished.
  156. * When we return from this function the fault will be retried.
  157. *
  158. * This function is called from do_swap_page().
  159. */
  160. void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
  161. unsigned long address)
  162. {
  163. pte_t *ptep, pte;
  164. spinlock_t *ptl;
  165. swp_entry_t entry;
  166. struct page *page;
  167. ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
  168. pte = *ptep;
  169. if (!is_swap_pte(pte))
  170. goto out;
  171. entry = pte_to_swp_entry(pte);
  172. if (!is_migration_entry(entry))
  173. goto out;
  174. page = migration_entry_to_page(entry);
  175. /*
  176. * Once radix-tree replacement of page migration started, page_count
  177. * *must* be zero. And, we don't want to call wait_on_page_locked()
  178. * against a page without get_page().
  179. * So, we use get_page_unless_zero(), here. Even failed, page fault
  180. * will occur again.
  181. */
  182. if (!get_page_unless_zero(page))
  183. goto out;
  184. pte_unmap_unlock(ptep, ptl);
  185. wait_on_page_locked(page);
  186. put_page(page);
  187. return;
  188. out:
  189. pte_unmap_unlock(ptep, ptl);
  190. }
  191. /*
  192. * Replace the page in the mapping.
  193. *
  194. * The number of remaining references must be:
  195. * 1 for anonymous pages without a mapping
  196. * 2 for pages with a mapping
  197. * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
  198. */
  199. static int migrate_page_move_mapping(struct address_space *mapping,
  200. struct page *newpage, struct page *page)
  201. {
  202. int expected_count;
  203. void **pslot;
  204. if (!mapping) {
  205. /* Anonymous page without mapping */
  206. if (page_count(page) != 1)
  207. return -EAGAIN;
  208. return 0;
  209. }
  210. spin_lock_irq(&mapping->tree_lock);
  211. pslot = radix_tree_lookup_slot(&mapping->page_tree,
  212. page_index(page));
  213. expected_count = 2 + page_has_private(page);
  214. if (page_count(page) != expected_count ||
  215. (struct page *)radix_tree_deref_slot(pslot) != page) {
  216. spin_unlock_irq(&mapping->tree_lock);
  217. return -EAGAIN;
  218. }
  219. if (!page_freeze_refs(page, expected_count)) {
  220. spin_unlock_irq(&mapping->tree_lock);
  221. return -EAGAIN;
  222. }
  223. /*
  224. * Now we know that no one else is looking at the page.
  225. */
  226. get_page(newpage); /* add cache reference */
  227. if (PageSwapCache(page)) {
  228. SetPageSwapCache(newpage);
  229. set_page_private(newpage, page_private(page));
  230. }
  231. radix_tree_replace_slot(pslot, newpage);
  232. page_unfreeze_refs(page, expected_count);
  233. /*
  234. * Drop cache reference from old page.
  235. * We know this isn't the last reference.
  236. */
  237. __put_page(page);
  238. /*
  239. * If moved to a different zone then also account
  240. * the page for that zone. Other VM counters will be
  241. * taken care of when we establish references to the
  242. * new page and drop references to the old page.
  243. *
  244. * Note that anonymous pages are accounted for
  245. * via NR_FILE_PAGES and NR_ANON_PAGES if they
  246. * are mapped to swap space.
  247. */
  248. __dec_zone_page_state(page, NR_FILE_PAGES);
  249. __inc_zone_page_state(newpage, NR_FILE_PAGES);
  250. if (PageSwapBacked(page)) {
  251. __dec_zone_page_state(page, NR_SHMEM);
  252. __inc_zone_page_state(newpage, NR_SHMEM);
  253. }
  254. spin_unlock_irq(&mapping->tree_lock);
  255. return 0;
  256. }
  257. /*
  258. * The expected number of remaining references is the same as that
  259. * of migrate_page_move_mapping().
  260. */
  261. int migrate_huge_page_move_mapping(struct address_space *mapping,
  262. struct page *newpage, struct page *page)
  263. {
  264. int expected_count;
  265. void **pslot;
  266. if (!mapping) {
  267. if (page_count(page) != 1)
  268. return -EAGAIN;
  269. return 0;
  270. }
  271. spin_lock_irq(&mapping->tree_lock);
  272. pslot = radix_tree_lookup_slot(&mapping->page_tree,
  273. page_index(page));
  274. expected_count = 2 + page_has_private(page);
  275. if (page_count(page) != expected_count ||
  276. (struct page *)radix_tree_deref_slot(pslot) != page) {
  277. spin_unlock_irq(&mapping->tree_lock);
  278. return -EAGAIN;
  279. }
  280. if (!page_freeze_refs(page, expected_count)) {
  281. spin_unlock_irq(&mapping->tree_lock);
  282. return -EAGAIN;
  283. }
  284. get_page(newpage);
  285. radix_tree_replace_slot(pslot, newpage);
  286. page_unfreeze_refs(page, expected_count);
  287. __put_page(page);
  288. spin_unlock_irq(&mapping->tree_lock);
  289. return 0;
  290. }
  291. /*
  292. * Copy the page to its new location
  293. */
  294. void migrate_page_copy(struct page *newpage, struct page *page)
  295. {
  296. if (PageHuge(page))
  297. copy_huge_page(newpage, page);
  298. else
  299. copy_highpage(newpage, page);
  300. if (PageError(page))
  301. SetPageError(newpage);
  302. if (PageReferenced(page))
  303. SetPageReferenced(newpage);
  304. if (PageUptodate(page))
  305. SetPageUptodate(newpage);
  306. if (TestClearPageActive(page)) {
  307. VM_BUG_ON(PageUnevictable(page));
  308. SetPageActive(newpage);
  309. } else if (TestClearPageUnevictable(page))
  310. SetPageUnevictable(newpage);
  311. if (PageChecked(page))
  312. SetPageChecked(newpage);
  313. if (PageMappedToDisk(page))
  314. SetPageMappedToDisk(newpage);
  315. if (PageDirty(page)) {
  316. clear_page_dirty_for_io(page);
  317. /*
  318. * Want to mark the page and the radix tree as dirty, and
  319. * redo the accounting that clear_page_dirty_for_io undid,
  320. * but we can't use set_page_dirty because that function
  321. * is actually a signal that all of the page has become dirty.
  322. * Wheras only part of our page may be dirty.
  323. */
  324. __set_page_dirty_nobuffers(newpage);
  325. }
  326. mlock_migrate_page(newpage, page);
  327. ksm_migrate_page(newpage, page);
  328. ClearPageSwapCache(page);
  329. ClearPagePrivate(page);
  330. set_page_private(page, 0);
  331. page->mapping = NULL;
  332. /*
  333. * If any waiters have accumulated on the new page then
  334. * wake them up.
  335. */
  336. if (PageWriteback(newpage))
  337. end_page_writeback(newpage);
  338. }
  339. /************************************************************
  340. * Migration functions
  341. ***********************************************************/
  342. /* Always fail migration. Used for mappings that are not movable */
  343. int fail_migrate_page(struct address_space *mapping,
  344. struct page *newpage, struct page *page)
  345. {
  346. return -EIO;
  347. }
  348. EXPORT_SYMBOL(fail_migrate_page);
  349. /*
  350. * Common logic to directly migrate a single page suitable for
  351. * pages that do not use PagePrivate/PagePrivate2.
  352. *
  353. * Pages are locked upon entry and exit.
  354. */
  355. int migrate_page(struct address_space *mapping,
  356. struct page *newpage, struct page *page)
  357. {
  358. int rc;
  359. BUG_ON(PageWriteback(page)); /* Writeback must be complete */
  360. rc = migrate_page_move_mapping(mapping, newpage, page);
  361. if (rc)
  362. return rc;
  363. migrate_page_copy(newpage, page);
  364. return 0;
  365. }
  366. EXPORT_SYMBOL(migrate_page);
  367. #ifdef CONFIG_BLOCK
  368. /*
  369. * Migration function for pages with buffers. This function can only be used
  370. * if the underlying filesystem guarantees that no other references to "page"
  371. * exist.
  372. */
  373. int buffer_migrate_page(struct address_space *mapping,
  374. struct page *newpage, struct page *page)
  375. {
  376. struct buffer_head *bh, *head;
  377. int rc;
  378. if (!page_has_buffers(page))
  379. return migrate_page(mapping, newpage, page);
  380. head = page_buffers(page);
  381. rc = migrate_page_move_mapping(mapping, newpage, page);
  382. if (rc)
  383. return rc;
  384. bh = head;
  385. do {
  386. get_bh(bh);
  387. lock_buffer(bh);
  388. bh = bh->b_this_page;
  389. } while (bh != head);
  390. ClearPagePrivate(page);
  391. set_page_private(newpage, page_private(page));
  392. set_page_private(page, 0);
  393. put_page(page);
  394. get_page(newpage);
  395. bh = head;
  396. do {
  397. set_bh_page(bh, newpage, bh_offset(bh));
  398. bh = bh->b_this_page;
  399. } while (bh != head);
  400. SetPagePrivate(newpage);
  401. migrate_page_copy(newpage, page);
  402. bh = head;
  403. do {
  404. unlock_buffer(bh);
  405. put_bh(bh);
  406. bh = bh->b_this_page;
  407. } while (bh != head);
  408. return 0;
  409. }
  410. EXPORT_SYMBOL(buffer_migrate_page);
  411. #endif
  412. /*
  413. * Writeback a page to clean the dirty state
  414. */
  415. static int writeout(struct address_space *mapping, struct page *page)
  416. {
  417. struct writeback_control wbc = {
  418. .sync_mode = WB_SYNC_NONE,
  419. .nr_to_write = 1,
  420. .range_start = 0,
  421. .range_end = LLONG_MAX,
  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.
  758. * Caller should call putback_lru_pages to return pages to the LRU
  759. * or free list.
  760. *
  761. * Return: Number of pages not migrated or error code.
  762. */
  763. int migrate_pages(struct list_head *from,
  764. new_page_t get_new_page, unsigned long private, int offlining)
  765. {
  766. int retry = 1;
  767. int nr_failed = 0;
  768. int pass = 0;
  769. struct page *page;
  770. struct page *page2;
  771. int swapwrite = current->flags & PF_SWAPWRITE;
  772. int rc;
  773. if (!swapwrite)
  774. current->flags |= PF_SWAPWRITE;
  775. for(pass = 0; pass < 10 && retry; pass++) {
  776. retry = 0;
  777. list_for_each_entry_safe(page, page2, from, lru) {
  778. cond_resched();
  779. rc = unmap_and_move(get_new_page, private,
  780. page, pass > 2, offlining);
  781. switch(rc) {
  782. case -ENOMEM:
  783. goto out;
  784. case -EAGAIN:
  785. retry++;
  786. break;
  787. case 0:
  788. break;
  789. default:
  790. /* Permanent failure */
  791. nr_failed++;
  792. break;
  793. }
  794. }
  795. }
  796. rc = 0;
  797. out:
  798. if (!swapwrite)
  799. current->flags &= ~PF_SWAPWRITE;
  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 || pp->addr < vma->vm_start || !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. if (err)
  930. putback_lru_pages(&pagelist);
  931. }
  932. up_read(&mm->mmap_sem);
  933. return err;
  934. }
  935. /*
  936. * Migrate an array of page address onto an array of nodes and fill
  937. * the corresponding array of status.
  938. */
  939. static int do_pages_move(struct mm_struct *mm, struct task_struct *task,
  940. unsigned long nr_pages,
  941. const void __user * __user *pages,
  942. const int __user *nodes,
  943. int __user *status, int flags)
  944. {
  945. struct page_to_node *pm;
  946. nodemask_t task_nodes;
  947. unsigned long chunk_nr_pages;
  948. unsigned long chunk_start;
  949. int err;
  950. task_nodes = cpuset_mems_allowed(task);
  951. err = -ENOMEM;
  952. pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
  953. if (!pm)
  954. goto out;
  955. migrate_prep();
  956. /*
  957. * Store a chunk of page_to_node array in a page,
  958. * but keep the last one as a marker
  959. */
  960. chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
  961. for (chunk_start = 0;
  962. chunk_start < nr_pages;
  963. chunk_start += chunk_nr_pages) {
  964. int j;
  965. if (chunk_start + chunk_nr_pages > nr_pages)
  966. chunk_nr_pages = nr_pages - chunk_start;
  967. /* fill the chunk pm with addrs and nodes from user-space */
  968. for (j = 0; j < chunk_nr_pages; j++) {
  969. const void __user *p;
  970. int node;
  971. err = -EFAULT;
  972. if (get_user(p, pages + j + chunk_start))
  973. goto out_pm;
  974. pm[j].addr = (unsigned long) p;
  975. if (get_user(node, nodes + j + chunk_start))
  976. goto out_pm;
  977. err = -ENODEV;
  978. if (node < 0 || node >= MAX_NUMNODES)
  979. goto out_pm;
  980. if (!node_state(node, N_HIGH_MEMORY))
  981. goto out_pm;
  982. err = -EACCES;
  983. if (!node_isset(node, task_nodes))
  984. goto out_pm;
  985. pm[j].node = node;
  986. }
  987. /* End marker for this chunk */
  988. pm[chunk_nr_pages].node = MAX_NUMNODES;
  989. /* Migrate this chunk */
  990. err = do_move_page_to_node_array(mm, pm,
  991. flags & MPOL_MF_MOVE_ALL);
  992. if (err < 0)
  993. goto out_pm;
  994. /* Return status information */
  995. for (j = 0; j < chunk_nr_pages; j++)
  996. if (put_user(pm[j].status, status + j + chunk_start)) {
  997. err = -EFAULT;
  998. goto out_pm;
  999. }
  1000. }
  1001. err = 0;
  1002. out_pm:
  1003. free_page((unsigned long)pm);
  1004. out:
  1005. return err;
  1006. }
  1007. /*
  1008. * Determine the nodes of an array of pages and store it in an array of status.
  1009. */
  1010. static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
  1011. const void __user **pages, int *status)
  1012. {
  1013. unsigned long i;
  1014. down_read(&mm->mmap_sem);
  1015. for (i = 0; i < nr_pages; i++) {
  1016. unsigned long addr = (unsigned long)(*pages);
  1017. struct vm_area_struct *vma;
  1018. struct page *page;
  1019. int err = -EFAULT;
  1020. vma = find_vma(mm, addr);
  1021. if (!vma || addr < vma->vm_start)
  1022. goto set_status;
  1023. page = follow_page(vma, addr, 0);
  1024. err = PTR_ERR(page);
  1025. if (IS_ERR(page))
  1026. goto set_status;
  1027. err = -ENOENT;
  1028. /* Use PageReserved to check for zero page */
  1029. if (!page || PageReserved(page) || PageKsm(page))
  1030. goto set_status;
  1031. err = page_to_nid(page);
  1032. set_status:
  1033. *status = err;
  1034. pages++;
  1035. status++;
  1036. }
  1037. up_read(&mm->mmap_sem);
  1038. }
  1039. /*
  1040. * Determine the nodes of a user array of pages and store it in
  1041. * a user array of status.
  1042. */
  1043. static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
  1044. const void __user * __user *pages,
  1045. int __user *status)
  1046. {
  1047. #define DO_PAGES_STAT_CHUNK_NR 16
  1048. const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
  1049. int chunk_status[DO_PAGES_STAT_CHUNK_NR];
  1050. while (nr_pages) {
  1051. unsigned long chunk_nr;
  1052. chunk_nr = nr_pages;
  1053. if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
  1054. chunk_nr = DO_PAGES_STAT_CHUNK_NR;
  1055. if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
  1056. break;
  1057. do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
  1058. if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
  1059. break;
  1060. pages += chunk_nr;
  1061. status += chunk_nr;
  1062. nr_pages -= chunk_nr;
  1063. }
  1064. return nr_pages ? -EFAULT : 0;
  1065. }
  1066. /*
  1067. * Move a list of pages in the address space of the currently executing
  1068. * process.
  1069. */
  1070. SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
  1071. const void __user * __user *, pages,
  1072. const int __user *, nodes,
  1073. int __user *, status, int, flags)
  1074. {
  1075. const struct cred *cred = current_cred(), *tcred;
  1076. struct task_struct *task;
  1077. struct mm_struct *mm;
  1078. int err;
  1079. /* Check flags */
  1080. if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
  1081. return -EINVAL;
  1082. if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
  1083. return -EPERM;
  1084. /* Find the mm_struct */
  1085. read_lock(&tasklist_lock);
  1086. task = pid ? find_task_by_vpid(pid) : current;
  1087. if (!task) {
  1088. read_unlock(&tasklist_lock);
  1089. return -ESRCH;
  1090. }
  1091. mm = get_task_mm(task);
  1092. read_unlock(&tasklist_lock);
  1093. if (!mm)
  1094. return -EINVAL;
  1095. /*
  1096. * Check if this process has the right to modify the specified
  1097. * process. The right exists if the process has administrative
  1098. * capabilities, superuser privileges or the same
  1099. * userid as the target process.
  1100. */
  1101. rcu_read_lock();
  1102. tcred = __task_cred(task);
  1103. if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
  1104. cred->uid != tcred->suid && cred->uid != tcred->uid &&
  1105. !capable(CAP_SYS_NICE)) {
  1106. rcu_read_unlock();
  1107. err = -EPERM;
  1108. goto out;
  1109. }
  1110. rcu_read_unlock();
  1111. err = security_task_movememory(task);
  1112. if (err)
  1113. goto out;
  1114. if (nodes) {
  1115. err = do_pages_move(mm, task, nr_pages, pages, nodes, status,
  1116. flags);
  1117. } else {
  1118. err = do_pages_stat(mm, nr_pages, pages, status);
  1119. }
  1120. out:
  1121. mmput(mm);
  1122. return err;
  1123. }
  1124. /*
  1125. * Call migration functions in the vma_ops that may prepare
  1126. * memory in a vm for migration. migration functions may perform
  1127. * the migration for vmas that do not have an underlying page struct.
  1128. */
  1129. int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
  1130. const nodemask_t *from, unsigned long flags)
  1131. {
  1132. struct vm_area_struct *vma;
  1133. int err = 0;
  1134. for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
  1135. if (vma->vm_ops && vma->vm_ops->migrate) {
  1136. err = vma->vm_ops->migrate(vma, to, from, flags);
  1137. if (err)
  1138. break;
  1139. }
  1140. }
  1141. return err;
  1142. }
  1143. #endif