migrate.c 33 KB

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