migrate.c 32 KB

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