migrate.c 49 KB

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