migrate.c 50 KB

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