migrate.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862
  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 <linux/balloon_compaction.h>
  38. #include <linux/mmu_notifier.h>
  39. #include <asm/tlbflush.h>
  40. #define CREATE_TRACE_POINTS
  41. #include <trace/events/migrate.h>
  42. #include "internal.h"
  43. /*
  44. * migrate_prep() needs to be called before we start compiling a list of pages
  45. * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is
  46. * undesirable, use migrate_prep_local()
  47. */
  48. int migrate_prep(void)
  49. {
  50. /*
  51. * Clear the LRU lists so pages can be isolated.
  52. * Note that pages may be moved off the LRU after we have
  53. * drained them. Those pages will fail to migrate like other
  54. * pages that may be busy.
  55. */
  56. lru_add_drain_all();
  57. return 0;
  58. }
  59. /* Do the necessary work of migrate_prep but not if it involves other CPUs */
  60. int migrate_prep_local(void)
  61. {
  62. lru_add_drain();
  63. return 0;
  64. }
  65. /*
  66. * Put previously isolated pages back onto the appropriate lists
  67. * from where they were once taken off for compaction/migration.
  68. *
  69. * This function shall be used whenever the isolated pageset has been
  70. * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range()
  71. * and isolate_huge_page().
  72. */
  73. void putback_movable_pages(struct list_head *l)
  74. {
  75. struct page *page;
  76. struct page *page2;
  77. list_for_each_entry_safe(page, page2, l, lru) {
  78. if (unlikely(PageHuge(page))) {
  79. putback_active_hugepage(page);
  80. continue;
  81. }
  82. list_del(&page->lru);
  83. dec_zone_page_state(page, NR_ISOLATED_ANON +
  84. page_is_file_cache(page));
  85. if (unlikely(isolated_balloon_page(page)))
  86. balloon_page_putback(page);
  87. else
  88. putback_lru_page(page);
  89. }
  90. }
  91. /*
  92. * Restore a potential migration pte to a working pte entry
  93. */
  94. static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,
  95. unsigned long addr, void *old)
  96. {
  97. struct mm_struct *mm = vma->vm_mm;
  98. swp_entry_t entry;
  99. pmd_t *pmd;
  100. pte_t *ptep, pte;
  101. spinlock_t *ptl;
  102. if (unlikely(PageHuge(new))) {
  103. ptep = huge_pte_offset(mm, addr);
  104. if (!ptep)
  105. goto out;
  106. ptl = huge_pte_lockptr(hstate_vma(vma), mm, ptep);
  107. } else {
  108. pmd = mm_find_pmd(mm, addr);
  109. if (!pmd)
  110. goto out;
  111. ptep = pte_offset_map(pmd, addr);
  112. /*
  113. * Peek to check is_swap_pte() before taking ptlock? No, we
  114. * can race mremap's move_ptes(), which skips anon_vma lock.
  115. */
  116. ptl = pte_lockptr(mm, pmd);
  117. }
  118. spin_lock(ptl);
  119. pte = *ptep;
  120. if (!is_swap_pte(pte))
  121. goto unlock;
  122. entry = pte_to_swp_entry(pte);
  123. if (!is_migration_entry(entry) ||
  124. migration_entry_to_page(entry) != old)
  125. goto unlock;
  126. get_page(new);
  127. pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
  128. if (pte_swp_soft_dirty(*ptep))
  129. pte = pte_mksoft_dirty(pte);
  130. /* Recheck VMA as permissions can change since migration started */
  131. if (is_write_migration_entry(entry))
  132. pte = maybe_mkwrite(pte, vma);
  133. #ifdef CONFIG_HUGETLB_PAGE
  134. if (PageHuge(new)) {
  135. pte = pte_mkhuge(pte);
  136. pte = arch_make_huge_pte(pte, vma, new, 0);
  137. }
  138. #endif
  139. flush_dcache_page(new);
  140. set_pte_at(mm, addr, ptep, pte);
  141. if (PageHuge(new)) {
  142. if (PageAnon(new))
  143. hugepage_add_anon_rmap(new, vma, addr);
  144. else
  145. page_dup_rmap(new);
  146. } else if (PageAnon(new))
  147. page_add_anon_rmap(new, vma, addr);
  148. else
  149. page_add_file_rmap(new);
  150. /* No need to invalidate - it was non-present before */
  151. update_mmu_cache(vma, addr, ptep);
  152. unlock:
  153. pte_unmap_unlock(ptep, ptl);
  154. out:
  155. return SWAP_AGAIN;
  156. }
  157. /*
  158. * Get rid of all migration entries and replace them by
  159. * references to the indicated page.
  160. */
  161. static void remove_migration_ptes(struct page *old, struct page *new)
  162. {
  163. struct rmap_walk_control rwc = {
  164. .rmap_one = remove_migration_pte,
  165. .arg = old,
  166. };
  167. rmap_walk(new, &rwc);
  168. }
  169. /*
  170. * Something used the pte of a page under migration. We need to
  171. * get to the page and wait until migration is finished.
  172. * When we return from this function the fault will be retried.
  173. */
  174. void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
  175. spinlock_t *ptl)
  176. {
  177. pte_t pte;
  178. swp_entry_t entry;
  179. struct page *page;
  180. spin_lock(ptl);
  181. pte = *ptep;
  182. if (!is_swap_pte(pte))
  183. goto out;
  184. entry = pte_to_swp_entry(pte);
  185. if (!is_migration_entry(entry))
  186. goto out;
  187. page = migration_entry_to_page(entry);
  188. /*
  189. * Once radix-tree replacement of page migration started, page_count
  190. * *must* be zero. And, we don't want to call wait_on_page_locked()
  191. * against a page without get_page().
  192. * So, we use get_page_unless_zero(), here. Even failed, page fault
  193. * will occur again.
  194. */
  195. if (!get_page_unless_zero(page))
  196. goto out;
  197. pte_unmap_unlock(ptep, ptl);
  198. wait_on_page_locked(page);
  199. put_page(page);
  200. return;
  201. out:
  202. pte_unmap_unlock(ptep, ptl);
  203. }
  204. void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
  205. unsigned long address)
  206. {
  207. spinlock_t *ptl = pte_lockptr(mm, pmd);
  208. pte_t *ptep = pte_offset_map(pmd, address);
  209. __migration_entry_wait(mm, ptep, ptl);
  210. }
  211. void migration_entry_wait_huge(struct vm_area_struct *vma,
  212. struct mm_struct *mm, pte_t *pte)
  213. {
  214. spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte);
  215. __migration_entry_wait(mm, pte, ptl);
  216. }
  217. #ifdef CONFIG_BLOCK
  218. /* Returns true if all buffers are successfully locked */
  219. static bool buffer_migrate_lock_buffers(struct buffer_head *head,
  220. enum migrate_mode mode)
  221. {
  222. struct buffer_head *bh = head;
  223. /* Simple case, sync compaction */
  224. if (mode != MIGRATE_ASYNC) {
  225. do {
  226. get_bh(bh);
  227. lock_buffer(bh);
  228. bh = bh->b_this_page;
  229. } while (bh != head);
  230. return true;
  231. }
  232. /* async case, we cannot block on lock_buffer so use trylock_buffer */
  233. do {
  234. get_bh(bh);
  235. if (!trylock_buffer(bh)) {
  236. /*
  237. * We failed to lock the buffer and cannot stall in
  238. * async migration. Release the taken locks
  239. */
  240. struct buffer_head *failed_bh = bh;
  241. put_bh(failed_bh);
  242. bh = head;
  243. while (bh != failed_bh) {
  244. unlock_buffer(bh);
  245. put_bh(bh);
  246. bh = bh->b_this_page;
  247. }
  248. return false;
  249. }
  250. bh = bh->b_this_page;
  251. } while (bh != head);
  252. return true;
  253. }
  254. #else
  255. static inline bool buffer_migrate_lock_buffers(struct buffer_head *head,
  256. enum migrate_mode mode)
  257. {
  258. return true;
  259. }
  260. #endif /* CONFIG_BLOCK */
  261. /*
  262. * Replace the page in the mapping.
  263. *
  264. * The number of remaining references must be:
  265. * 1 for anonymous pages without a mapping
  266. * 2 for pages with a mapping
  267. * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
  268. */
  269. int migrate_page_move_mapping(struct address_space *mapping,
  270. struct page *newpage, struct page *page,
  271. struct buffer_head *head, enum migrate_mode mode,
  272. int extra_count)
  273. {
  274. int expected_count = 1 + extra_count;
  275. void **pslot;
  276. if (!mapping) {
  277. /* Anonymous page without mapping */
  278. if (page_count(page) != expected_count)
  279. return -EAGAIN;
  280. return MIGRATEPAGE_SUCCESS;
  281. }
  282. spin_lock_irq(&mapping->tree_lock);
  283. pslot = radix_tree_lookup_slot(&mapping->page_tree,
  284. page_index(page));
  285. expected_count += 1 + page_has_private(page);
  286. if (page_count(page) != expected_count ||
  287. radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
  288. spin_unlock_irq(&mapping->tree_lock);
  289. return -EAGAIN;
  290. }
  291. if (!page_freeze_refs(page, expected_count)) {
  292. spin_unlock_irq(&mapping->tree_lock);
  293. return -EAGAIN;
  294. }
  295. /*
  296. * In the async migration case of moving a page with buffers, lock the
  297. * buffers using trylock before the mapping is moved. If the mapping
  298. * was moved, we later failed to lock the buffers and could not move
  299. * the mapping back due to an elevated page count, we would have to
  300. * block waiting on other references to be dropped.
  301. */
  302. if (mode == MIGRATE_ASYNC && head &&
  303. !buffer_migrate_lock_buffers(head, mode)) {
  304. page_unfreeze_refs(page, expected_count);
  305. spin_unlock_irq(&mapping->tree_lock);
  306. return -EAGAIN;
  307. }
  308. /*
  309. * Now we know that no one else is looking at the page.
  310. */
  311. get_page(newpage); /* add cache reference */
  312. if (PageSwapCache(page)) {
  313. SetPageSwapCache(newpage);
  314. set_page_private(newpage, page_private(page));
  315. }
  316. radix_tree_replace_slot(pslot, newpage);
  317. /*
  318. * Drop cache reference from old page by unfreezing
  319. * to one less reference.
  320. * We know this isn't the last reference.
  321. */
  322. page_unfreeze_refs(page, expected_count - 1);
  323. /*
  324. * If moved to a different zone then also account
  325. * the page for that zone. Other VM counters will be
  326. * taken care of when we establish references to the
  327. * new page and drop references to the old page.
  328. *
  329. * Note that anonymous pages are accounted for
  330. * via NR_FILE_PAGES and NR_ANON_PAGES if they
  331. * are mapped to swap space.
  332. */
  333. __dec_zone_page_state(page, NR_FILE_PAGES);
  334. __inc_zone_page_state(newpage, NR_FILE_PAGES);
  335. if (!PageSwapCache(page) && PageSwapBacked(page)) {
  336. __dec_zone_page_state(page, NR_SHMEM);
  337. __inc_zone_page_state(newpage, NR_SHMEM);
  338. }
  339. spin_unlock_irq(&mapping->tree_lock);
  340. return MIGRATEPAGE_SUCCESS;
  341. }
  342. /*
  343. * The expected number of remaining references is the same as that
  344. * of migrate_page_move_mapping().
  345. */
  346. int migrate_huge_page_move_mapping(struct address_space *mapping,
  347. struct page *newpage, struct page *page)
  348. {
  349. int expected_count;
  350. void **pslot;
  351. if (!mapping) {
  352. if (page_count(page) != 1)
  353. return -EAGAIN;
  354. return MIGRATEPAGE_SUCCESS;
  355. }
  356. spin_lock_irq(&mapping->tree_lock);
  357. pslot = radix_tree_lookup_slot(&mapping->page_tree,
  358. page_index(page));
  359. expected_count = 2 + page_has_private(page);
  360. if (page_count(page) != expected_count ||
  361. radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
  362. spin_unlock_irq(&mapping->tree_lock);
  363. return -EAGAIN;
  364. }
  365. if (!page_freeze_refs(page, expected_count)) {
  366. spin_unlock_irq(&mapping->tree_lock);
  367. return -EAGAIN;
  368. }
  369. get_page(newpage);
  370. radix_tree_replace_slot(pslot, newpage);
  371. page_unfreeze_refs(page, expected_count - 1);
  372. spin_unlock_irq(&mapping->tree_lock);
  373. return MIGRATEPAGE_SUCCESS;
  374. }
  375. /*
  376. * Gigantic pages are so large that we do not guarantee that page++ pointer
  377. * arithmetic will work across the entire page. We need something more
  378. * specialized.
  379. */
  380. static void __copy_gigantic_page(struct page *dst, struct page *src,
  381. int nr_pages)
  382. {
  383. int i;
  384. struct page *dst_base = dst;
  385. struct page *src_base = src;
  386. for (i = 0; i < nr_pages; ) {
  387. cond_resched();
  388. copy_highpage(dst, src);
  389. i++;
  390. dst = mem_map_next(dst, dst_base, i);
  391. src = mem_map_next(src, src_base, i);
  392. }
  393. }
  394. static void copy_huge_page(struct page *dst, struct page *src)
  395. {
  396. int i;
  397. int nr_pages;
  398. if (PageHuge(src)) {
  399. /* hugetlbfs page */
  400. struct hstate *h = page_hstate(src);
  401. nr_pages = pages_per_huge_page(h);
  402. if (unlikely(nr_pages > MAX_ORDER_NR_PAGES)) {
  403. __copy_gigantic_page(dst, src, nr_pages);
  404. return;
  405. }
  406. } else {
  407. /* thp page */
  408. BUG_ON(!PageTransHuge(src));
  409. nr_pages = hpage_nr_pages(src);
  410. }
  411. for (i = 0; i < nr_pages; i++) {
  412. cond_resched();
  413. copy_highpage(dst + i, src + i);
  414. }
  415. }
  416. /*
  417. * Copy the page to its new location
  418. */
  419. void migrate_page_copy(struct page *newpage, struct page *page)
  420. {
  421. int cpupid;
  422. if (PageHuge(page) || PageTransHuge(page))
  423. copy_huge_page(newpage, page);
  424. else
  425. copy_highpage(newpage, page);
  426. if (PageError(page))
  427. SetPageError(newpage);
  428. if (PageReferenced(page))
  429. SetPageReferenced(newpage);
  430. if (PageUptodate(page))
  431. SetPageUptodate(newpage);
  432. if (TestClearPageActive(page)) {
  433. VM_BUG_ON_PAGE(PageUnevictable(page), page);
  434. SetPageActive(newpage);
  435. } else if (TestClearPageUnevictable(page))
  436. SetPageUnevictable(newpage);
  437. if (PageChecked(page))
  438. SetPageChecked(newpage);
  439. if (PageMappedToDisk(page))
  440. SetPageMappedToDisk(newpage);
  441. if (PageDirty(page)) {
  442. clear_page_dirty_for_io(page);
  443. /*
  444. * Want to mark the page and the radix tree as dirty, and
  445. * redo the accounting that clear_page_dirty_for_io undid,
  446. * but we can't use set_page_dirty because that function
  447. * is actually a signal that all of the page has become dirty.
  448. * Whereas only part of our page may be dirty.
  449. */
  450. if (PageSwapBacked(page))
  451. SetPageDirty(newpage);
  452. else
  453. __set_page_dirty_nobuffers(newpage);
  454. }
  455. /*
  456. * Copy NUMA information to the new page, to prevent over-eager
  457. * future migrations of this same page.
  458. */
  459. cpupid = page_cpupid_xchg_last(page, -1);
  460. page_cpupid_xchg_last(newpage, cpupid);
  461. mlock_migrate_page(newpage, page);
  462. ksm_migrate_page(newpage, page);
  463. /*
  464. * Please do not reorder this without considering how mm/ksm.c's
  465. * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache().
  466. */
  467. if (PageSwapCache(page))
  468. ClearPageSwapCache(page);
  469. ClearPagePrivate(page);
  470. set_page_private(page, 0);
  471. /*
  472. * If any waiters have accumulated on the new page then
  473. * wake them up.
  474. */
  475. if (PageWriteback(newpage))
  476. end_page_writeback(newpage);
  477. }
  478. /************************************************************
  479. * Migration functions
  480. ***********************************************************/
  481. /*
  482. * Common logic to directly migrate a single page suitable for
  483. * pages that do not use PagePrivate/PagePrivate2.
  484. *
  485. * Pages are locked upon entry and exit.
  486. */
  487. int migrate_page(struct address_space *mapping,
  488. struct page *newpage, struct page *page,
  489. enum migrate_mode mode)
  490. {
  491. int rc;
  492. BUG_ON(PageWriteback(page)); /* Writeback must be complete */
  493. rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode, 0);
  494. if (rc != MIGRATEPAGE_SUCCESS)
  495. return rc;
  496. migrate_page_copy(newpage, page);
  497. return MIGRATEPAGE_SUCCESS;
  498. }
  499. EXPORT_SYMBOL(migrate_page);
  500. #ifdef CONFIG_BLOCK
  501. /*
  502. * Migration function for pages with buffers. This function can only be used
  503. * if the underlying filesystem guarantees that no other references to "page"
  504. * exist.
  505. */
  506. int buffer_migrate_page(struct address_space *mapping,
  507. struct page *newpage, struct page *page, enum migrate_mode mode)
  508. {
  509. struct buffer_head *bh, *head;
  510. int rc;
  511. if (!page_has_buffers(page))
  512. return migrate_page(mapping, newpage, page, mode);
  513. head = page_buffers(page);
  514. rc = migrate_page_move_mapping(mapping, newpage, page, head, mode, 0);
  515. if (rc != MIGRATEPAGE_SUCCESS)
  516. return rc;
  517. /*
  518. * In the async case, migrate_page_move_mapping locked the buffers
  519. * with an IRQ-safe spinlock held. In the sync case, the buffers
  520. * need to be locked now
  521. */
  522. if (mode != MIGRATE_ASYNC)
  523. BUG_ON(!buffer_migrate_lock_buffers(head, mode));
  524. ClearPagePrivate(page);
  525. set_page_private(newpage, page_private(page));
  526. set_page_private(page, 0);
  527. put_page(page);
  528. get_page(newpage);
  529. bh = head;
  530. do {
  531. set_bh_page(bh, newpage, bh_offset(bh));
  532. bh = bh->b_this_page;
  533. } while (bh != head);
  534. SetPagePrivate(newpage);
  535. migrate_page_copy(newpage, page);
  536. bh = head;
  537. do {
  538. unlock_buffer(bh);
  539. put_bh(bh);
  540. bh = bh->b_this_page;
  541. } while (bh != head);
  542. return MIGRATEPAGE_SUCCESS;
  543. }
  544. EXPORT_SYMBOL(buffer_migrate_page);
  545. #endif
  546. /*
  547. * Writeback a page to clean the dirty state
  548. */
  549. static int writeout(struct address_space *mapping, struct page *page)
  550. {
  551. struct writeback_control wbc = {
  552. .sync_mode = WB_SYNC_NONE,
  553. .nr_to_write = 1,
  554. .range_start = 0,
  555. .range_end = LLONG_MAX,
  556. .for_reclaim = 1
  557. };
  558. int rc;
  559. if (!mapping->a_ops->writepage)
  560. /* No write method for the address space */
  561. return -EINVAL;
  562. if (!clear_page_dirty_for_io(page))
  563. /* Someone else already triggered a write */
  564. return -EAGAIN;
  565. /*
  566. * A dirty page may imply that the underlying filesystem has
  567. * the page on some queue. So the page must be clean for
  568. * migration. Writeout may mean we loose the lock and the
  569. * page state is no longer what we checked for earlier.
  570. * At this point we know that the migration attempt cannot
  571. * be successful.
  572. */
  573. remove_migration_ptes(page, page);
  574. rc = mapping->a_ops->writepage(page, &wbc);
  575. if (rc != AOP_WRITEPAGE_ACTIVATE)
  576. /* unlocked. Relock */
  577. lock_page(page);
  578. return (rc < 0) ? -EIO : -EAGAIN;
  579. }
  580. /*
  581. * Default handling if a filesystem does not provide a migration function.
  582. */
  583. static int fallback_migrate_page(struct address_space *mapping,
  584. struct page *newpage, struct page *page, enum migrate_mode mode)
  585. {
  586. if (PageDirty(page)) {
  587. /* Only writeback pages in full synchronous migration */
  588. if (mode != MIGRATE_SYNC)
  589. return -EBUSY;
  590. return writeout(mapping, page);
  591. }
  592. /*
  593. * Buffers may be managed in a filesystem specific way.
  594. * We must have no buffers or drop them.
  595. */
  596. if (page_has_private(page) &&
  597. !try_to_release_page(page, GFP_KERNEL))
  598. return -EAGAIN;
  599. return migrate_page(mapping, newpage, page, mode);
  600. }
  601. /*
  602. * Move a page to a newly allocated page
  603. * The page is locked and all ptes have been successfully removed.
  604. *
  605. * The new page will have replaced the old page if this function
  606. * is successful.
  607. *
  608. * Return value:
  609. * < 0 - error code
  610. * MIGRATEPAGE_SUCCESS - success
  611. */
  612. static int move_to_new_page(struct page *newpage, struct page *page,
  613. int page_was_mapped, enum migrate_mode mode)
  614. {
  615. struct address_space *mapping;
  616. int rc;
  617. /*
  618. * Block others from accessing the page when we get around to
  619. * establishing additional references. We are the only one
  620. * holding a reference to the new page at this point.
  621. */
  622. if (!trylock_page(newpage))
  623. BUG();
  624. /* Prepare mapping for the new page.*/
  625. newpage->index = page->index;
  626. newpage->mapping = page->mapping;
  627. if (PageSwapBacked(page))
  628. SetPageSwapBacked(newpage);
  629. mapping = page_mapping(page);
  630. if (!mapping)
  631. rc = migrate_page(mapping, newpage, page, mode);
  632. else if (mapping->a_ops->migratepage)
  633. /*
  634. * Most pages have a mapping and most filesystems provide a
  635. * migratepage callback. Anonymous pages are part of swap
  636. * space which also has its own migratepage callback. This
  637. * is the most common path for page migration.
  638. */
  639. rc = mapping->a_ops->migratepage(mapping,
  640. newpage, page, mode);
  641. else
  642. rc = fallback_migrate_page(mapping, newpage, page, mode);
  643. if (rc != MIGRATEPAGE_SUCCESS) {
  644. newpage->mapping = NULL;
  645. } else {
  646. mem_cgroup_migrate(page, newpage, false);
  647. if (page_was_mapped)
  648. remove_migration_ptes(page, newpage);
  649. page->mapping = NULL;
  650. }
  651. unlock_page(newpage);
  652. return rc;
  653. }
  654. static int __unmap_and_move(struct page *page, struct page *newpage,
  655. int force, enum migrate_mode mode)
  656. {
  657. int rc = -EAGAIN;
  658. int page_was_mapped = 0;
  659. struct anon_vma *anon_vma = NULL;
  660. if (!trylock_page(page)) {
  661. if (!force || mode == MIGRATE_ASYNC)
  662. goto out;
  663. /*
  664. * It's not safe for direct compaction to call lock_page.
  665. * For example, during page readahead pages are added locked
  666. * to the LRU. Later, when the IO completes the pages are
  667. * marked uptodate and unlocked. However, the queueing
  668. * could be merging multiple pages for one bio (e.g.
  669. * mpage_readpages). If an allocation happens for the
  670. * second or third page, the process can end up locking
  671. * the same page twice and deadlocking. Rather than
  672. * trying to be clever about what pages can be locked,
  673. * avoid the use of lock_page for direct compaction
  674. * altogether.
  675. */
  676. if (current->flags & PF_MEMALLOC)
  677. goto out;
  678. lock_page(page);
  679. }
  680. if (PageWriteback(page)) {
  681. /*
  682. * Only in the case of a full synchronous migration is it
  683. * necessary to wait for PageWriteback. In the async case,
  684. * the retry loop is too short and in the sync-light case,
  685. * the overhead of stalling is too much
  686. */
  687. if (mode != MIGRATE_SYNC) {
  688. rc = -EBUSY;
  689. goto out_unlock;
  690. }
  691. if (!force)
  692. goto out_unlock;
  693. wait_on_page_writeback(page);
  694. }
  695. /*
  696. * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
  697. * we cannot notice that anon_vma is freed while we migrates a page.
  698. * This get_anon_vma() delays freeing anon_vma pointer until the end
  699. * of migration. File cache pages are no problem because of page_lock()
  700. * File Caches may use write_page() or lock_page() in migration, then,
  701. * just care Anon page here.
  702. */
  703. if (PageAnon(page) && !PageKsm(page)) {
  704. /*
  705. * Only page_lock_anon_vma_read() understands the subtleties of
  706. * getting a hold on an anon_vma from outside one of its mms.
  707. */
  708. anon_vma = page_get_anon_vma(page);
  709. if (anon_vma) {
  710. /*
  711. * Anon page
  712. */
  713. } else if (PageSwapCache(page)) {
  714. /*
  715. * We cannot be sure that the anon_vma of an unmapped
  716. * swapcache page is safe to use because we don't
  717. * know in advance if the VMA that this page belonged
  718. * to still exists. If the VMA and others sharing the
  719. * data have been freed, then the anon_vma could
  720. * already be invalid.
  721. *
  722. * To avoid this possibility, swapcache pages get
  723. * migrated but are not remapped when migration
  724. * completes
  725. */
  726. } else {
  727. goto out_unlock;
  728. }
  729. }
  730. if (unlikely(isolated_balloon_page(page))) {
  731. /*
  732. * A ballooned page does not need any special attention from
  733. * physical to virtual reverse mapping procedures.
  734. * Skip any attempt to unmap PTEs or to remap swap cache,
  735. * in order to avoid burning cycles at rmap level, and perform
  736. * the page migration right away (proteced by page lock).
  737. */
  738. rc = balloon_page_migrate(newpage, page, mode);
  739. goto out_unlock;
  740. }
  741. /*
  742. * Corner case handling:
  743. * 1. When a new swap-cache page is read into, it is added to the LRU
  744. * and treated as swapcache but it has no rmap yet.
  745. * Calling try_to_unmap() against a page->mapping==NULL page will
  746. * trigger a BUG. So handle it here.
  747. * 2. An orphaned page (see truncate_complete_page) might have
  748. * fs-private metadata. The page can be picked up due to memory
  749. * offlining. Everywhere else except page reclaim, the page is
  750. * invisible to the vm, so the page can not be migrated. So try to
  751. * free the metadata, so the page can be freed.
  752. */
  753. if (!page->mapping) {
  754. VM_BUG_ON_PAGE(PageAnon(page), page);
  755. if (page_has_private(page)) {
  756. try_to_free_buffers(page);
  757. goto out_unlock;
  758. }
  759. goto skip_unmap;
  760. }
  761. /* Establish migration ptes or remove ptes */
  762. if (page_mapped(page)) {
  763. try_to_unmap(page,
  764. TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS|
  765. TTU_IGNORE_HWPOISON);
  766. page_was_mapped = 1;
  767. }
  768. skip_unmap:
  769. if (!page_mapped(page))
  770. rc = move_to_new_page(newpage, page, page_was_mapped, mode);
  771. if (rc && page_was_mapped)
  772. remove_migration_ptes(page, page);
  773. /* Drop an anon_vma reference if we took one */
  774. if (anon_vma)
  775. put_anon_vma(anon_vma);
  776. out_unlock:
  777. unlock_page(page);
  778. out:
  779. return rc;
  780. }
  781. /*
  782. * gcc 4.7 and 4.8 on arm get an ICEs when inlining unmap_and_move(). Work
  783. * around it.
  784. */
  785. #if (GCC_VERSION >= 40700 && GCC_VERSION < 40900) && defined(CONFIG_ARM)
  786. #define ICE_noinline noinline
  787. #else
  788. #define ICE_noinline
  789. #endif
  790. /*
  791. * Obtain the lock on page, remove all ptes and migrate the page
  792. * to the newly allocated page in newpage.
  793. */
  794. static ICE_noinline int unmap_and_move(new_page_t get_new_page,
  795. free_page_t put_new_page,
  796. unsigned long private, struct page *page,
  797. int force, enum migrate_mode mode,
  798. enum migrate_reason reason)
  799. {
  800. int rc = 0;
  801. int *result = NULL;
  802. struct page *newpage = get_new_page(page, private, &result);
  803. if (!newpage)
  804. return -ENOMEM;
  805. if (page_count(page) == 1) {
  806. /* page was freed from under us. So we are done. */
  807. goto out;
  808. }
  809. if (unlikely(PageTransHuge(page)))
  810. if (unlikely(split_huge_page(page)))
  811. goto out;
  812. rc = __unmap_and_move(page, newpage, force, mode);
  813. out:
  814. if (rc != -EAGAIN) {
  815. /*
  816. * A page that has been migrated has all references
  817. * removed and will be freed. A page that has not been
  818. * migrated will have kepts its references and be
  819. * restored.
  820. */
  821. list_del(&page->lru);
  822. dec_zone_page_state(page, NR_ISOLATED_ANON +
  823. page_is_file_cache(page));
  824. /* Soft-offlined page shouldn't go through lru cache list */
  825. if (reason == MR_MEMORY_FAILURE)
  826. put_page(page);
  827. else
  828. putback_lru_page(page);
  829. }
  830. /*
  831. * If migration was not successful and there's a freeing callback, use
  832. * it. Otherwise, putback_lru_page() will drop the reference grabbed
  833. * during isolation.
  834. */
  835. if (rc != MIGRATEPAGE_SUCCESS && put_new_page) {
  836. ClearPageSwapBacked(newpage);
  837. put_new_page(newpage, private);
  838. } else if (unlikely(__is_movable_balloon_page(newpage))) {
  839. /* drop our reference, page already in the balloon */
  840. put_page(newpage);
  841. } else
  842. putback_lru_page(newpage);
  843. if (result) {
  844. if (rc)
  845. *result = rc;
  846. else
  847. *result = page_to_nid(newpage);
  848. }
  849. return rc;
  850. }
  851. /*
  852. * Counterpart of unmap_and_move_page() for hugepage migration.
  853. *
  854. * This function doesn't wait the completion of hugepage I/O
  855. * because there is no race between I/O and migration for hugepage.
  856. * Note that currently hugepage I/O occurs only in direct I/O
  857. * where no lock is held and PG_writeback is irrelevant,
  858. * and writeback status of all subpages are counted in the reference
  859. * count of the head page (i.e. if all subpages of a 2MB hugepage are
  860. * under direct I/O, the reference of the head page is 512 and a bit more.)
  861. * This means that when we try to migrate hugepage whose subpages are
  862. * doing direct I/O, some references remain after try_to_unmap() and
  863. * hugepage migration fails without data corruption.
  864. *
  865. * There is also no race when direct I/O is issued on the page under migration,
  866. * because then pte is replaced with migration swap entry and direct I/O code
  867. * will wait in the page fault for migration to complete.
  868. */
  869. static int unmap_and_move_huge_page(new_page_t get_new_page,
  870. free_page_t put_new_page, unsigned long private,
  871. struct page *hpage, int force,
  872. enum migrate_mode mode)
  873. {
  874. int rc = 0;
  875. int *result = NULL;
  876. int page_was_mapped = 0;
  877. struct page *new_hpage;
  878. struct anon_vma *anon_vma = NULL;
  879. /*
  880. * Movability of hugepages depends on architectures and hugepage size.
  881. * This check is necessary because some callers of hugepage migration
  882. * like soft offline and memory hotremove don't walk through page
  883. * tables or check whether the hugepage is pmd-based or not before
  884. * kicking migration.
  885. */
  886. if (!hugepage_migration_supported(page_hstate(hpage))) {
  887. putback_active_hugepage(hpage);
  888. return -ENOSYS;
  889. }
  890. new_hpage = get_new_page(hpage, private, &result);
  891. if (!new_hpage)
  892. return -ENOMEM;
  893. rc = -EAGAIN;
  894. if (!trylock_page(hpage)) {
  895. if (!force || mode != MIGRATE_SYNC)
  896. goto out;
  897. lock_page(hpage);
  898. }
  899. if (PageAnon(hpage))
  900. anon_vma = page_get_anon_vma(hpage);
  901. if (page_mapped(hpage)) {
  902. try_to_unmap(hpage,
  903. TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
  904. page_was_mapped = 1;
  905. }
  906. if (!page_mapped(hpage))
  907. rc = move_to_new_page(new_hpage, hpage, page_was_mapped, mode);
  908. if (rc != MIGRATEPAGE_SUCCESS && page_was_mapped)
  909. remove_migration_ptes(hpage, hpage);
  910. if (anon_vma)
  911. put_anon_vma(anon_vma);
  912. if (rc == MIGRATEPAGE_SUCCESS)
  913. hugetlb_cgroup_migrate(hpage, new_hpage);
  914. unlock_page(hpage);
  915. out:
  916. if (rc != -EAGAIN)
  917. putback_active_hugepage(hpage);
  918. /*
  919. * If migration was not successful and there's a freeing callback, use
  920. * it. Otherwise, put_page() will drop the reference grabbed during
  921. * isolation.
  922. */
  923. if (rc != MIGRATEPAGE_SUCCESS && put_new_page)
  924. put_new_page(new_hpage, private);
  925. else
  926. put_page(new_hpage);
  927. if (result) {
  928. if (rc)
  929. *result = rc;
  930. else
  931. *result = page_to_nid(new_hpage);
  932. }
  933. return rc;
  934. }
  935. /*
  936. * migrate_pages - migrate the pages specified in a list, to the free pages
  937. * supplied as the target for the page migration
  938. *
  939. * @from: The list of pages to be migrated.
  940. * @get_new_page: The function used to allocate free pages to be used
  941. * as the target of the page migration.
  942. * @put_new_page: The function used to free target pages if migration
  943. * fails, or NULL if no special handling is necessary.
  944. * @private: Private data to be passed on to get_new_page()
  945. * @mode: The migration mode that specifies the constraints for
  946. * page migration, if any.
  947. * @reason: The reason for page migration.
  948. *
  949. * The function returns after 10 attempts or if no pages are movable any more
  950. * because the list has become empty or no retryable pages exist any more.
  951. * The caller should call putback_lru_pages() to return pages to the LRU
  952. * or free list only if ret != 0.
  953. *
  954. * Returns the number of pages that were not migrated, or an error code.
  955. */
  956. int migrate_pages(struct list_head *from, new_page_t get_new_page,
  957. free_page_t put_new_page, unsigned long private,
  958. enum migrate_mode mode, int reason)
  959. {
  960. int retry = 1;
  961. int nr_failed = 0;
  962. int nr_succeeded = 0;
  963. int pass = 0;
  964. struct page *page;
  965. struct page *page2;
  966. int swapwrite = current->flags & PF_SWAPWRITE;
  967. int rc;
  968. if (!swapwrite)
  969. current->flags |= PF_SWAPWRITE;
  970. for(pass = 0; pass < 10 && retry; pass++) {
  971. retry = 0;
  972. list_for_each_entry_safe(page, page2, from, lru) {
  973. cond_resched();
  974. if (PageHuge(page))
  975. rc = unmap_and_move_huge_page(get_new_page,
  976. put_new_page, private, page,
  977. pass > 2, mode);
  978. else
  979. rc = unmap_and_move(get_new_page, put_new_page,
  980. private, page, pass > 2, mode,
  981. reason);
  982. switch(rc) {
  983. case -ENOMEM:
  984. goto out;
  985. case -EAGAIN:
  986. retry++;
  987. break;
  988. case MIGRATEPAGE_SUCCESS:
  989. nr_succeeded++;
  990. break;
  991. default:
  992. /*
  993. * Permanent failure (-EBUSY, -ENOSYS, etc.):
  994. * unlike -EAGAIN case, the failed page is
  995. * removed from migration page list and not
  996. * retried in the next outer loop.
  997. */
  998. nr_failed++;
  999. break;
  1000. }
  1001. }
  1002. }
  1003. rc = nr_failed + retry;
  1004. out:
  1005. if (nr_succeeded)
  1006. count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
  1007. if (nr_failed)
  1008. count_vm_events(PGMIGRATE_FAIL, nr_failed);
  1009. trace_mm_migrate_pages(nr_succeeded, nr_failed, mode, reason);
  1010. if (!swapwrite)
  1011. current->flags &= ~PF_SWAPWRITE;
  1012. return rc;
  1013. }
  1014. #ifdef CONFIG_NUMA
  1015. /*
  1016. * Move a list of individual pages
  1017. */
  1018. struct page_to_node {
  1019. unsigned long addr;
  1020. struct page *page;
  1021. int node;
  1022. int status;
  1023. };
  1024. static struct page *new_page_node(struct page *p, unsigned long private,
  1025. int **result)
  1026. {
  1027. struct page_to_node *pm = (struct page_to_node *)private;
  1028. while (pm->node != MAX_NUMNODES && pm->page != p)
  1029. pm++;
  1030. if (pm->node == MAX_NUMNODES)
  1031. return NULL;
  1032. *result = &pm->status;
  1033. if (PageHuge(p))
  1034. return alloc_huge_page_node(page_hstate(compound_head(p)),
  1035. pm->node);
  1036. else
  1037. return alloc_pages_exact_node(pm->node,
  1038. GFP_HIGHUSER_MOVABLE | __GFP_THISNODE, 0);
  1039. }
  1040. /*
  1041. * Move a set of pages as indicated in the pm array. The addr
  1042. * field must be set to the virtual address of the page to be moved
  1043. * and the node number must contain a valid target node.
  1044. * The pm array ends with node = MAX_NUMNODES.
  1045. */
  1046. static int do_move_page_to_node_array(struct mm_struct *mm,
  1047. struct page_to_node *pm,
  1048. int migrate_all)
  1049. {
  1050. int err;
  1051. struct page_to_node *pp;
  1052. LIST_HEAD(pagelist);
  1053. down_read(&mm->mmap_sem);
  1054. /*
  1055. * Build a list of pages to migrate
  1056. */
  1057. for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
  1058. struct vm_area_struct *vma;
  1059. struct page *page;
  1060. err = -EFAULT;
  1061. vma = find_vma(mm, pp->addr);
  1062. if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
  1063. goto set_status;
  1064. page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT);
  1065. err = PTR_ERR(page);
  1066. if (IS_ERR(page))
  1067. goto set_status;
  1068. err = -ENOENT;
  1069. if (!page)
  1070. goto set_status;
  1071. /* Use PageReserved to check for zero page */
  1072. if (PageReserved(page))
  1073. goto put_and_set;
  1074. pp->page = page;
  1075. err = page_to_nid(page);
  1076. if (err == pp->node)
  1077. /*
  1078. * Node already in the right place
  1079. */
  1080. goto put_and_set;
  1081. err = -EACCES;
  1082. if (page_mapcount(page) > 1 &&
  1083. !migrate_all)
  1084. goto put_and_set;
  1085. if (PageHuge(page)) {
  1086. if (PageHead(page))
  1087. isolate_huge_page(page, &pagelist);
  1088. goto put_and_set;
  1089. }
  1090. err = isolate_lru_page(page);
  1091. if (!err) {
  1092. list_add_tail(&page->lru, &pagelist);
  1093. inc_zone_page_state(page, NR_ISOLATED_ANON +
  1094. page_is_file_cache(page));
  1095. }
  1096. put_and_set:
  1097. /*
  1098. * Either remove the duplicate refcount from
  1099. * isolate_lru_page() or drop the page ref if it was
  1100. * not isolated.
  1101. */
  1102. put_page(page);
  1103. set_status:
  1104. pp->status = err;
  1105. }
  1106. err = 0;
  1107. if (!list_empty(&pagelist)) {
  1108. err = migrate_pages(&pagelist, new_page_node, NULL,
  1109. (unsigned long)pm, MIGRATE_SYNC, MR_SYSCALL);
  1110. if (err)
  1111. putback_movable_pages(&pagelist);
  1112. }
  1113. up_read(&mm->mmap_sem);
  1114. return err;
  1115. }
  1116. /*
  1117. * Migrate an array of page address onto an array of nodes and fill
  1118. * the corresponding array of status.
  1119. */
  1120. static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
  1121. unsigned long nr_pages,
  1122. const void __user * __user *pages,
  1123. const int __user *nodes,
  1124. int __user *status, int flags)
  1125. {
  1126. struct page_to_node *pm;
  1127. unsigned long chunk_nr_pages;
  1128. unsigned long chunk_start;
  1129. int err;
  1130. err = -ENOMEM;
  1131. pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
  1132. if (!pm)
  1133. goto out;
  1134. migrate_prep();
  1135. /*
  1136. * Store a chunk of page_to_node array in a page,
  1137. * but keep the last one as a marker
  1138. */
  1139. chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
  1140. for (chunk_start = 0;
  1141. chunk_start < nr_pages;
  1142. chunk_start += chunk_nr_pages) {
  1143. int j;
  1144. if (chunk_start + chunk_nr_pages > nr_pages)
  1145. chunk_nr_pages = nr_pages - chunk_start;
  1146. /* fill the chunk pm with addrs and nodes from user-space */
  1147. for (j = 0; j < chunk_nr_pages; j++) {
  1148. const void __user *p;
  1149. int node;
  1150. err = -EFAULT;
  1151. if (get_user(p, pages + j + chunk_start))
  1152. goto out_pm;
  1153. pm[j].addr = (unsigned long) p;
  1154. if (get_user(node, nodes + j + chunk_start))
  1155. goto out_pm;
  1156. err = -ENODEV;
  1157. if (node < 0 || node >= MAX_NUMNODES)
  1158. goto out_pm;
  1159. if (!node_state(node, N_MEMORY))
  1160. goto out_pm;
  1161. err = -EACCES;
  1162. if (!node_isset(node, task_nodes))
  1163. goto out_pm;
  1164. pm[j].node = node;
  1165. }
  1166. /* End marker for this chunk */
  1167. pm[chunk_nr_pages].node = MAX_NUMNODES;
  1168. /* Migrate this chunk */
  1169. err = do_move_page_to_node_array(mm, pm,
  1170. flags & MPOL_MF_MOVE_ALL);
  1171. if (err < 0)
  1172. goto out_pm;
  1173. /* Return status information */
  1174. for (j = 0; j < chunk_nr_pages; j++)
  1175. if (put_user(pm[j].status, status + j + chunk_start)) {
  1176. err = -EFAULT;
  1177. goto out_pm;
  1178. }
  1179. }
  1180. err = 0;
  1181. out_pm:
  1182. free_page((unsigned long)pm);
  1183. out:
  1184. return err;
  1185. }
  1186. /*
  1187. * Determine the nodes of an array of pages and store it in an array of status.
  1188. */
  1189. static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
  1190. const void __user **pages, int *status)
  1191. {
  1192. unsigned long i;
  1193. down_read(&mm->mmap_sem);
  1194. for (i = 0; i < nr_pages; i++) {
  1195. unsigned long addr = (unsigned long)(*pages);
  1196. struct vm_area_struct *vma;
  1197. struct page *page;
  1198. int err = -EFAULT;
  1199. vma = find_vma(mm, addr);
  1200. if (!vma || addr < vma->vm_start)
  1201. goto set_status;
  1202. page = follow_page(vma, addr, 0);
  1203. err = PTR_ERR(page);
  1204. if (IS_ERR(page))
  1205. goto set_status;
  1206. err = -ENOENT;
  1207. /* Use PageReserved to check for zero page */
  1208. if (!page || PageReserved(page))
  1209. goto set_status;
  1210. err = page_to_nid(page);
  1211. set_status:
  1212. *status = err;
  1213. pages++;
  1214. status++;
  1215. }
  1216. up_read(&mm->mmap_sem);
  1217. }
  1218. /*
  1219. * Determine the nodes of a user array of pages and store it in
  1220. * a user array of status.
  1221. */
  1222. static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
  1223. const void __user * __user *pages,
  1224. int __user *status)
  1225. {
  1226. #define DO_PAGES_STAT_CHUNK_NR 16
  1227. const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
  1228. int chunk_status[DO_PAGES_STAT_CHUNK_NR];
  1229. while (nr_pages) {
  1230. unsigned long chunk_nr;
  1231. chunk_nr = nr_pages;
  1232. if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
  1233. chunk_nr = DO_PAGES_STAT_CHUNK_NR;
  1234. if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
  1235. break;
  1236. do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
  1237. if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
  1238. break;
  1239. pages += chunk_nr;
  1240. status += chunk_nr;
  1241. nr_pages -= chunk_nr;
  1242. }
  1243. return nr_pages ? -EFAULT : 0;
  1244. }
  1245. /*
  1246. * Move a list of pages in the address space of the currently executing
  1247. * process.
  1248. */
  1249. SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
  1250. const void __user * __user *, pages,
  1251. const int __user *, nodes,
  1252. int __user *, status, int, flags)
  1253. {
  1254. const struct cred *cred = current_cred(), *tcred;
  1255. struct task_struct *task;
  1256. struct mm_struct *mm;
  1257. int err;
  1258. nodemask_t task_nodes;
  1259. /* Check flags */
  1260. if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
  1261. return -EINVAL;
  1262. if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
  1263. return -EPERM;
  1264. /* Find the mm_struct */
  1265. rcu_read_lock();
  1266. task = pid ? find_task_by_vpid(pid) : current;
  1267. if (!task) {
  1268. rcu_read_unlock();
  1269. return -ESRCH;
  1270. }
  1271. get_task_struct(task);
  1272. /*
  1273. * Check if this process has the right to modify the specified
  1274. * process. The right exists if the process has administrative
  1275. * capabilities, superuser privileges or the same
  1276. * userid as the target process.
  1277. */
  1278. tcred = __task_cred(task);
  1279. if (!uid_eq(cred->euid, tcred->suid) && !uid_eq(cred->euid, tcred->uid) &&
  1280. !uid_eq(cred->uid, tcred->suid) && !uid_eq(cred->uid, tcred->uid) &&
  1281. !capable(CAP_SYS_NICE)) {
  1282. rcu_read_unlock();
  1283. err = -EPERM;
  1284. goto out;
  1285. }
  1286. rcu_read_unlock();
  1287. err = security_task_movememory(task);
  1288. if (err)
  1289. goto out;
  1290. task_nodes = cpuset_mems_allowed(task);
  1291. mm = get_task_mm(task);
  1292. put_task_struct(task);
  1293. if (!mm)
  1294. return -EINVAL;
  1295. if (nodes)
  1296. err = do_pages_move(mm, task_nodes, nr_pages, pages,
  1297. nodes, status, flags);
  1298. else
  1299. err = do_pages_stat(mm, nr_pages, pages, status);
  1300. mmput(mm);
  1301. return err;
  1302. out:
  1303. put_task_struct(task);
  1304. return err;
  1305. }
  1306. #ifdef CONFIG_NUMA_BALANCING
  1307. /*
  1308. * Returns true if this is a safe migration target node for misplaced NUMA
  1309. * pages. Currently it only checks the watermarks which crude
  1310. */
  1311. static bool migrate_balanced_pgdat(struct pglist_data *pgdat,
  1312. unsigned long nr_migrate_pages)
  1313. {
  1314. int z;
  1315. for (z = pgdat->nr_zones - 1; z >= 0; z--) {
  1316. struct zone *zone = pgdat->node_zones + z;
  1317. if (!populated_zone(zone))
  1318. continue;
  1319. if (!zone_reclaimable(zone))
  1320. continue;
  1321. /* Avoid waking kswapd by allocating pages_to_migrate pages. */
  1322. if (!zone_watermark_ok(zone, 0,
  1323. high_wmark_pages(zone) +
  1324. nr_migrate_pages,
  1325. 0, 0))
  1326. continue;
  1327. return true;
  1328. }
  1329. return false;
  1330. }
  1331. static struct page *alloc_misplaced_dst_page(struct page *page,
  1332. unsigned long data,
  1333. int **result)
  1334. {
  1335. int nid = (int) data;
  1336. struct page *newpage;
  1337. newpage = alloc_pages_exact_node(nid,
  1338. (GFP_HIGHUSER_MOVABLE |
  1339. __GFP_THISNODE | __GFP_NOMEMALLOC |
  1340. __GFP_NORETRY | __GFP_NOWARN) &
  1341. ~GFP_IOFS, 0);
  1342. return newpage;
  1343. }
  1344. /*
  1345. * page migration rate limiting control.
  1346. * Do not migrate more than @pages_to_migrate in a @migrate_interval_millisecs
  1347. * window of time. Default here says do not migrate more than 1280M per second.
  1348. */
  1349. static unsigned int migrate_interval_millisecs __read_mostly = 100;
  1350. static unsigned int ratelimit_pages __read_mostly = 128 << (20 - PAGE_SHIFT);
  1351. /* Returns true if the node is migrate rate-limited after the update */
  1352. static bool numamigrate_update_ratelimit(pg_data_t *pgdat,
  1353. unsigned long nr_pages)
  1354. {
  1355. /*
  1356. * Rate-limit the amount of data that is being migrated to a node.
  1357. * Optimal placement is no good if the memory bus is saturated and
  1358. * all the time is being spent migrating!
  1359. */
  1360. if (time_after(jiffies, pgdat->numabalancing_migrate_next_window)) {
  1361. spin_lock(&pgdat->numabalancing_migrate_lock);
  1362. pgdat->numabalancing_migrate_nr_pages = 0;
  1363. pgdat->numabalancing_migrate_next_window = jiffies +
  1364. msecs_to_jiffies(migrate_interval_millisecs);
  1365. spin_unlock(&pgdat->numabalancing_migrate_lock);
  1366. }
  1367. if (pgdat->numabalancing_migrate_nr_pages > ratelimit_pages) {
  1368. trace_mm_numa_migrate_ratelimit(current, pgdat->node_id,
  1369. nr_pages);
  1370. return true;
  1371. }
  1372. /*
  1373. * This is an unlocked non-atomic update so errors are possible.
  1374. * The consequences are failing to migrate when we potentiall should
  1375. * have which is not severe enough to warrant locking. If it is ever
  1376. * a problem, it can be converted to a per-cpu counter.
  1377. */
  1378. pgdat->numabalancing_migrate_nr_pages += nr_pages;
  1379. return false;
  1380. }
  1381. static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
  1382. {
  1383. int page_lru;
  1384. VM_BUG_ON_PAGE(compound_order(page) && !PageTransHuge(page), page);
  1385. /* Avoid migrating to a node that is nearly full */
  1386. if (!migrate_balanced_pgdat(pgdat, 1UL << compound_order(page)))
  1387. return 0;
  1388. if (isolate_lru_page(page))
  1389. return 0;
  1390. /*
  1391. * migrate_misplaced_transhuge_page() skips page migration's usual
  1392. * check on page_count(), so we must do it here, now that the page
  1393. * has been isolated: a GUP pin, or any other pin, prevents migration.
  1394. * The expected page count is 3: 1 for page's mapcount and 1 for the
  1395. * caller's pin and 1 for the reference taken by isolate_lru_page().
  1396. */
  1397. if (PageTransHuge(page) && page_count(page) != 3) {
  1398. putback_lru_page(page);
  1399. return 0;
  1400. }
  1401. page_lru = page_is_file_cache(page);
  1402. mod_zone_page_state(page_zone(page), NR_ISOLATED_ANON + page_lru,
  1403. hpage_nr_pages(page));
  1404. /*
  1405. * Isolating the page has taken another reference, so the
  1406. * caller's reference can be safely dropped without the page
  1407. * disappearing underneath us during migration.
  1408. */
  1409. put_page(page);
  1410. return 1;
  1411. }
  1412. bool pmd_trans_migrating(pmd_t pmd)
  1413. {
  1414. struct page *page = pmd_page(pmd);
  1415. return PageLocked(page);
  1416. }
  1417. /*
  1418. * Attempt to migrate a misplaced page to the specified destination
  1419. * node. Caller is expected to have an elevated reference count on
  1420. * the page that will be dropped by this function before returning.
  1421. */
  1422. int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma,
  1423. int node)
  1424. {
  1425. pg_data_t *pgdat = NODE_DATA(node);
  1426. int isolated;
  1427. int nr_remaining;
  1428. LIST_HEAD(migratepages);
  1429. /*
  1430. * Don't migrate file pages that are mapped in multiple processes
  1431. * with execute permissions as they are probably shared libraries.
  1432. */
  1433. if (page_mapcount(page) != 1 && page_is_file_cache(page) &&
  1434. (vma->vm_flags & VM_EXEC))
  1435. goto out;
  1436. /*
  1437. * Rate-limit the amount of data that is being migrated to a node.
  1438. * Optimal placement is no good if the memory bus is saturated and
  1439. * all the time is being spent migrating!
  1440. */
  1441. if (numamigrate_update_ratelimit(pgdat, 1))
  1442. goto out;
  1443. isolated = numamigrate_isolate_page(pgdat, page);
  1444. if (!isolated)
  1445. goto out;
  1446. list_add(&page->lru, &migratepages);
  1447. nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_page,
  1448. NULL, node, MIGRATE_ASYNC,
  1449. MR_NUMA_MISPLACED);
  1450. if (nr_remaining) {
  1451. if (!list_empty(&migratepages)) {
  1452. list_del(&page->lru);
  1453. dec_zone_page_state(page, NR_ISOLATED_ANON +
  1454. page_is_file_cache(page));
  1455. putback_lru_page(page);
  1456. }
  1457. isolated = 0;
  1458. } else
  1459. count_vm_numa_event(NUMA_PAGE_MIGRATE);
  1460. BUG_ON(!list_empty(&migratepages));
  1461. return isolated;
  1462. out:
  1463. put_page(page);
  1464. return 0;
  1465. }
  1466. #endif /* CONFIG_NUMA_BALANCING */
  1467. #if defined(CONFIG_NUMA_BALANCING) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
  1468. /*
  1469. * Migrates a THP to a given target node. page must be locked and is unlocked
  1470. * before returning.
  1471. */
  1472. int migrate_misplaced_transhuge_page(struct mm_struct *mm,
  1473. struct vm_area_struct *vma,
  1474. pmd_t *pmd, pmd_t entry,
  1475. unsigned long address,
  1476. struct page *page, int node)
  1477. {
  1478. spinlock_t *ptl;
  1479. pg_data_t *pgdat = NODE_DATA(node);
  1480. int isolated = 0;
  1481. struct page *new_page = NULL;
  1482. int page_lru = page_is_file_cache(page);
  1483. unsigned long mmun_start = address & HPAGE_PMD_MASK;
  1484. unsigned long mmun_end = mmun_start + HPAGE_PMD_SIZE;
  1485. pmd_t orig_entry;
  1486. /*
  1487. * Rate-limit the amount of data that is being migrated to a node.
  1488. * Optimal placement is no good if the memory bus is saturated and
  1489. * all the time is being spent migrating!
  1490. */
  1491. if (numamigrate_update_ratelimit(pgdat, HPAGE_PMD_NR))
  1492. goto out_dropref;
  1493. new_page = alloc_pages_node(node,
  1494. (GFP_TRANSHUGE | __GFP_THISNODE) & ~__GFP_WAIT,
  1495. HPAGE_PMD_ORDER);
  1496. if (!new_page)
  1497. goto out_fail;
  1498. isolated = numamigrate_isolate_page(pgdat, page);
  1499. if (!isolated) {
  1500. put_page(new_page);
  1501. goto out_fail;
  1502. }
  1503. if (mm_tlb_flush_pending(mm))
  1504. flush_tlb_range(vma, mmun_start, mmun_end);
  1505. /* Prepare a page as a migration target */
  1506. __set_page_locked(new_page);
  1507. SetPageSwapBacked(new_page);
  1508. /* anon mapping, we can simply copy page->mapping to the new page: */
  1509. new_page->mapping = page->mapping;
  1510. new_page->index = page->index;
  1511. migrate_page_copy(new_page, page);
  1512. WARN_ON(PageLRU(new_page));
  1513. /* Recheck the target PMD */
  1514. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  1515. ptl = pmd_lock(mm, pmd);
  1516. if (unlikely(!pmd_same(*pmd, entry) || page_count(page) != 2)) {
  1517. fail_putback:
  1518. spin_unlock(ptl);
  1519. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  1520. /* Reverse changes made by migrate_page_copy() */
  1521. if (TestClearPageActive(new_page))
  1522. SetPageActive(page);
  1523. if (TestClearPageUnevictable(new_page))
  1524. SetPageUnevictable(page);
  1525. mlock_migrate_page(page, new_page);
  1526. unlock_page(new_page);
  1527. put_page(new_page); /* Free it */
  1528. /* Retake the callers reference and putback on LRU */
  1529. get_page(page);
  1530. putback_lru_page(page);
  1531. mod_zone_page_state(page_zone(page),
  1532. NR_ISOLATED_ANON + page_lru, -HPAGE_PMD_NR);
  1533. goto out_unlock;
  1534. }
  1535. orig_entry = *pmd;
  1536. entry = mk_pmd(new_page, vma->vm_page_prot);
  1537. entry = pmd_mkhuge(entry);
  1538. entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
  1539. /*
  1540. * Clear the old entry under pagetable lock and establish the new PTE.
  1541. * Any parallel GUP will either observe the old page blocking on the
  1542. * page lock, block on the page table lock or observe the new page.
  1543. * The SetPageUptodate on the new page and page_add_new_anon_rmap
  1544. * guarantee the copy is visible before the pagetable update.
  1545. */
  1546. flush_cache_range(vma, mmun_start, mmun_end);
  1547. page_add_anon_rmap(new_page, vma, mmun_start);
  1548. pmdp_huge_clear_flush_notify(vma, mmun_start, pmd);
  1549. set_pmd_at(mm, mmun_start, pmd, entry);
  1550. flush_tlb_range(vma, mmun_start, mmun_end);
  1551. update_mmu_cache_pmd(vma, address, &entry);
  1552. if (page_count(page) != 2) {
  1553. set_pmd_at(mm, mmun_start, pmd, orig_entry);
  1554. flush_tlb_range(vma, mmun_start, mmun_end);
  1555. mmu_notifier_invalidate_range(mm, mmun_start, mmun_end);
  1556. update_mmu_cache_pmd(vma, address, &entry);
  1557. page_remove_rmap(new_page);
  1558. goto fail_putback;
  1559. }
  1560. mem_cgroup_migrate(page, new_page, false);
  1561. page_remove_rmap(page);
  1562. spin_unlock(ptl);
  1563. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  1564. /* Take an "isolate" reference and put new page on the LRU. */
  1565. get_page(new_page);
  1566. putback_lru_page(new_page);
  1567. unlock_page(new_page);
  1568. unlock_page(page);
  1569. put_page(page); /* Drop the rmap reference */
  1570. put_page(page); /* Drop the LRU isolation reference */
  1571. count_vm_events(PGMIGRATE_SUCCESS, HPAGE_PMD_NR);
  1572. count_vm_numa_events(NUMA_PAGE_MIGRATE, HPAGE_PMD_NR);
  1573. mod_zone_page_state(page_zone(page),
  1574. NR_ISOLATED_ANON + page_lru,
  1575. -HPAGE_PMD_NR);
  1576. return isolated;
  1577. out_fail:
  1578. count_vm_events(PGMIGRATE_FAIL, HPAGE_PMD_NR);
  1579. out_dropref:
  1580. ptl = pmd_lock(mm, pmd);
  1581. if (pmd_same(*pmd, entry)) {
  1582. entry = pmd_modify(entry, vma->vm_page_prot);
  1583. set_pmd_at(mm, mmun_start, pmd, entry);
  1584. update_mmu_cache_pmd(vma, address, &entry);
  1585. }
  1586. spin_unlock(ptl);
  1587. out_unlock:
  1588. unlock_page(page);
  1589. put_page(page);
  1590. return 0;
  1591. }
  1592. #endif /* CONFIG_NUMA_BALANCING */
  1593. #endif /* CONFIG_NUMA */