migrate.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Memory Migration functionality - linux/mm/migrate.c
  4. *
  5. * Copyright (C) 2006 Silicon Graphics, Inc., Christoph Lameter
  6. *
  7. * Page migration was first developed in the context of the memory hotplug
  8. * project. The main authors of the migration code are:
  9. *
  10. * IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
  11. * Hirokazu Takahashi <taka@valinux.co.jp>
  12. * Dave Hansen <haveblue@us.ibm.com>
  13. * Christoph Lameter
  14. */
  15. #include <linux/migrate.h>
  16. #include <linux/export.h>
  17. #include <linux/swap.h>
  18. #include <linux/swapops.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/mm_inline.h>
  22. #include <linux/nsproxy.h>
  23. #include <linux/pagevec.h>
  24. #include <linux/ksm.h>
  25. #include <linux/rmap.h>
  26. #include <linux/topology.h>
  27. #include <linux/cpu.h>
  28. #include <linux/cpuset.h>
  29. #include <linux/writeback.h>
  30. #include <linux/mempolicy.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/security.h>
  33. #include <linux/backing-dev.h>
  34. #include <linux/compaction.h>
  35. #include <linux/syscalls.h>
  36. #include <linux/compat.h>
  37. #include <linux/hugetlb.h>
  38. #include <linux/hugetlb_cgroup.h>
  39. #include <linux/gfp.h>
  40. #include <linux/pfn_t.h>
  41. #include <linux/memremap.h>
  42. #include <linux/userfaultfd_k.h>
  43. #include <linux/balloon_compaction.h>
  44. #include <linux/mmu_notifier.h>
  45. #include <linux/page_idle.h>
  46. #include <linux/page_owner.h>
  47. #include <linux/sched/mm.h>
  48. #include <linux/ptrace.h>
  49. #include <asm/tlbflush.h>
  50. #define CREATE_TRACE_POINTS
  51. #include <trace/events/migrate.h>
  52. #include "internal.h"
  53. /*
  54. * migrate_prep() needs to be called before we start compiling a list of pages
  55. * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is
  56. * undesirable, use migrate_prep_local()
  57. */
  58. int migrate_prep(void)
  59. {
  60. /*
  61. * Clear the LRU lists so pages can be isolated.
  62. * Note that pages may be moved off the LRU after we have
  63. * drained them. Those pages will fail to migrate like other
  64. * pages that may be busy.
  65. */
  66. lru_add_drain_all();
  67. return 0;
  68. }
  69. /* Do the necessary work of migrate_prep but not if it involves other CPUs */
  70. int migrate_prep_local(void)
  71. {
  72. lru_add_drain();
  73. return 0;
  74. }
  75. int isolate_movable_page(struct page *page, isolate_mode_t mode)
  76. {
  77. struct address_space *mapping;
  78. /*
  79. * Avoid burning cycles with pages that are yet under __free_pages(),
  80. * or just got freed under us.
  81. *
  82. * In case we 'win' a race for a movable page being freed under us and
  83. * raise its refcount preventing __free_pages() from doing its job
  84. * the put_page() at the end of this block will take care of
  85. * release this page, thus avoiding a nasty leakage.
  86. */
  87. if (unlikely(!get_page_unless_zero(page)))
  88. goto out;
  89. /*
  90. * Check PageMovable before holding a PG_lock because page's owner
  91. * assumes anybody doesn't touch PG_lock of newly allocated page
  92. * so unconditionally grapping the lock ruins page's owner side.
  93. */
  94. if (unlikely(!__PageMovable(page)))
  95. goto out_putpage;
  96. /*
  97. * As movable pages are not isolated from LRU lists, concurrent
  98. * compaction threads can race against page migration functions
  99. * as well as race against the releasing a page.
  100. *
  101. * In order to avoid having an already isolated movable page
  102. * being (wrongly) re-isolated while it is under migration,
  103. * or to avoid attempting to isolate pages being released,
  104. * lets be sure we have the page lock
  105. * before proceeding with the movable page isolation steps.
  106. */
  107. if (unlikely(!trylock_page(page)))
  108. goto out_putpage;
  109. if (!PageMovable(page) || PageIsolated(page))
  110. goto out_no_isolated;
  111. mapping = page_mapping(page);
  112. VM_BUG_ON_PAGE(!mapping, page);
  113. if (!mapping->a_ops->isolate_page(page, mode))
  114. goto out_no_isolated;
  115. /* Driver shouldn't use PG_isolated bit of page->flags */
  116. WARN_ON_ONCE(PageIsolated(page));
  117. __SetPageIsolated(page);
  118. unlock_page(page);
  119. return 0;
  120. out_no_isolated:
  121. unlock_page(page);
  122. out_putpage:
  123. put_page(page);
  124. out:
  125. return -EBUSY;
  126. }
  127. /* It should be called on page which is PG_movable */
  128. void putback_movable_page(struct page *page)
  129. {
  130. struct address_space *mapping;
  131. VM_BUG_ON_PAGE(!PageLocked(page), page);
  132. VM_BUG_ON_PAGE(!PageMovable(page), page);
  133. VM_BUG_ON_PAGE(!PageIsolated(page), page);
  134. mapping = page_mapping(page);
  135. mapping->a_ops->putback_page(page);
  136. __ClearPageIsolated(page);
  137. }
  138. /*
  139. * Put previously isolated pages back onto the appropriate lists
  140. * from where they were once taken off for compaction/migration.
  141. *
  142. * This function shall be used whenever the isolated pageset has been
  143. * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range()
  144. * and isolate_huge_page().
  145. */
  146. void putback_movable_pages(struct list_head *l)
  147. {
  148. struct page *page;
  149. struct page *page2;
  150. list_for_each_entry_safe(page, page2, l, lru) {
  151. if (unlikely(PageHuge(page))) {
  152. putback_active_hugepage(page);
  153. continue;
  154. }
  155. list_del(&page->lru);
  156. /*
  157. * We isolated non-lru movable page so here we can use
  158. * __PageMovable because LRU page's mapping cannot have
  159. * PAGE_MAPPING_MOVABLE.
  160. */
  161. if (unlikely(__PageMovable(page))) {
  162. VM_BUG_ON_PAGE(!PageIsolated(page), page);
  163. lock_page(page);
  164. if (PageMovable(page))
  165. putback_movable_page(page);
  166. else
  167. __ClearPageIsolated(page);
  168. unlock_page(page);
  169. put_page(page);
  170. } else {
  171. mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
  172. page_is_file_cache(page), -hpage_nr_pages(page));
  173. putback_lru_page(page);
  174. }
  175. }
  176. }
  177. /*
  178. * Restore a potential migration pte to a working pte entry
  179. */
  180. static bool remove_migration_pte(struct page *page, struct vm_area_struct *vma,
  181. unsigned long addr, void *old)
  182. {
  183. struct page_vma_mapped_walk pvmw = {
  184. .page = old,
  185. .vma = vma,
  186. .address = addr,
  187. .flags = PVMW_SYNC | PVMW_MIGRATION,
  188. };
  189. struct page *new;
  190. pte_t pte;
  191. swp_entry_t entry;
  192. VM_BUG_ON_PAGE(PageTail(page), page);
  193. while (page_vma_mapped_walk(&pvmw)) {
  194. if (PageKsm(page))
  195. new = page;
  196. else
  197. new = page - pvmw.page->index +
  198. linear_page_index(vma, pvmw.address);
  199. #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
  200. /* PMD-mapped THP migration entry */
  201. if (!pvmw.pte) {
  202. VM_BUG_ON_PAGE(PageHuge(page) || !PageTransCompound(page), page);
  203. remove_migration_pmd(&pvmw, new);
  204. continue;
  205. }
  206. #endif
  207. get_page(new);
  208. pte = pte_mkold(mk_pte(new, READ_ONCE(vma->vm_page_prot)));
  209. if (pte_swp_soft_dirty(*pvmw.pte))
  210. pte = pte_mksoft_dirty(pte);
  211. /*
  212. * Recheck VMA as permissions can change since migration started
  213. */
  214. entry = pte_to_swp_entry(*pvmw.pte);
  215. if (is_write_migration_entry(entry))
  216. pte = maybe_mkwrite(pte, vma);
  217. if (unlikely(is_zone_device_page(new))) {
  218. if (is_device_private_page(new)) {
  219. entry = make_device_private_entry(new, pte_write(pte));
  220. pte = swp_entry_to_pte(entry);
  221. } else if (is_device_public_page(new)) {
  222. pte = pte_mkdevmap(pte);
  223. flush_dcache_page(new);
  224. }
  225. } else
  226. flush_dcache_page(new);
  227. #ifdef CONFIG_HUGETLB_PAGE
  228. if (PageHuge(new)) {
  229. pte = pte_mkhuge(pte);
  230. pte = arch_make_huge_pte(pte, vma, new, 0);
  231. set_huge_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
  232. if (PageAnon(new))
  233. hugepage_add_anon_rmap(new, vma, pvmw.address);
  234. else
  235. page_dup_rmap(new, true);
  236. } else
  237. #endif
  238. {
  239. set_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
  240. if (PageAnon(new))
  241. page_add_anon_rmap(new, vma, pvmw.address, false);
  242. else
  243. page_add_file_rmap(new, false);
  244. }
  245. if (vma->vm_flags & VM_LOCKED && !PageTransCompound(new))
  246. mlock_vma_page(new);
  247. if (PageTransHuge(page) && PageMlocked(page))
  248. clear_page_mlock(page);
  249. /* No need to invalidate - it was non-present before */
  250. update_mmu_cache(vma, pvmw.address, pvmw.pte);
  251. }
  252. return true;
  253. }
  254. /*
  255. * Get rid of all migration entries and replace them by
  256. * references to the indicated page.
  257. */
  258. void remove_migration_ptes(struct page *old, struct page *new, bool locked)
  259. {
  260. struct rmap_walk_control rwc = {
  261. .rmap_one = remove_migration_pte,
  262. .arg = old,
  263. };
  264. if (locked)
  265. rmap_walk_locked(new, &rwc);
  266. else
  267. rmap_walk(new, &rwc);
  268. }
  269. /*
  270. * Something used the pte of a page under migration. We need to
  271. * get to the page and wait until migration is finished.
  272. * When we return from this function the fault will be retried.
  273. */
  274. void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
  275. spinlock_t *ptl)
  276. {
  277. pte_t pte;
  278. swp_entry_t entry;
  279. struct page *page;
  280. spin_lock(ptl);
  281. pte = *ptep;
  282. if (!is_swap_pte(pte))
  283. goto out;
  284. entry = pte_to_swp_entry(pte);
  285. if (!is_migration_entry(entry))
  286. goto out;
  287. page = migration_entry_to_page(entry);
  288. /*
  289. * Once page cache replacement of page migration started, page_count
  290. * *must* be zero. And, we don't want to call wait_on_page_locked()
  291. * against a page without get_page().
  292. * So, we use get_page_unless_zero(), here. Even failed, page fault
  293. * will occur again.
  294. */
  295. if (!get_page_unless_zero(page))
  296. goto out;
  297. pte_unmap_unlock(ptep, ptl);
  298. wait_on_page_locked(page);
  299. put_page(page);
  300. return;
  301. out:
  302. pte_unmap_unlock(ptep, ptl);
  303. }
  304. void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
  305. unsigned long address)
  306. {
  307. spinlock_t *ptl = pte_lockptr(mm, pmd);
  308. pte_t *ptep = pte_offset_map(pmd, address);
  309. __migration_entry_wait(mm, ptep, ptl);
  310. }
  311. void migration_entry_wait_huge(struct vm_area_struct *vma,
  312. struct mm_struct *mm, pte_t *pte)
  313. {
  314. spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte);
  315. __migration_entry_wait(mm, pte, ptl);
  316. }
  317. #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
  318. void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd)
  319. {
  320. spinlock_t *ptl;
  321. struct page *page;
  322. ptl = pmd_lock(mm, pmd);
  323. if (!is_pmd_migration_entry(*pmd))
  324. goto unlock;
  325. page = migration_entry_to_page(pmd_to_swp_entry(*pmd));
  326. if (!get_page_unless_zero(page))
  327. goto unlock;
  328. spin_unlock(ptl);
  329. wait_on_page_locked(page);
  330. put_page(page);
  331. return;
  332. unlock:
  333. spin_unlock(ptl);
  334. }
  335. #endif
  336. #ifdef CONFIG_BLOCK
  337. /* Returns true if all buffers are successfully locked */
  338. static bool buffer_migrate_lock_buffers(struct buffer_head *head,
  339. enum migrate_mode mode)
  340. {
  341. struct buffer_head *bh = head;
  342. /* Simple case, sync compaction */
  343. if (mode != MIGRATE_ASYNC) {
  344. do {
  345. get_bh(bh);
  346. lock_buffer(bh);
  347. bh = bh->b_this_page;
  348. } while (bh != head);
  349. return true;
  350. }
  351. /* async case, we cannot block on lock_buffer so use trylock_buffer */
  352. do {
  353. get_bh(bh);
  354. if (!trylock_buffer(bh)) {
  355. /*
  356. * We failed to lock the buffer and cannot stall in
  357. * async migration. Release the taken locks
  358. */
  359. struct buffer_head *failed_bh = bh;
  360. put_bh(failed_bh);
  361. bh = head;
  362. while (bh != failed_bh) {
  363. unlock_buffer(bh);
  364. put_bh(bh);
  365. bh = bh->b_this_page;
  366. }
  367. return false;
  368. }
  369. bh = bh->b_this_page;
  370. } while (bh != head);
  371. return true;
  372. }
  373. #else
  374. static inline bool buffer_migrate_lock_buffers(struct buffer_head *head,
  375. enum migrate_mode mode)
  376. {
  377. return true;
  378. }
  379. #endif /* CONFIG_BLOCK */
  380. /*
  381. * Replace the page in the mapping.
  382. *
  383. * The number of remaining references must be:
  384. * 1 for anonymous pages without a mapping
  385. * 2 for pages with a mapping
  386. * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
  387. */
  388. int migrate_page_move_mapping(struct address_space *mapping,
  389. struct page *newpage, struct page *page,
  390. struct buffer_head *head, enum migrate_mode mode,
  391. int extra_count)
  392. {
  393. XA_STATE(xas, &mapping->i_pages, page_index(page));
  394. struct zone *oldzone, *newzone;
  395. int dirty;
  396. int expected_count = 1 + extra_count;
  397. /*
  398. * Device public or private pages have an extra refcount as they are
  399. * ZONE_DEVICE pages.
  400. */
  401. expected_count += is_device_private_page(page);
  402. expected_count += is_device_public_page(page);
  403. if (!mapping) {
  404. /* Anonymous page without mapping */
  405. if (page_count(page) != expected_count)
  406. return -EAGAIN;
  407. /* No turning back from here */
  408. newpage->index = page->index;
  409. newpage->mapping = page->mapping;
  410. if (PageSwapBacked(page))
  411. __SetPageSwapBacked(newpage);
  412. return MIGRATEPAGE_SUCCESS;
  413. }
  414. oldzone = page_zone(page);
  415. newzone = page_zone(newpage);
  416. xas_lock_irq(&xas);
  417. expected_count += hpage_nr_pages(page) + page_has_private(page);
  418. if (page_count(page) != expected_count || xas_load(&xas) != page) {
  419. xas_unlock_irq(&xas);
  420. return -EAGAIN;
  421. }
  422. if (!page_ref_freeze(page, expected_count)) {
  423. xas_unlock_irq(&xas);
  424. return -EAGAIN;
  425. }
  426. /*
  427. * In the async migration case of moving a page with buffers, lock the
  428. * buffers using trylock before the mapping is moved. If the mapping
  429. * was moved, we later failed to lock the buffers and could not move
  430. * the mapping back due to an elevated page count, we would have to
  431. * block waiting on other references to be dropped.
  432. */
  433. if (mode == MIGRATE_ASYNC && head &&
  434. !buffer_migrate_lock_buffers(head, mode)) {
  435. page_ref_unfreeze(page, expected_count);
  436. xas_unlock_irq(&xas);
  437. return -EAGAIN;
  438. }
  439. /*
  440. * Now we know that no one else is looking at the page:
  441. * no turning back from here.
  442. */
  443. newpage->index = page->index;
  444. newpage->mapping = page->mapping;
  445. page_ref_add(newpage, hpage_nr_pages(page)); /* add cache reference */
  446. if (PageSwapBacked(page)) {
  447. __SetPageSwapBacked(newpage);
  448. if (PageSwapCache(page)) {
  449. SetPageSwapCache(newpage);
  450. set_page_private(newpage, page_private(page));
  451. }
  452. } else {
  453. VM_BUG_ON_PAGE(PageSwapCache(page), page);
  454. }
  455. /* Move dirty while page refs frozen and newpage not yet exposed */
  456. dirty = PageDirty(page);
  457. if (dirty) {
  458. ClearPageDirty(page);
  459. SetPageDirty(newpage);
  460. }
  461. xas_store(&xas, newpage);
  462. if (PageTransHuge(page)) {
  463. int i;
  464. for (i = 1; i < HPAGE_PMD_NR; i++) {
  465. xas_next(&xas);
  466. xas_store(&xas, newpage + i);
  467. }
  468. }
  469. /*
  470. * Drop cache reference from old page by unfreezing
  471. * to one less reference.
  472. * We know this isn't the last reference.
  473. */
  474. page_ref_unfreeze(page, expected_count - hpage_nr_pages(page));
  475. xas_unlock(&xas);
  476. /* Leave irq disabled to prevent preemption while updating stats */
  477. /*
  478. * If moved to a different zone then also account
  479. * the page for that zone. Other VM counters will be
  480. * taken care of when we establish references to the
  481. * new page and drop references to the old page.
  482. *
  483. * Note that anonymous pages are accounted for
  484. * via NR_FILE_PAGES and NR_ANON_MAPPED if they
  485. * are mapped to swap space.
  486. */
  487. if (newzone != oldzone) {
  488. __dec_node_state(oldzone->zone_pgdat, NR_FILE_PAGES);
  489. __inc_node_state(newzone->zone_pgdat, NR_FILE_PAGES);
  490. if (PageSwapBacked(page) && !PageSwapCache(page)) {
  491. __dec_node_state(oldzone->zone_pgdat, NR_SHMEM);
  492. __inc_node_state(newzone->zone_pgdat, NR_SHMEM);
  493. }
  494. if (dirty && mapping_cap_account_dirty(mapping)) {
  495. __dec_node_state(oldzone->zone_pgdat, NR_FILE_DIRTY);
  496. __dec_zone_state(oldzone, NR_ZONE_WRITE_PENDING);
  497. __inc_node_state(newzone->zone_pgdat, NR_FILE_DIRTY);
  498. __inc_zone_state(newzone, NR_ZONE_WRITE_PENDING);
  499. }
  500. }
  501. local_irq_enable();
  502. return MIGRATEPAGE_SUCCESS;
  503. }
  504. EXPORT_SYMBOL(migrate_page_move_mapping);
  505. /*
  506. * The expected number of remaining references is the same as that
  507. * of migrate_page_move_mapping().
  508. */
  509. int migrate_huge_page_move_mapping(struct address_space *mapping,
  510. struct page *newpage, struct page *page)
  511. {
  512. XA_STATE(xas, &mapping->i_pages, page_index(page));
  513. int expected_count;
  514. xas_lock_irq(&xas);
  515. expected_count = 2 + page_has_private(page);
  516. if (page_count(page) != expected_count || xas_load(&xas) != page) {
  517. xas_unlock_irq(&xas);
  518. return -EAGAIN;
  519. }
  520. if (!page_ref_freeze(page, expected_count)) {
  521. xas_unlock_irq(&xas);
  522. return -EAGAIN;
  523. }
  524. newpage->index = page->index;
  525. newpage->mapping = page->mapping;
  526. get_page(newpage);
  527. xas_store(&xas, newpage);
  528. page_ref_unfreeze(page, expected_count - 1);
  529. xas_unlock_irq(&xas);
  530. return MIGRATEPAGE_SUCCESS;
  531. }
  532. /*
  533. * Gigantic pages are so large that we do not guarantee that page++ pointer
  534. * arithmetic will work across the entire page. We need something more
  535. * specialized.
  536. */
  537. static void __copy_gigantic_page(struct page *dst, struct page *src,
  538. int nr_pages)
  539. {
  540. int i;
  541. struct page *dst_base = dst;
  542. struct page *src_base = src;
  543. for (i = 0; i < nr_pages; ) {
  544. cond_resched();
  545. copy_highpage(dst, src);
  546. i++;
  547. dst = mem_map_next(dst, dst_base, i);
  548. src = mem_map_next(src, src_base, i);
  549. }
  550. }
  551. static void copy_huge_page(struct page *dst, struct page *src)
  552. {
  553. int i;
  554. int nr_pages;
  555. if (PageHuge(src)) {
  556. /* hugetlbfs page */
  557. struct hstate *h = page_hstate(src);
  558. nr_pages = pages_per_huge_page(h);
  559. if (unlikely(nr_pages > MAX_ORDER_NR_PAGES)) {
  560. __copy_gigantic_page(dst, src, nr_pages);
  561. return;
  562. }
  563. } else {
  564. /* thp page */
  565. BUG_ON(!PageTransHuge(src));
  566. nr_pages = hpage_nr_pages(src);
  567. }
  568. for (i = 0; i < nr_pages; i++) {
  569. cond_resched();
  570. copy_highpage(dst + i, src + i);
  571. }
  572. }
  573. /*
  574. * Copy the page to its new location
  575. */
  576. void migrate_page_states(struct page *newpage, struct page *page)
  577. {
  578. int cpupid;
  579. if (PageError(page))
  580. SetPageError(newpage);
  581. if (PageReferenced(page))
  582. SetPageReferenced(newpage);
  583. if (PageUptodate(page))
  584. SetPageUptodate(newpage);
  585. if (TestClearPageActive(page)) {
  586. VM_BUG_ON_PAGE(PageUnevictable(page), page);
  587. SetPageActive(newpage);
  588. } else if (TestClearPageUnevictable(page))
  589. SetPageUnevictable(newpage);
  590. if (PageWorkingset(page))
  591. SetPageWorkingset(newpage);
  592. if (PageChecked(page))
  593. SetPageChecked(newpage);
  594. if (PageMappedToDisk(page))
  595. SetPageMappedToDisk(newpage);
  596. /* Move dirty on pages not done by migrate_page_move_mapping() */
  597. if (PageDirty(page))
  598. SetPageDirty(newpage);
  599. if (page_is_young(page))
  600. set_page_young(newpage);
  601. if (page_is_idle(page))
  602. set_page_idle(newpage);
  603. /*
  604. * Copy NUMA information to the new page, to prevent over-eager
  605. * future migrations of this same page.
  606. */
  607. cpupid = page_cpupid_xchg_last(page, -1);
  608. page_cpupid_xchg_last(newpage, cpupid);
  609. ksm_migrate_page(newpage, page);
  610. /*
  611. * Please do not reorder this without considering how mm/ksm.c's
  612. * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache().
  613. */
  614. if (PageSwapCache(page))
  615. ClearPageSwapCache(page);
  616. ClearPagePrivate(page);
  617. set_page_private(page, 0);
  618. /*
  619. * If any waiters have accumulated on the new page then
  620. * wake them up.
  621. */
  622. if (PageWriteback(newpage))
  623. end_page_writeback(newpage);
  624. copy_page_owner(page, newpage);
  625. mem_cgroup_migrate(page, newpage);
  626. }
  627. EXPORT_SYMBOL(migrate_page_states);
  628. void migrate_page_copy(struct page *newpage, struct page *page)
  629. {
  630. if (PageHuge(page) || PageTransHuge(page))
  631. copy_huge_page(newpage, page);
  632. else
  633. copy_highpage(newpage, page);
  634. migrate_page_states(newpage, page);
  635. }
  636. EXPORT_SYMBOL(migrate_page_copy);
  637. /************************************************************
  638. * Migration functions
  639. ***********************************************************/
  640. /*
  641. * Common logic to directly migrate a single LRU page suitable for
  642. * pages that do not use PagePrivate/PagePrivate2.
  643. *
  644. * Pages are locked upon entry and exit.
  645. */
  646. int migrate_page(struct address_space *mapping,
  647. struct page *newpage, struct page *page,
  648. enum migrate_mode mode)
  649. {
  650. int rc;
  651. BUG_ON(PageWriteback(page)); /* Writeback must be complete */
  652. rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode, 0);
  653. if (rc != MIGRATEPAGE_SUCCESS)
  654. return rc;
  655. if (mode != MIGRATE_SYNC_NO_COPY)
  656. migrate_page_copy(newpage, page);
  657. else
  658. migrate_page_states(newpage, page);
  659. return MIGRATEPAGE_SUCCESS;
  660. }
  661. EXPORT_SYMBOL(migrate_page);
  662. #ifdef CONFIG_BLOCK
  663. /*
  664. * Migration function for pages with buffers. This function can only be used
  665. * if the underlying filesystem guarantees that no other references to "page"
  666. * exist.
  667. */
  668. int buffer_migrate_page(struct address_space *mapping,
  669. struct page *newpage, struct page *page, enum migrate_mode mode)
  670. {
  671. struct buffer_head *bh, *head;
  672. int rc;
  673. if (!page_has_buffers(page))
  674. return migrate_page(mapping, newpage, page, mode);
  675. head = page_buffers(page);
  676. rc = migrate_page_move_mapping(mapping, newpage, page, head, mode, 0);
  677. if (rc != MIGRATEPAGE_SUCCESS)
  678. return rc;
  679. /*
  680. * In the async case, migrate_page_move_mapping locked the buffers
  681. * with an IRQ-safe spinlock held. In the sync case, the buffers
  682. * need to be locked now
  683. */
  684. if (mode != MIGRATE_ASYNC)
  685. BUG_ON(!buffer_migrate_lock_buffers(head, mode));
  686. ClearPagePrivate(page);
  687. set_page_private(newpage, page_private(page));
  688. set_page_private(page, 0);
  689. put_page(page);
  690. get_page(newpage);
  691. bh = head;
  692. do {
  693. set_bh_page(bh, newpage, bh_offset(bh));
  694. bh = bh->b_this_page;
  695. } while (bh != head);
  696. SetPagePrivate(newpage);
  697. if (mode != MIGRATE_SYNC_NO_COPY)
  698. migrate_page_copy(newpage, page);
  699. else
  700. migrate_page_states(newpage, page);
  701. bh = head;
  702. do {
  703. unlock_buffer(bh);
  704. put_bh(bh);
  705. bh = bh->b_this_page;
  706. } while (bh != head);
  707. return MIGRATEPAGE_SUCCESS;
  708. }
  709. EXPORT_SYMBOL(buffer_migrate_page);
  710. #endif
  711. /*
  712. * Writeback a page to clean the dirty state
  713. */
  714. static int writeout(struct address_space *mapping, struct page *page)
  715. {
  716. struct writeback_control wbc = {
  717. .sync_mode = WB_SYNC_NONE,
  718. .nr_to_write = 1,
  719. .range_start = 0,
  720. .range_end = LLONG_MAX,
  721. .for_reclaim = 1
  722. };
  723. int rc;
  724. if (!mapping->a_ops->writepage)
  725. /* No write method for the address space */
  726. return -EINVAL;
  727. if (!clear_page_dirty_for_io(page))
  728. /* Someone else already triggered a write */
  729. return -EAGAIN;
  730. /*
  731. * A dirty page may imply that the underlying filesystem has
  732. * the page on some queue. So the page must be clean for
  733. * migration. Writeout may mean we loose the lock and the
  734. * page state is no longer what we checked for earlier.
  735. * At this point we know that the migration attempt cannot
  736. * be successful.
  737. */
  738. remove_migration_ptes(page, page, false);
  739. rc = mapping->a_ops->writepage(page, &wbc);
  740. if (rc != AOP_WRITEPAGE_ACTIVATE)
  741. /* unlocked. Relock */
  742. lock_page(page);
  743. return (rc < 0) ? -EIO : -EAGAIN;
  744. }
  745. /*
  746. * Default handling if a filesystem does not provide a migration function.
  747. */
  748. static int fallback_migrate_page(struct address_space *mapping,
  749. struct page *newpage, struct page *page, enum migrate_mode mode)
  750. {
  751. if (PageDirty(page)) {
  752. /* Only writeback pages in full synchronous migration */
  753. switch (mode) {
  754. case MIGRATE_SYNC:
  755. case MIGRATE_SYNC_NO_COPY:
  756. break;
  757. default:
  758. return -EBUSY;
  759. }
  760. return writeout(mapping, page);
  761. }
  762. /*
  763. * Buffers may be managed in a filesystem specific way.
  764. * We must have no buffers or drop them.
  765. */
  766. if (page_has_private(page) &&
  767. !try_to_release_page(page, GFP_KERNEL))
  768. return -EAGAIN;
  769. return migrate_page(mapping, newpage, page, mode);
  770. }
  771. /*
  772. * Move a page to a newly allocated page
  773. * The page is locked and all ptes have been successfully removed.
  774. *
  775. * The new page will have replaced the old page if this function
  776. * is successful.
  777. *
  778. * Return value:
  779. * < 0 - error code
  780. * MIGRATEPAGE_SUCCESS - success
  781. */
  782. static int move_to_new_page(struct page *newpage, struct page *page,
  783. enum migrate_mode mode)
  784. {
  785. struct address_space *mapping;
  786. int rc = -EAGAIN;
  787. bool is_lru = !__PageMovable(page);
  788. VM_BUG_ON_PAGE(!PageLocked(page), page);
  789. VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
  790. mapping = page_mapping(page);
  791. if (likely(is_lru)) {
  792. if (!mapping)
  793. rc = migrate_page(mapping, newpage, page, mode);
  794. else if (mapping->a_ops->migratepage)
  795. /*
  796. * Most pages have a mapping and most filesystems
  797. * provide a migratepage callback. Anonymous pages
  798. * are part of swap space which also has its own
  799. * migratepage callback. This is the most common path
  800. * for page migration.
  801. */
  802. rc = mapping->a_ops->migratepage(mapping, newpage,
  803. page, mode);
  804. else
  805. rc = fallback_migrate_page(mapping, newpage,
  806. page, mode);
  807. } else {
  808. /*
  809. * In case of non-lru page, it could be released after
  810. * isolation step. In that case, we shouldn't try migration.
  811. */
  812. VM_BUG_ON_PAGE(!PageIsolated(page), page);
  813. if (!PageMovable(page)) {
  814. rc = MIGRATEPAGE_SUCCESS;
  815. __ClearPageIsolated(page);
  816. goto out;
  817. }
  818. rc = mapping->a_ops->migratepage(mapping, newpage,
  819. page, mode);
  820. WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS &&
  821. !PageIsolated(page));
  822. }
  823. /*
  824. * When successful, old pagecache page->mapping must be cleared before
  825. * page is freed; but stats require that PageAnon be left as PageAnon.
  826. */
  827. if (rc == MIGRATEPAGE_SUCCESS) {
  828. if (__PageMovable(page)) {
  829. VM_BUG_ON_PAGE(!PageIsolated(page), page);
  830. /*
  831. * We clear PG_movable under page_lock so any compactor
  832. * cannot try to migrate this page.
  833. */
  834. __ClearPageIsolated(page);
  835. }
  836. /*
  837. * Anonymous and movable page->mapping will be cleard by
  838. * free_pages_prepare so don't reset it here for keeping
  839. * the type to work PageAnon, for example.
  840. */
  841. if (!PageMappingFlags(page))
  842. page->mapping = NULL;
  843. }
  844. out:
  845. return rc;
  846. }
  847. static int __unmap_and_move(struct page *page, struct page *newpage,
  848. int force, enum migrate_mode mode)
  849. {
  850. int rc = -EAGAIN;
  851. int page_was_mapped = 0;
  852. struct anon_vma *anon_vma = NULL;
  853. bool is_lru = !__PageMovable(page);
  854. if (!trylock_page(page)) {
  855. if (!force || mode == MIGRATE_ASYNC)
  856. goto out;
  857. /*
  858. * It's not safe for direct compaction to call lock_page.
  859. * For example, during page readahead pages are added locked
  860. * to the LRU. Later, when the IO completes the pages are
  861. * marked uptodate and unlocked. However, the queueing
  862. * could be merging multiple pages for one bio (e.g.
  863. * mpage_readpages). If an allocation happens for the
  864. * second or third page, the process can end up locking
  865. * the same page twice and deadlocking. Rather than
  866. * trying to be clever about what pages can be locked,
  867. * avoid the use of lock_page for direct compaction
  868. * altogether.
  869. */
  870. if (current->flags & PF_MEMALLOC)
  871. goto out;
  872. lock_page(page);
  873. }
  874. if (PageWriteback(page)) {
  875. /*
  876. * Only in the case of a full synchronous migration is it
  877. * necessary to wait for PageWriteback. In the async case,
  878. * the retry loop is too short and in the sync-light case,
  879. * the overhead of stalling is too much
  880. */
  881. switch (mode) {
  882. case MIGRATE_SYNC:
  883. case MIGRATE_SYNC_NO_COPY:
  884. break;
  885. default:
  886. rc = -EBUSY;
  887. goto out_unlock;
  888. }
  889. if (!force)
  890. goto out_unlock;
  891. wait_on_page_writeback(page);
  892. }
  893. /*
  894. * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
  895. * we cannot notice that anon_vma is freed while we migrates a page.
  896. * This get_anon_vma() delays freeing anon_vma pointer until the end
  897. * of migration. File cache pages are no problem because of page_lock()
  898. * File Caches may use write_page() or lock_page() in migration, then,
  899. * just care Anon page here.
  900. *
  901. * Only page_get_anon_vma() understands the subtleties of
  902. * getting a hold on an anon_vma from outside one of its mms.
  903. * But if we cannot get anon_vma, then we won't need it anyway,
  904. * because that implies that the anon page is no longer mapped
  905. * (and cannot be remapped so long as we hold the page lock).
  906. */
  907. if (PageAnon(page) && !PageKsm(page))
  908. anon_vma = page_get_anon_vma(page);
  909. /*
  910. * Block others from accessing the new page when we get around to
  911. * establishing additional references. We are usually the only one
  912. * holding a reference to newpage at this point. We used to have a BUG
  913. * here if trylock_page(newpage) fails, but would like to allow for
  914. * cases where there might be a race with the previous use of newpage.
  915. * This is much like races on refcount of oldpage: just don't BUG().
  916. */
  917. if (unlikely(!trylock_page(newpage)))
  918. goto out_unlock;
  919. if (unlikely(!is_lru)) {
  920. rc = move_to_new_page(newpage, page, mode);
  921. goto out_unlock_both;
  922. }
  923. /*
  924. * Corner case handling:
  925. * 1. When a new swap-cache page is read into, it is added to the LRU
  926. * and treated as swapcache but it has no rmap yet.
  927. * Calling try_to_unmap() against a page->mapping==NULL page will
  928. * trigger a BUG. So handle it here.
  929. * 2. An orphaned page (see truncate_complete_page) might have
  930. * fs-private metadata. The page can be picked up due to memory
  931. * offlining. Everywhere else except page reclaim, the page is
  932. * invisible to the vm, so the page can not be migrated. So try to
  933. * free the metadata, so the page can be freed.
  934. */
  935. if (!page->mapping) {
  936. VM_BUG_ON_PAGE(PageAnon(page), page);
  937. if (page_has_private(page)) {
  938. try_to_free_buffers(page);
  939. goto out_unlock_both;
  940. }
  941. } else if (page_mapped(page)) {
  942. /* Establish migration ptes */
  943. VM_BUG_ON_PAGE(PageAnon(page) && !PageKsm(page) && !anon_vma,
  944. page);
  945. try_to_unmap(page,
  946. TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
  947. page_was_mapped = 1;
  948. }
  949. if (!page_mapped(page))
  950. rc = move_to_new_page(newpage, page, mode);
  951. if (page_was_mapped)
  952. remove_migration_ptes(page,
  953. rc == MIGRATEPAGE_SUCCESS ? newpage : page, false);
  954. out_unlock_both:
  955. unlock_page(newpage);
  956. out_unlock:
  957. /* Drop an anon_vma reference if we took one */
  958. if (anon_vma)
  959. put_anon_vma(anon_vma);
  960. unlock_page(page);
  961. out:
  962. /*
  963. * If migration is successful, decrease refcount of the newpage
  964. * which will not free the page because new page owner increased
  965. * refcounter. As well, if it is LRU page, add the page to LRU
  966. * list in here.
  967. */
  968. if (rc == MIGRATEPAGE_SUCCESS) {
  969. if (unlikely(__PageMovable(newpage)))
  970. put_page(newpage);
  971. else
  972. putback_lru_page(newpage);
  973. }
  974. return rc;
  975. }
  976. /*
  977. * gcc 4.7 and 4.8 on arm get an ICEs when inlining unmap_and_move(). Work
  978. * around it.
  979. */
  980. #if defined(CONFIG_ARM) && \
  981. defined(GCC_VERSION) && GCC_VERSION < 40900 && GCC_VERSION >= 40700
  982. #define ICE_noinline noinline
  983. #else
  984. #define ICE_noinline
  985. #endif
  986. /*
  987. * Obtain the lock on page, remove all ptes and migrate the page
  988. * to the newly allocated page in newpage.
  989. */
  990. static ICE_noinline int unmap_and_move(new_page_t get_new_page,
  991. free_page_t put_new_page,
  992. unsigned long private, struct page *page,
  993. int force, enum migrate_mode mode,
  994. enum migrate_reason reason)
  995. {
  996. int rc = MIGRATEPAGE_SUCCESS;
  997. struct page *newpage;
  998. if (!thp_migration_supported() && PageTransHuge(page))
  999. return -ENOMEM;
  1000. newpage = get_new_page(page, private);
  1001. if (!newpage)
  1002. return -ENOMEM;
  1003. if (page_count(page) == 1) {
  1004. /* page was freed from under us. So we are done. */
  1005. ClearPageActive(page);
  1006. ClearPageUnevictable(page);
  1007. if (unlikely(__PageMovable(page))) {
  1008. lock_page(page);
  1009. if (!PageMovable(page))
  1010. __ClearPageIsolated(page);
  1011. unlock_page(page);
  1012. }
  1013. if (put_new_page)
  1014. put_new_page(newpage, private);
  1015. else
  1016. put_page(newpage);
  1017. goto out;
  1018. }
  1019. rc = __unmap_and_move(page, newpage, force, mode);
  1020. if (rc == MIGRATEPAGE_SUCCESS)
  1021. set_page_owner_migrate_reason(newpage, reason);
  1022. out:
  1023. if (rc != -EAGAIN) {
  1024. /*
  1025. * A page that has been migrated has all references
  1026. * removed and will be freed. A page that has not been
  1027. * migrated will have kepts its references and be
  1028. * restored.
  1029. */
  1030. list_del(&page->lru);
  1031. /*
  1032. * Compaction can migrate also non-LRU pages which are
  1033. * not accounted to NR_ISOLATED_*. They can be recognized
  1034. * as __PageMovable
  1035. */
  1036. if (likely(!__PageMovable(page)))
  1037. mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
  1038. page_is_file_cache(page), -hpage_nr_pages(page));
  1039. }
  1040. /*
  1041. * If migration is successful, releases reference grabbed during
  1042. * isolation. Otherwise, restore the page to right list unless
  1043. * we want to retry.
  1044. */
  1045. if (rc == MIGRATEPAGE_SUCCESS) {
  1046. put_page(page);
  1047. if (reason == MR_MEMORY_FAILURE) {
  1048. /*
  1049. * Set PG_HWPoison on just freed page
  1050. * intentionally. Although it's rather weird,
  1051. * it's how HWPoison flag works at the moment.
  1052. */
  1053. if (set_hwpoison_free_buddy_page(page))
  1054. num_poisoned_pages_inc();
  1055. }
  1056. } else {
  1057. if (rc != -EAGAIN) {
  1058. if (likely(!__PageMovable(page))) {
  1059. putback_lru_page(page);
  1060. goto put_new;
  1061. }
  1062. lock_page(page);
  1063. if (PageMovable(page))
  1064. putback_movable_page(page);
  1065. else
  1066. __ClearPageIsolated(page);
  1067. unlock_page(page);
  1068. put_page(page);
  1069. }
  1070. put_new:
  1071. if (put_new_page)
  1072. put_new_page(newpage, private);
  1073. else
  1074. put_page(newpage);
  1075. }
  1076. return rc;
  1077. }
  1078. /*
  1079. * Counterpart of unmap_and_move_page() for hugepage migration.
  1080. *
  1081. * This function doesn't wait the completion of hugepage I/O
  1082. * because there is no race between I/O and migration for hugepage.
  1083. * Note that currently hugepage I/O occurs only in direct I/O
  1084. * where no lock is held and PG_writeback is irrelevant,
  1085. * and writeback status of all subpages are counted in the reference
  1086. * count of the head page (i.e. if all subpages of a 2MB hugepage are
  1087. * under direct I/O, the reference of the head page is 512 and a bit more.)
  1088. * This means that when we try to migrate hugepage whose subpages are
  1089. * doing direct I/O, some references remain after try_to_unmap() and
  1090. * hugepage migration fails without data corruption.
  1091. *
  1092. * There is also no race when direct I/O is issued on the page under migration,
  1093. * because then pte is replaced with migration swap entry and direct I/O code
  1094. * will wait in the page fault for migration to complete.
  1095. */
  1096. static int unmap_and_move_huge_page(new_page_t get_new_page,
  1097. free_page_t put_new_page, unsigned long private,
  1098. struct page *hpage, int force,
  1099. enum migrate_mode mode, int reason)
  1100. {
  1101. int rc = -EAGAIN;
  1102. int page_was_mapped = 0;
  1103. struct page *new_hpage;
  1104. struct anon_vma *anon_vma = NULL;
  1105. /*
  1106. * Movability of hugepages depends on architectures and hugepage size.
  1107. * This check is necessary because some callers of hugepage migration
  1108. * like soft offline and memory hotremove don't walk through page
  1109. * tables or check whether the hugepage is pmd-based or not before
  1110. * kicking migration.
  1111. */
  1112. if (!hugepage_migration_supported(page_hstate(hpage))) {
  1113. putback_active_hugepage(hpage);
  1114. return -ENOSYS;
  1115. }
  1116. new_hpage = get_new_page(hpage, private);
  1117. if (!new_hpage)
  1118. return -ENOMEM;
  1119. if (!trylock_page(hpage)) {
  1120. if (!force)
  1121. goto out;
  1122. switch (mode) {
  1123. case MIGRATE_SYNC:
  1124. case MIGRATE_SYNC_NO_COPY:
  1125. break;
  1126. default:
  1127. goto out;
  1128. }
  1129. lock_page(hpage);
  1130. }
  1131. if (PageAnon(hpage))
  1132. anon_vma = page_get_anon_vma(hpage);
  1133. if (unlikely(!trylock_page(new_hpage)))
  1134. goto put_anon;
  1135. if (page_mapped(hpage)) {
  1136. try_to_unmap(hpage,
  1137. TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
  1138. page_was_mapped = 1;
  1139. }
  1140. if (!page_mapped(hpage))
  1141. rc = move_to_new_page(new_hpage, hpage, mode);
  1142. if (page_was_mapped)
  1143. remove_migration_ptes(hpage,
  1144. rc == MIGRATEPAGE_SUCCESS ? new_hpage : hpage, false);
  1145. unlock_page(new_hpage);
  1146. put_anon:
  1147. if (anon_vma)
  1148. put_anon_vma(anon_vma);
  1149. if (rc == MIGRATEPAGE_SUCCESS) {
  1150. move_hugetlb_state(hpage, new_hpage, reason);
  1151. put_new_page = NULL;
  1152. }
  1153. unlock_page(hpage);
  1154. out:
  1155. if (rc != -EAGAIN)
  1156. putback_active_hugepage(hpage);
  1157. /*
  1158. * If migration was not successful and there's a freeing callback, use
  1159. * it. Otherwise, put_page() will drop the reference grabbed during
  1160. * isolation.
  1161. */
  1162. if (put_new_page)
  1163. put_new_page(new_hpage, private);
  1164. else
  1165. putback_active_hugepage(new_hpage);
  1166. return rc;
  1167. }
  1168. /*
  1169. * migrate_pages - migrate the pages specified in a list, to the free pages
  1170. * supplied as the target for the page migration
  1171. *
  1172. * @from: The list of pages to be migrated.
  1173. * @get_new_page: The function used to allocate free pages to be used
  1174. * as the target of the page migration.
  1175. * @put_new_page: The function used to free target pages if migration
  1176. * fails, or NULL if no special handling is necessary.
  1177. * @private: Private data to be passed on to get_new_page()
  1178. * @mode: The migration mode that specifies the constraints for
  1179. * page migration, if any.
  1180. * @reason: The reason for page migration.
  1181. *
  1182. * The function returns after 10 attempts or if no pages are movable any more
  1183. * because the list has become empty or no retryable pages exist any more.
  1184. * The caller should call putback_movable_pages() to return pages to the LRU
  1185. * or free list only if ret != 0.
  1186. *
  1187. * Returns the number of pages that were not migrated, or an error code.
  1188. */
  1189. int migrate_pages(struct list_head *from, new_page_t get_new_page,
  1190. free_page_t put_new_page, unsigned long private,
  1191. enum migrate_mode mode, int reason)
  1192. {
  1193. int retry = 1;
  1194. int nr_failed = 0;
  1195. int nr_succeeded = 0;
  1196. int pass = 0;
  1197. struct page *page;
  1198. struct page *page2;
  1199. int swapwrite = current->flags & PF_SWAPWRITE;
  1200. int rc;
  1201. if (!swapwrite)
  1202. current->flags |= PF_SWAPWRITE;
  1203. for(pass = 0; pass < 10 && retry; pass++) {
  1204. retry = 0;
  1205. list_for_each_entry_safe(page, page2, from, lru) {
  1206. retry:
  1207. cond_resched();
  1208. if (PageHuge(page))
  1209. rc = unmap_and_move_huge_page(get_new_page,
  1210. put_new_page, private, page,
  1211. pass > 2, mode, reason);
  1212. else
  1213. rc = unmap_and_move(get_new_page, put_new_page,
  1214. private, page, pass > 2, mode,
  1215. reason);
  1216. switch(rc) {
  1217. case -ENOMEM:
  1218. /*
  1219. * THP migration might be unsupported or the
  1220. * allocation could've failed so we should
  1221. * retry on the same page with the THP split
  1222. * to base pages.
  1223. *
  1224. * Head page is retried immediately and tail
  1225. * pages are added to the tail of the list so
  1226. * we encounter them after the rest of the list
  1227. * is processed.
  1228. */
  1229. if (PageTransHuge(page) && !PageHuge(page)) {
  1230. lock_page(page);
  1231. rc = split_huge_page_to_list(page, from);
  1232. unlock_page(page);
  1233. if (!rc) {
  1234. list_safe_reset_next(page, page2, lru);
  1235. goto retry;
  1236. }
  1237. }
  1238. nr_failed++;
  1239. goto out;
  1240. case -EAGAIN:
  1241. retry++;
  1242. break;
  1243. case MIGRATEPAGE_SUCCESS:
  1244. nr_succeeded++;
  1245. break;
  1246. default:
  1247. /*
  1248. * Permanent failure (-EBUSY, -ENOSYS, etc.):
  1249. * unlike -EAGAIN case, the failed page is
  1250. * removed from migration page list and not
  1251. * retried in the next outer loop.
  1252. */
  1253. nr_failed++;
  1254. break;
  1255. }
  1256. }
  1257. }
  1258. nr_failed += retry;
  1259. rc = nr_failed;
  1260. out:
  1261. if (nr_succeeded)
  1262. count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
  1263. if (nr_failed)
  1264. count_vm_events(PGMIGRATE_FAIL, nr_failed);
  1265. trace_mm_migrate_pages(nr_succeeded, nr_failed, mode, reason);
  1266. if (!swapwrite)
  1267. current->flags &= ~PF_SWAPWRITE;
  1268. return rc;
  1269. }
  1270. #ifdef CONFIG_NUMA
  1271. static int store_status(int __user *status, int start, int value, int nr)
  1272. {
  1273. while (nr-- > 0) {
  1274. if (put_user(value, status + start))
  1275. return -EFAULT;
  1276. start++;
  1277. }
  1278. return 0;
  1279. }
  1280. static int do_move_pages_to_node(struct mm_struct *mm,
  1281. struct list_head *pagelist, int node)
  1282. {
  1283. int err;
  1284. if (list_empty(pagelist))
  1285. return 0;
  1286. err = migrate_pages(pagelist, alloc_new_node_page, NULL, node,
  1287. MIGRATE_SYNC, MR_SYSCALL);
  1288. if (err)
  1289. putback_movable_pages(pagelist);
  1290. return err;
  1291. }
  1292. /*
  1293. * Resolves the given address to a struct page, isolates it from the LRU and
  1294. * puts it to the given pagelist.
  1295. * Returns -errno if the page cannot be found/isolated or 0 when it has been
  1296. * queued or the page doesn't need to be migrated because it is already on
  1297. * the target node
  1298. */
  1299. static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
  1300. int node, struct list_head *pagelist, bool migrate_all)
  1301. {
  1302. struct vm_area_struct *vma;
  1303. struct page *page;
  1304. unsigned int follflags;
  1305. int err;
  1306. down_read(&mm->mmap_sem);
  1307. err = -EFAULT;
  1308. vma = find_vma(mm, addr);
  1309. if (!vma || addr < vma->vm_start || !vma_migratable(vma))
  1310. goto out;
  1311. /* FOLL_DUMP to ignore special (like zero) pages */
  1312. follflags = FOLL_GET | FOLL_DUMP;
  1313. page = follow_page(vma, addr, follflags);
  1314. err = PTR_ERR(page);
  1315. if (IS_ERR(page))
  1316. goto out;
  1317. err = -ENOENT;
  1318. if (!page)
  1319. goto out;
  1320. err = 0;
  1321. if (page_to_nid(page) == node)
  1322. goto out_putpage;
  1323. err = -EACCES;
  1324. if (page_mapcount(page) > 1 && !migrate_all)
  1325. goto out_putpage;
  1326. if (PageHuge(page)) {
  1327. if (PageHead(page)) {
  1328. isolate_huge_page(page, pagelist);
  1329. err = 0;
  1330. }
  1331. } else {
  1332. struct page *head;
  1333. head = compound_head(page);
  1334. err = isolate_lru_page(head);
  1335. if (err)
  1336. goto out_putpage;
  1337. err = 0;
  1338. list_add_tail(&head->lru, pagelist);
  1339. mod_node_page_state(page_pgdat(head),
  1340. NR_ISOLATED_ANON + page_is_file_cache(head),
  1341. hpage_nr_pages(head));
  1342. }
  1343. out_putpage:
  1344. /*
  1345. * Either remove the duplicate refcount from
  1346. * isolate_lru_page() or drop the page ref if it was
  1347. * not isolated.
  1348. */
  1349. put_page(page);
  1350. out:
  1351. up_read(&mm->mmap_sem);
  1352. return err;
  1353. }
  1354. /*
  1355. * Migrate an array of page address onto an array of nodes and fill
  1356. * the corresponding array of status.
  1357. */
  1358. static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
  1359. unsigned long nr_pages,
  1360. const void __user * __user *pages,
  1361. const int __user *nodes,
  1362. int __user *status, int flags)
  1363. {
  1364. int current_node = NUMA_NO_NODE;
  1365. LIST_HEAD(pagelist);
  1366. int start, i;
  1367. int err = 0, err1;
  1368. migrate_prep();
  1369. for (i = start = 0; i < nr_pages; i++) {
  1370. const void __user *p;
  1371. unsigned long addr;
  1372. int node;
  1373. err = -EFAULT;
  1374. if (get_user(p, pages + i))
  1375. goto out_flush;
  1376. if (get_user(node, nodes + i))
  1377. goto out_flush;
  1378. addr = (unsigned long)p;
  1379. err = -ENODEV;
  1380. if (node < 0 || node >= MAX_NUMNODES)
  1381. goto out_flush;
  1382. if (!node_state(node, N_MEMORY))
  1383. goto out_flush;
  1384. err = -EACCES;
  1385. if (!node_isset(node, task_nodes))
  1386. goto out_flush;
  1387. if (current_node == NUMA_NO_NODE) {
  1388. current_node = node;
  1389. start = i;
  1390. } else if (node != current_node) {
  1391. err = do_move_pages_to_node(mm, &pagelist, current_node);
  1392. if (err)
  1393. goto out;
  1394. err = store_status(status, start, current_node, i - start);
  1395. if (err)
  1396. goto out;
  1397. start = i;
  1398. current_node = node;
  1399. }
  1400. /*
  1401. * Errors in the page lookup or isolation are not fatal and we simply
  1402. * report them via status
  1403. */
  1404. err = add_page_for_migration(mm, addr, current_node,
  1405. &pagelist, flags & MPOL_MF_MOVE_ALL);
  1406. if (!err)
  1407. continue;
  1408. err = store_status(status, i, err, 1);
  1409. if (err)
  1410. goto out_flush;
  1411. err = do_move_pages_to_node(mm, &pagelist, current_node);
  1412. if (err)
  1413. goto out;
  1414. if (i > start) {
  1415. err = store_status(status, start, current_node, i - start);
  1416. if (err)
  1417. goto out;
  1418. }
  1419. current_node = NUMA_NO_NODE;
  1420. }
  1421. out_flush:
  1422. if (list_empty(&pagelist))
  1423. return err;
  1424. /* Make sure we do not overwrite the existing error */
  1425. err1 = do_move_pages_to_node(mm, &pagelist, current_node);
  1426. if (!err1)
  1427. err1 = store_status(status, start, current_node, i - start);
  1428. if (!err)
  1429. err = err1;
  1430. out:
  1431. return err;
  1432. }
  1433. /*
  1434. * Determine the nodes of an array of pages and store it in an array of status.
  1435. */
  1436. static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
  1437. const void __user **pages, int *status)
  1438. {
  1439. unsigned long i;
  1440. down_read(&mm->mmap_sem);
  1441. for (i = 0; i < nr_pages; i++) {
  1442. unsigned long addr = (unsigned long)(*pages);
  1443. struct vm_area_struct *vma;
  1444. struct page *page;
  1445. int err = -EFAULT;
  1446. vma = find_vma(mm, addr);
  1447. if (!vma || addr < vma->vm_start)
  1448. goto set_status;
  1449. /* FOLL_DUMP to ignore special (like zero) pages */
  1450. page = follow_page(vma, addr, FOLL_DUMP);
  1451. err = PTR_ERR(page);
  1452. if (IS_ERR(page))
  1453. goto set_status;
  1454. err = page ? page_to_nid(page) : -ENOENT;
  1455. set_status:
  1456. *status = err;
  1457. pages++;
  1458. status++;
  1459. }
  1460. up_read(&mm->mmap_sem);
  1461. }
  1462. /*
  1463. * Determine the nodes of a user array of pages and store it in
  1464. * a user array of status.
  1465. */
  1466. static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
  1467. const void __user * __user *pages,
  1468. int __user *status)
  1469. {
  1470. #define DO_PAGES_STAT_CHUNK_NR 16
  1471. const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
  1472. int chunk_status[DO_PAGES_STAT_CHUNK_NR];
  1473. while (nr_pages) {
  1474. unsigned long chunk_nr;
  1475. chunk_nr = nr_pages;
  1476. if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
  1477. chunk_nr = DO_PAGES_STAT_CHUNK_NR;
  1478. if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
  1479. break;
  1480. do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
  1481. if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
  1482. break;
  1483. pages += chunk_nr;
  1484. status += chunk_nr;
  1485. nr_pages -= chunk_nr;
  1486. }
  1487. return nr_pages ? -EFAULT : 0;
  1488. }
  1489. /*
  1490. * Move a list of pages in the address space of the currently executing
  1491. * process.
  1492. */
  1493. static int kernel_move_pages(pid_t pid, unsigned long nr_pages,
  1494. const void __user * __user *pages,
  1495. const int __user *nodes,
  1496. int __user *status, int flags)
  1497. {
  1498. struct task_struct *task;
  1499. struct mm_struct *mm;
  1500. int err;
  1501. nodemask_t task_nodes;
  1502. /* Check flags */
  1503. if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
  1504. return -EINVAL;
  1505. if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
  1506. return -EPERM;
  1507. /* Find the mm_struct */
  1508. rcu_read_lock();
  1509. task = pid ? find_task_by_vpid(pid) : current;
  1510. if (!task) {
  1511. rcu_read_unlock();
  1512. return -ESRCH;
  1513. }
  1514. get_task_struct(task);
  1515. /*
  1516. * Check if this process has the right to modify the specified
  1517. * process. Use the regular "ptrace_may_access()" checks.
  1518. */
  1519. if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
  1520. rcu_read_unlock();
  1521. err = -EPERM;
  1522. goto out;
  1523. }
  1524. rcu_read_unlock();
  1525. err = security_task_movememory(task);
  1526. if (err)
  1527. goto out;
  1528. task_nodes = cpuset_mems_allowed(task);
  1529. mm = get_task_mm(task);
  1530. put_task_struct(task);
  1531. if (!mm)
  1532. return -EINVAL;
  1533. if (nodes)
  1534. err = do_pages_move(mm, task_nodes, nr_pages, pages,
  1535. nodes, status, flags);
  1536. else
  1537. err = do_pages_stat(mm, nr_pages, pages, status);
  1538. mmput(mm);
  1539. return err;
  1540. out:
  1541. put_task_struct(task);
  1542. return err;
  1543. }
  1544. SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
  1545. const void __user * __user *, pages,
  1546. const int __user *, nodes,
  1547. int __user *, status, int, flags)
  1548. {
  1549. return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags);
  1550. }
  1551. #ifdef CONFIG_COMPAT
  1552. COMPAT_SYSCALL_DEFINE6(move_pages, pid_t, pid, compat_ulong_t, nr_pages,
  1553. compat_uptr_t __user *, pages32,
  1554. const int __user *, nodes,
  1555. int __user *, status,
  1556. int, flags)
  1557. {
  1558. const void __user * __user *pages;
  1559. int i;
  1560. pages = compat_alloc_user_space(nr_pages * sizeof(void *));
  1561. for (i = 0; i < nr_pages; i++) {
  1562. compat_uptr_t p;
  1563. if (get_user(p, pages32 + i) ||
  1564. put_user(compat_ptr(p), pages + i))
  1565. return -EFAULT;
  1566. }
  1567. return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags);
  1568. }
  1569. #endif /* CONFIG_COMPAT */
  1570. #ifdef CONFIG_NUMA_BALANCING
  1571. /*
  1572. * Returns true if this is a safe migration target node for misplaced NUMA
  1573. * pages. Currently it only checks the watermarks which crude
  1574. */
  1575. static bool migrate_balanced_pgdat(struct pglist_data *pgdat,
  1576. unsigned long nr_migrate_pages)
  1577. {
  1578. int z;
  1579. for (z = pgdat->nr_zones - 1; z >= 0; z--) {
  1580. struct zone *zone = pgdat->node_zones + z;
  1581. if (!populated_zone(zone))
  1582. continue;
  1583. /* Avoid waking kswapd by allocating pages_to_migrate pages. */
  1584. if (!zone_watermark_ok(zone, 0,
  1585. high_wmark_pages(zone) +
  1586. nr_migrate_pages,
  1587. 0, 0))
  1588. continue;
  1589. return true;
  1590. }
  1591. return false;
  1592. }
  1593. static struct page *alloc_misplaced_dst_page(struct page *page,
  1594. unsigned long data)
  1595. {
  1596. int nid = (int) data;
  1597. struct page *newpage;
  1598. newpage = __alloc_pages_node(nid,
  1599. (GFP_HIGHUSER_MOVABLE |
  1600. __GFP_THISNODE | __GFP_NOMEMALLOC |
  1601. __GFP_NORETRY | __GFP_NOWARN) &
  1602. ~__GFP_RECLAIM, 0);
  1603. return newpage;
  1604. }
  1605. static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
  1606. {
  1607. int page_lru;
  1608. VM_BUG_ON_PAGE(compound_order(page) && !PageTransHuge(page), page);
  1609. /* Avoid migrating to a node that is nearly full */
  1610. if (!migrate_balanced_pgdat(pgdat, 1UL << compound_order(page)))
  1611. return 0;
  1612. if (isolate_lru_page(page))
  1613. return 0;
  1614. /*
  1615. * migrate_misplaced_transhuge_page() skips page migration's usual
  1616. * check on page_count(), so we must do it here, now that the page
  1617. * has been isolated: a GUP pin, or any other pin, prevents migration.
  1618. * The expected page count is 3: 1 for page's mapcount and 1 for the
  1619. * caller's pin and 1 for the reference taken by isolate_lru_page().
  1620. */
  1621. if (PageTransHuge(page) && page_count(page) != 3) {
  1622. putback_lru_page(page);
  1623. return 0;
  1624. }
  1625. page_lru = page_is_file_cache(page);
  1626. mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_lru,
  1627. hpage_nr_pages(page));
  1628. /*
  1629. * Isolating the page has taken another reference, so the
  1630. * caller's reference can be safely dropped without the page
  1631. * disappearing underneath us during migration.
  1632. */
  1633. put_page(page);
  1634. return 1;
  1635. }
  1636. bool pmd_trans_migrating(pmd_t pmd)
  1637. {
  1638. struct page *page = pmd_page(pmd);
  1639. return PageLocked(page);
  1640. }
  1641. /*
  1642. * Attempt to migrate a misplaced page to the specified destination
  1643. * node. Caller is expected to have an elevated reference count on
  1644. * the page that will be dropped by this function before returning.
  1645. */
  1646. int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma,
  1647. int node)
  1648. {
  1649. pg_data_t *pgdat = NODE_DATA(node);
  1650. int isolated;
  1651. int nr_remaining;
  1652. LIST_HEAD(migratepages);
  1653. /*
  1654. * Don't migrate file pages that are mapped in multiple processes
  1655. * with execute permissions as they are probably shared libraries.
  1656. */
  1657. if (page_mapcount(page) != 1 && page_is_file_cache(page) &&
  1658. (vma->vm_flags & VM_EXEC))
  1659. goto out;
  1660. /*
  1661. * Also do not migrate dirty pages as not all filesystems can move
  1662. * dirty pages in MIGRATE_ASYNC mode which is a waste of cycles.
  1663. */
  1664. if (page_is_file_cache(page) && PageDirty(page))
  1665. goto out;
  1666. isolated = numamigrate_isolate_page(pgdat, page);
  1667. if (!isolated)
  1668. goto out;
  1669. list_add(&page->lru, &migratepages);
  1670. nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_page,
  1671. NULL, node, MIGRATE_ASYNC,
  1672. MR_NUMA_MISPLACED);
  1673. if (nr_remaining) {
  1674. if (!list_empty(&migratepages)) {
  1675. list_del(&page->lru);
  1676. dec_node_page_state(page, NR_ISOLATED_ANON +
  1677. page_is_file_cache(page));
  1678. putback_lru_page(page);
  1679. }
  1680. isolated = 0;
  1681. } else
  1682. count_vm_numa_event(NUMA_PAGE_MIGRATE);
  1683. BUG_ON(!list_empty(&migratepages));
  1684. return isolated;
  1685. out:
  1686. put_page(page);
  1687. return 0;
  1688. }
  1689. #endif /* CONFIG_NUMA_BALANCING */
  1690. #if defined(CONFIG_NUMA_BALANCING) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
  1691. /*
  1692. * Migrates a THP to a given target node. page must be locked and is unlocked
  1693. * before returning.
  1694. */
  1695. int migrate_misplaced_transhuge_page(struct mm_struct *mm,
  1696. struct vm_area_struct *vma,
  1697. pmd_t *pmd, pmd_t entry,
  1698. unsigned long address,
  1699. struct page *page, int node)
  1700. {
  1701. spinlock_t *ptl;
  1702. pg_data_t *pgdat = NODE_DATA(node);
  1703. int isolated = 0;
  1704. struct page *new_page = NULL;
  1705. int page_lru = page_is_file_cache(page);
  1706. unsigned long start = address & HPAGE_PMD_MASK;
  1707. new_page = alloc_pages_node(node,
  1708. (GFP_TRANSHUGE_LIGHT | __GFP_THISNODE),
  1709. HPAGE_PMD_ORDER);
  1710. if (!new_page)
  1711. goto out_fail;
  1712. prep_transhuge_page(new_page);
  1713. isolated = numamigrate_isolate_page(pgdat, page);
  1714. if (!isolated) {
  1715. put_page(new_page);
  1716. goto out_fail;
  1717. }
  1718. /* Prepare a page as a migration target */
  1719. __SetPageLocked(new_page);
  1720. if (PageSwapBacked(page))
  1721. __SetPageSwapBacked(new_page);
  1722. /* anon mapping, we can simply copy page->mapping to the new page: */
  1723. new_page->mapping = page->mapping;
  1724. new_page->index = page->index;
  1725. /* flush the cache before copying using the kernel virtual address */
  1726. flush_cache_range(vma, start, start + HPAGE_PMD_SIZE);
  1727. migrate_page_copy(new_page, page);
  1728. WARN_ON(PageLRU(new_page));
  1729. /* Recheck the target PMD */
  1730. ptl = pmd_lock(mm, pmd);
  1731. if (unlikely(!pmd_same(*pmd, entry) || !page_ref_freeze(page, 2))) {
  1732. spin_unlock(ptl);
  1733. /* Reverse changes made by migrate_page_copy() */
  1734. if (TestClearPageActive(new_page))
  1735. SetPageActive(page);
  1736. if (TestClearPageUnevictable(new_page))
  1737. SetPageUnevictable(page);
  1738. unlock_page(new_page);
  1739. put_page(new_page); /* Free it */
  1740. /* Retake the callers reference and putback on LRU */
  1741. get_page(page);
  1742. putback_lru_page(page);
  1743. mod_node_page_state(page_pgdat(page),
  1744. NR_ISOLATED_ANON + page_lru, -HPAGE_PMD_NR);
  1745. goto out_unlock;
  1746. }
  1747. entry = mk_huge_pmd(new_page, vma->vm_page_prot);
  1748. entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
  1749. /*
  1750. * Overwrite the old entry under pagetable lock and establish
  1751. * the new PTE. Any parallel GUP will either observe the old
  1752. * page blocking on the page lock, block on the page table
  1753. * lock or observe the new page. The SetPageUptodate on the
  1754. * new page and page_add_new_anon_rmap guarantee the copy is
  1755. * visible before the pagetable update.
  1756. */
  1757. page_add_anon_rmap(new_page, vma, start, true);
  1758. /*
  1759. * At this point the pmd is numa/protnone (i.e. non present) and the TLB
  1760. * has already been flushed globally. So no TLB can be currently
  1761. * caching this non present pmd mapping. There's no need to clear the
  1762. * pmd before doing set_pmd_at(), nor to flush the TLB after
  1763. * set_pmd_at(). Clearing the pmd here would introduce a race
  1764. * condition against MADV_DONTNEED, because MADV_DONTNEED only holds the
  1765. * mmap_sem for reading. If the pmd is set to NULL at any given time,
  1766. * MADV_DONTNEED won't wait on the pmd lock and it'll skip clearing this
  1767. * pmd.
  1768. */
  1769. set_pmd_at(mm, start, pmd, entry);
  1770. update_mmu_cache_pmd(vma, address, &entry);
  1771. page_ref_unfreeze(page, 2);
  1772. mlock_migrate_page(new_page, page);
  1773. page_remove_rmap(page, true);
  1774. set_page_owner_migrate_reason(new_page, MR_NUMA_MISPLACED);
  1775. spin_unlock(ptl);
  1776. /* Take an "isolate" reference and put new page on the LRU. */
  1777. get_page(new_page);
  1778. putback_lru_page(new_page);
  1779. unlock_page(new_page);
  1780. unlock_page(page);
  1781. put_page(page); /* Drop the rmap reference */
  1782. put_page(page); /* Drop the LRU isolation reference */
  1783. count_vm_events(PGMIGRATE_SUCCESS, HPAGE_PMD_NR);
  1784. count_vm_numa_events(NUMA_PAGE_MIGRATE, HPAGE_PMD_NR);
  1785. mod_node_page_state(page_pgdat(page),
  1786. NR_ISOLATED_ANON + page_lru,
  1787. -HPAGE_PMD_NR);
  1788. return isolated;
  1789. out_fail:
  1790. count_vm_events(PGMIGRATE_FAIL, HPAGE_PMD_NR);
  1791. ptl = pmd_lock(mm, pmd);
  1792. if (pmd_same(*pmd, entry)) {
  1793. entry = pmd_modify(entry, vma->vm_page_prot);
  1794. set_pmd_at(mm, start, pmd, entry);
  1795. update_mmu_cache_pmd(vma, address, &entry);
  1796. }
  1797. spin_unlock(ptl);
  1798. out_unlock:
  1799. unlock_page(page);
  1800. put_page(page);
  1801. return 0;
  1802. }
  1803. #endif /* CONFIG_NUMA_BALANCING */
  1804. #endif /* CONFIG_NUMA */
  1805. #if defined(CONFIG_MIGRATE_VMA_HELPER)
  1806. struct migrate_vma {
  1807. struct vm_area_struct *vma;
  1808. unsigned long *dst;
  1809. unsigned long *src;
  1810. unsigned long cpages;
  1811. unsigned long npages;
  1812. unsigned long start;
  1813. unsigned long end;
  1814. };
  1815. static int migrate_vma_collect_hole(unsigned long start,
  1816. unsigned long end,
  1817. struct mm_walk *walk)
  1818. {
  1819. struct migrate_vma *migrate = walk->private;
  1820. unsigned long addr;
  1821. for (addr = start & PAGE_MASK; addr < end; addr += PAGE_SIZE) {
  1822. migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE;
  1823. migrate->dst[migrate->npages] = 0;
  1824. migrate->npages++;
  1825. migrate->cpages++;
  1826. }
  1827. return 0;
  1828. }
  1829. static int migrate_vma_collect_skip(unsigned long start,
  1830. unsigned long end,
  1831. struct mm_walk *walk)
  1832. {
  1833. struct migrate_vma *migrate = walk->private;
  1834. unsigned long addr;
  1835. for (addr = start & PAGE_MASK; addr < end; addr += PAGE_SIZE) {
  1836. migrate->dst[migrate->npages] = 0;
  1837. migrate->src[migrate->npages++] = 0;
  1838. }
  1839. return 0;
  1840. }
  1841. static int migrate_vma_collect_pmd(pmd_t *pmdp,
  1842. unsigned long start,
  1843. unsigned long end,
  1844. struct mm_walk *walk)
  1845. {
  1846. struct migrate_vma *migrate = walk->private;
  1847. struct vm_area_struct *vma = walk->vma;
  1848. struct mm_struct *mm = vma->vm_mm;
  1849. unsigned long addr = start, unmapped = 0;
  1850. spinlock_t *ptl;
  1851. pte_t *ptep;
  1852. again:
  1853. if (pmd_none(*pmdp))
  1854. return migrate_vma_collect_hole(start, end, walk);
  1855. if (pmd_trans_huge(*pmdp)) {
  1856. struct page *page;
  1857. ptl = pmd_lock(mm, pmdp);
  1858. if (unlikely(!pmd_trans_huge(*pmdp))) {
  1859. spin_unlock(ptl);
  1860. goto again;
  1861. }
  1862. page = pmd_page(*pmdp);
  1863. if (is_huge_zero_page(page)) {
  1864. spin_unlock(ptl);
  1865. split_huge_pmd(vma, pmdp, addr);
  1866. if (pmd_trans_unstable(pmdp))
  1867. return migrate_vma_collect_skip(start, end,
  1868. walk);
  1869. } else {
  1870. int ret;
  1871. get_page(page);
  1872. spin_unlock(ptl);
  1873. if (unlikely(!trylock_page(page)))
  1874. return migrate_vma_collect_skip(start, end,
  1875. walk);
  1876. ret = split_huge_page(page);
  1877. unlock_page(page);
  1878. put_page(page);
  1879. if (ret)
  1880. return migrate_vma_collect_skip(start, end,
  1881. walk);
  1882. if (pmd_none(*pmdp))
  1883. return migrate_vma_collect_hole(start, end,
  1884. walk);
  1885. }
  1886. }
  1887. if (unlikely(pmd_bad(*pmdp)))
  1888. return migrate_vma_collect_skip(start, end, walk);
  1889. ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
  1890. arch_enter_lazy_mmu_mode();
  1891. for (; addr < end; addr += PAGE_SIZE, ptep++) {
  1892. unsigned long mpfn, pfn;
  1893. struct page *page;
  1894. swp_entry_t entry;
  1895. pte_t pte;
  1896. pte = *ptep;
  1897. pfn = pte_pfn(pte);
  1898. if (pte_none(pte)) {
  1899. mpfn = MIGRATE_PFN_MIGRATE;
  1900. migrate->cpages++;
  1901. pfn = 0;
  1902. goto next;
  1903. }
  1904. if (!pte_present(pte)) {
  1905. mpfn = pfn = 0;
  1906. /*
  1907. * Only care about unaddressable device page special
  1908. * page table entry. Other special swap entries are not
  1909. * migratable, and we ignore regular swapped page.
  1910. */
  1911. entry = pte_to_swp_entry(pte);
  1912. if (!is_device_private_entry(entry))
  1913. goto next;
  1914. page = device_private_entry_to_page(entry);
  1915. mpfn = migrate_pfn(page_to_pfn(page))|
  1916. MIGRATE_PFN_DEVICE | MIGRATE_PFN_MIGRATE;
  1917. if (is_write_device_private_entry(entry))
  1918. mpfn |= MIGRATE_PFN_WRITE;
  1919. } else {
  1920. if (is_zero_pfn(pfn)) {
  1921. mpfn = MIGRATE_PFN_MIGRATE;
  1922. migrate->cpages++;
  1923. pfn = 0;
  1924. goto next;
  1925. }
  1926. page = _vm_normal_page(migrate->vma, addr, pte, true);
  1927. mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
  1928. mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0;
  1929. }
  1930. /* FIXME support THP */
  1931. if (!page || !page->mapping || PageTransCompound(page)) {
  1932. mpfn = pfn = 0;
  1933. goto next;
  1934. }
  1935. pfn = page_to_pfn(page);
  1936. /*
  1937. * By getting a reference on the page we pin it and that blocks
  1938. * any kind of migration. Side effect is that it "freezes" the
  1939. * pte.
  1940. *
  1941. * We drop this reference after isolating the page from the lru
  1942. * for non device page (device page are not on the lru and thus
  1943. * can't be dropped from it).
  1944. */
  1945. get_page(page);
  1946. migrate->cpages++;
  1947. /*
  1948. * Optimize for the common case where page is only mapped once
  1949. * in one process. If we can lock the page, then we can safely
  1950. * set up a special migration page table entry now.
  1951. */
  1952. if (trylock_page(page)) {
  1953. pte_t swp_pte;
  1954. mpfn |= MIGRATE_PFN_LOCKED;
  1955. ptep_get_and_clear(mm, addr, ptep);
  1956. /* Setup special migration page table entry */
  1957. entry = make_migration_entry(page, mpfn &
  1958. MIGRATE_PFN_WRITE);
  1959. swp_pte = swp_entry_to_pte(entry);
  1960. if (pte_soft_dirty(pte))
  1961. swp_pte = pte_swp_mksoft_dirty(swp_pte);
  1962. set_pte_at(mm, addr, ptep, swp_pte);
  1963. /*
  1964. * This is like regular unmap: we remove the rmap and
  1965. * drop page refcount. Page won't be freed, as we took
  1966. * a reference just above.
  1967. */
  1968. page_remove_rmap(page, false);
  1969. put_page(page);
  1970. if (pte_present(pte))
  1971. unmapped++;
  1972. }
  1973. next:
  1974. migrate->dst[migrate->npages] = 0;
  1975. migrate->src[migrate->npages++] = mpfn;
  1976. }
  1977. arch_leave_lazy_mmu_mode();
  1978. pte_unmap_unlock(ptep - 1, ptl);
  1979. /* Only flush the TLB if we actually modified any entries */
  1980. if (unmapped)
  1981. flush_tlb_range(walk->vma, start, end);
  1982. return 0;
  1983. }
  1984. /*
  1985. * migrate_vma_collect() - collect pages over a range of virtual addresses
  1986. * @migrate: migrate struct containing all migration information
  1987. *
  1988. * This will walk the CPU page table. For each virtual address backed by a
  1989. * valid page, it updates the src array and takes a reference on the page, in
  1990. * order to pin the page until we lock it and unmap it.
  1991. */
  1992. static void migrate_vma_collect(struct migrate_vma *migrate)
  1993. {
  1994. struct mm_walk mm_walk;
  1995. mm_walk.pmd_entry = migrate_vma_collect_pmd;
  1996. mm_walk.pte_entry = NULL;
  1997. mm_walk.pte_hole = migrate_vma_collect_hole;
  1998. mm_walk.hugetlb_entry = NULL;
  1999. mm_walk.test_walk = NULL;
  2000. mm_walk.vma = migrate->vma;
  2001. mm_walk.mm = migrate->vma->vm_mm;
  2002. mm_walk.private = migrate;
  2003. mmu_notifier_invalidate_range_start(mm_walk.mm,
  2004. migrate->start,
  2005. migrate->end);
  2006. walk_page_range(migrate->start, migrate->end, &mm_walk);
  2007. mmu_notifier_invalidate_range_end(mm_walk.mm,
  2008. migrate->start,
  2009. migrate->end);
  2010. migrate->end = migrate->start + (migrate->npages << PAGE_SHIFT);
  2011. }
  2012. /*
  2013. * migrate_vma_check_page() - check if page is pinned or not
  2014. * @page: struct page to check
  2015. *
  2016. * Pinned pages cannot be migrated. This is the same test as in
  2017. * migrate_page_move_mapping(), except that here we allow migration of a
  2018. * ZONE_DEVICE page.
  2019. */
  2020. static bool migrate_vma_check_page(struct page *page)
  2021. {
  2022. /*
  2023. * One extra ref because caller holds an extra reference, either from
  2024. * isolate_lru_page() for a regular page, or migrate_vma_collect() for
  2025. * a device page.
  2026. */
  2027. int extra = 1;
  2028. /*
  2029. * FIXME support THP (transparent huge page), it is bit more complex to
  2030. * check them than regular pages, because they can be mapped with a pmd
  2031. * or with a pte (split pte mapping).
  2032. */
  2033. if (PageCompound(page))
  2034. return false;
  2035. /* Page from ZONE_DEVICE have one extra reference */
  2036. if (is_zone_device_page(page)) {
  2037. /*
  2038. * Private page can never be pin as they have no valid pte and
  2039. * GUP will fail for those. Yet if there is a pending migration
  2040. * a thread might try to wait on the pte migration entry and
  2041. * will bump the page reference count. Sadly there is no way to
  2042. * differentiate a regular pin from migration wait. Hence to
  2043. * avoid 2 racing thread trying to migrate back to CPU to enter
  2044. * infinite loop (one stoping migration because the other is
  2045. * waiting on pte migration entry). We always return true here.
  2046. *
  2047. * FIXME proper solution is to rework migration_entry_wait() so
  2048. * it does not need to take a reference on page.
  2049. */
  2050. if (is_device_private_page(page))
  2051. return true;
  2052. /*
  2053. * Only allow device public page to be migrated and account for
  2054. * the extra reference count imply by ZONE_DEVICE pages.
  2055. */
  2056. if (!is_device_public_page(page))
  2057. return false;
  2058. extra++;
  2059. }
  2060. /* For file back page */
  2061. if (page_mapping(page))
  2062. extra += 1 + page_has_private(page);
  2063. if ((page_count(page) - extra) > page_mapcount(page))
  2064. return false;
  2065. return true;
  2066. }
  2067. /*
  2068. * migrate_vma_prepare() - lock pages and isolate them from the lru
  2069. * @migrate: migrate struct containing all migration information
  2070. *
  2071. * This locks pages that have been collected by migrate_vma_collect(). Once each
  2072. * page is locked it is isolated from the lru (for non-device pages). Finally,
  2073. * the ref taken by migrate_vma_collect() is dropped, as locked pages cannot be
  2074. * migrated by concurrent kernel threads.
  2075. */
  2076. static void migrate_vma_prepare(struct migrate_vma *migrate)
  2077. {
  2078. const unsigned long npages = migrate->npages;
  2079. const unsigned long start = migrate->start;
  2080. unsigned long addr, i, restore = 0;
  2081. bool allow_drain = true;
  2082. lru_add_drain();
  2083. for (i = 0; (i < npages) && migrate->cpages; i++) {
  2084. struct page *page = migrate_pfn_to_page(migrate->src[i]);
  2085. bool remap = true;
  2086. if (!page)
  2087. continue;
  2088. if (!(migrate->src[i] & MIGRATE_PFN_LOCKED)) {
  2089. /*
  2090. * Because we are migrating several pages there can be
  2091. * a deadlock between 2 concurrent migration where each
  2092. * are waiting on each other page lock.
  2093. *
  2094. * Make migrate_vma() a best effort thing and backoff
  2095. * for any page we can not lock right away.
  2096. */
  2097. if (!trylock_page(page)) {
  2098. migrate->src[i] = 0;
  2099. migrate->cpages--;
  2100. put_page(page);
  2101. continue;
  2102. }
  2103. remap = false;
  2104. migrate->src[i] |= MIGRATE_PFN_LOCKED;
  2105. }
  2106. /* ZONE_DEVICE pages are not on LRU */
  2107. if (!is_zone_device_page(page)) {
  2108. if (!PageLRU(page) && allow_drain) {
  2109. /* Drain CPU's pagevec */
  2110. lru_add_drain_all();
  2111. allow_drain = false;
  2112. }
  2113. if (isolate_lru_page(page)) {
  2114. if (remap) {
  2115. migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
  2116. migrate->cpages--;
  2117. restore++;
  2118. } else {
  2119. migrate->src[i] = 0;
  2120. unlock_page(page);
  2121. migrate->cpages--;
  2122. put_page(page);
  2123. }
  2124. continue;
  2125. }
  2126. /* Drop the reference we took in collect */
  2127. put_page(page);
  2128. }
  2129. if (!migrate_vma_check_page(page)) {
  2130. if (remap) {
  2131. migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
  2132. migrate->cpages--;
  2133. restore++;
  2134. if (!is_zone_device_page(page)) {
  2135. get_page(page);
  2136. putback_lru_page(page);
  2137. }
  2138. } else {
  2139. migrate->src[i] = 0;
  2140. unlock_page(page);
  2141. migrate->cpages--;
  2142. if (!is_zone_device_page(page))
  2143. putback_lru_page(page);
  2144. else
  2145. put_page(page);
  2146. }
  2147. }
  2148. }
  2149. for (i = 0, addr = start; i < npages && restore; i++, addr += PAGE_SIZE) {
  2150. struct page *page = migrate_pfn_to_page(migrate->src[i]);
  2151. if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE))
  2152. continue;
  2153. remove_migration_pte(page, migrate->vma, addr, page);
  2154. migrate->src[i] = 0;
  2155. unlock_page(page);
  2156. put_page(page);
  2157. restore--;
  2158. }
  2159. }
  2160. /*
  2161. * migrate_vma_unmap() - replace page mapping with special migration pte entry
  2162. * @migrate: migrate struct containing all migration information
  2163. *
  2164. * Replace page mapping (CPU page table pte) with a special migration pte entry
  2165. * and check again if it has been pinned. Pinned pages are restored because we
  2166. * cannot migrate them.
  2167. *
  2168. * This is the last step before we call the device driver callback to allocate
  2169. * destination memory and copy contents of original page over to new page.
  2170. */
  2171. static void migrate_vma_unmap(struct migrate_vma *migrate)
  2172. {
  2173. int flags = TTU_MIGRATION | TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS;
  2174. const unsigned long npages = migrate->npages;
  2175. const unsigned long start = migrate->start;
  2176. unsigned long addr, i, restore = 0;
  2177. for (i = 0; i < npages; i++) {
  2178. struct page *page = migrate_pfn_to_page(migrate->src[i]);
  2179. if (!page || !(migrate->src[i] & MIGRATE_PFN_MIGRATE))
  2180. continue;
  2181. if (page_mapped(page)) {
  2182. try_to_unmap(page, flags);
  2183. if (page_mapped(page))
  2184. goto restore;
  2185. }
  2186. if (migrate_vma_check_page(page))
  2187. continue;
  2188. restore:
  2189. migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
  2190. migrate->cpages--;
  2191. restore++;
  2192. }
  2193. for (addr = start, i = 0; i < npages && restore; addr += PAGE_SIZE, i++) {
  2194. struct page *page = migrate_pfn_to_page(migrate->src[i]);
  2195. if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE))
  2196. continue;
  2197. remove_migration_ptes(page, page, false);
  2198. migrate->src[i] = 0;
  2199. unlock_page(page);
  2200. restore--;
  2201. if (is_zone_device_page(page))
  2202. put_page(page);
  2203. else
  2204. putback_lru_page(page);
  2205. }
  2206. }
  2207. static void migrate_vma_insert_page(struct migrate_vma *migrate,
  2208. unsigned long addr,
  2209. struct page *page,
  2210. unsigned long *src,
  2211. unsigned long *dst)
  2212. {
  2213. struct vm_area_struct *vma = migrate->vma;
  2214. struct mm_struct *mm = vma->vm_mm;
  2215. struct mem_cgroup *memcg;
  2216. bool flush = false;
  2217. spinlock_t *ptl;
  2218. pte_t entry;
  2219. pgd_t *pgdp;
  2220. p4d_t *p4dp;
  2221. pud_t *pudp;
  2222. pmd_t *pmdp;
  2223. pte_t *ptep;
  2224. /* Only allow populating anonymous memory */
  2225. if (!vma_is_anonymous(vma))
  2226. goto abort;
  2227. pgdp = pgd_offset(mm, addr);
  2228. p4dp = p4d_alloc(mm, pgdp, addr);
  2229. if (!p4dp)
  2230. goto abort;
  2231. pudp = pud_alloc(mm, p4dp, addr);
  2232. if (!pudp)
  2233. goto abort;
  2234. pmdp = pmd_alloc(mm, pudp, addr);
  2235. if (!pmdp)
  2236. goto abort;
  2237. if (pmd_trans_huge(*pmdp) || pmd_devmap(*pmdp))
  2238. goto abort;
  2239. /*
  2240. * Use pte_alloc() instead of pte_alloc_map(). We can't run
  2241. * pte_offset_map() on pmds where a huge pmd might be created
  2242. * from a different thread.
  2243. *
  2244. * pte_alloc_map() is safe to use under down_write(mmap_sem) or when
  2245. * parallel threads are excluded by other means.
  2246. *
  2247. * Here we only have down_read(mmap_sem).
  2248. */
  2249. if (pte_alloc(mm, pmdp, addr))
  2250. goto abort;
  2251. /* See the comment in pte_alloc_one_map() */
  2252. if (unlikely(pmd_trans_unstable(pmdp)))
  2253. goto abort;
  2254. if (unlikely(anon_vma_prepare(vma)))
  2255. goto abort;
  2256. if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL, &memcg, false))
  2257. goto abort;
  2258. /*
  2259. * The memory barrier inside __SetPageUptodate makes sure that
  2260. * preceding stores to the page contents become visible before
  2261. * the set_pte_at() write.
  2262. */
  2263. __SetPageUptodate(page);
  2264. if (is_zone_device_page(page)) {
  2265. if (is_device_private_page(page)) {
  2266. swp_entry_t swp_entry;
  2267. swp_entry = make_device_private_entry(page, vma->vm_flags & VM_WRITE);
  2268. entry = swp_entry_to_pte(swp_entry);
  2269. } else if (is_device_public_page(page)) {
  2270. entry = pte_mkold(mk_pte(page, READ_ONCE(vma->vm_page_prot)));
  2271. if (vma->vm_flags & VM_WRITE)
  2272. entry = pte_mkwrite(pte_mkdirty(entry));
  2273. entry = pte_mkdevmap(entry);
  2274. }
  2275. } else {
  2276. entry = mk_pte(page, vma->vm_page_prot);
  2277. if (vma->vm_flags & VM_WRITE)
  2278. entry = pte_mkwrite(pte_mkdirty(entry));
  2279. }
  2280. ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
  2281. if (pte_present(*ptep)) {
  2282. unsigned long pfn = pte_pfn(*ptep);
  2283. if (!is_zero_pfn(pfn)) {
  2284. pte_unmap_unlock(ptep, ptl);
  2285. mem_cgroup_cancel_charge(page, memcg, false);
  2286. goto abort;
  2287. }
  2288. flush = true;
  2289. } else if (!pte_none(*ptep)) {
  2290. pte_unmap_unlock(ptep, ptl);
  2291. mem_cgroup_cancel_charge(page, memcg, false);
  2292. goto abort;
  2293. }
  2294. /*
  2295. * Check for usefaultfd but do not deliver the fault. Instead,
  2296. * just back off.
  2297. */
  2298. if (userfaultfd_missing(vma)) {
  2299. pte_unmap_unlock(ptep, ptl);
  2300. mem_cgroup_cancel_charge(page, memcg, false);
  2301. goto abort;
  2302. }
  2303. inc_mm_counter(mm, MM_ANONPAGES);
  2304. page_add_new_anon_rmap(page, vma, addr, false);
  2305. mem_cgroup_commit_charge(page, memcg, false, false);
  2306. if (!is_zone_device_page(page))
  2307. lru_cache_add_active_or_unevictable(page, vma);
  2308. get_page(page);
  2309. if (flush) {
  2310. flush_cache_page(vma, addr, pte_pfn(*ptep));
  2311. ptep_clear_flush_notify(vma, addr, ptep);
  2312. set_pte_at_notify(mm, addr, ptep, entry);
  2313. update_mmu_cache(vma, addr, ptep);
  2314. } else {
  2315. /* No need to invalidate - it was non-present before */
  2316. set_pte_at(mm, addr, ptep, entry);
  2317. update_mmu_cache(vma, addr, ptep);
  2318. }
  2319. pte_unmap_unlock(ptep, ptl);
  2320. *src = MIGRATE_PFN_MIGRATE;
  2321. return;
  2322. abort:
  2323. *src &= ~MIGRATE_PFN_MIGRATE;
  2324. }
  2325. /*
  2326. * migrate_vma_pages() - migrate meta-data from src page to dst page
  2327. * @migrate: migrate struct containing all migration information
  2328. *
  2329. * This migrates struct page meta-data from source struct page to destination
  2330. * struct page. This effectively finishes the migration from source page to the
  2331. * destination page.
  2332. */
  2333. static void migrate_vma_pages(struct migrate_vma *migrate)
  2334. {
  2335. const unsigned long npages = migrate->npages;
  2336. const unsigned long start = migrate->start;
  2337. struct vm_area_struct *vma = migrate->vma;
  2338. struct mm_struct *mm = vma->vm_mm;
  2339. unsigned long addr, i, mmu_start;
  2340. bool notified = false;
  2341. for (i = 0, addr = start; i < npages; addr += PAGE_SIZE, i++) {
  2342. struct page *newpage = migrate_pfn_to_page(migrate->dst[i]);
  2343. struct page *page = migrate_pfn_to_page(migrate->src[i]);
  2344. struct address_space *mapping;
  2345. int r;
  2346. if (!newpage) {
  2347. migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
  2348. continue;
  2349. }
  2350. if (!page) {
  2351. if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE)) {
  2352. continue;
  2353. }
  2354. if (!notified) {
  2355. mmu_start = addr;
  2356. notified = true;
  2357. mmu_notifier_invalidate_range_start(mm,
  2358. mmu_start,
  2359. migrate->end);
  2360. }
  2361. migrate_vma_insert_page(migrate, addr, newpage,
  2362. &migrate->src[i],
  2363. &migrate->dst[i]);
  2364. continue;
  2365. }
  2366. mapping = page_mapping(page);
  2367. if (is_zone_device_page(newpage)) {
  2368. if (is_device_private_page(newpage)) {
  2369. /*
  2370. * For now only support private anonymous when
  2371. * migrating to un-addressable device memory.
  2372. */
  2373. if (mapping) {
  2374. migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
  2375. continue;
  2376. }
  2377. } else if (!is_device_public_page(newpage)) {
  2378. /*
  2379. * Other types of ZONE_DEVICE page are not
  2380. * supported.
  2381. */
  2382. migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
  2383. continue;
  2384. }
  2385. }
  2386. r = migrate_page(mapping, newpage, page, MIGRATE_SYNC_NO_COPY);
  2387. if (r != MIGRATEPAGE_SUCCESS)
  2388. migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
  2389. }
  2390. /*
  2391. * No need to double call mmu_notifier->invalidate_range() callback as
  2392. * the above ptep_clear_flush_notify() inside migrate_vma_insert_page()
  2393. * did already call it.
  2394. */
  2395. if (notified)
  2396. mmu_notifier_invalidate_range_only_end(mm, mmu_start,
  2397. migrate->end);
  2398. }
  2399. /*
  2400. * migrate_vma_finalize() - restore CPU page table entry
  2401. * @migrate: migrate struct containing all migration information
  2402. *
  2403. * This replaces the special migration pte entry with either a mapping to the
  2404. * new page if migration was successful for that page, or to the original page
  2405. * otherwise.
  2406. *
  2407. * This also unlocks the pages and puts them back on the lru, or drops the extra
  2408. * refcount, for device pages.
  2409. */
  2410. static void migrate_vma_finalize(struct migrate_vma *migrate)
  2411. {
  2412. const unsigned long npages = migrate->npages;
  2413. unsigned long i;
  2414. for (i = 0; i < npages; i++) {
  2415. struct page *newpage = migrate_pfn_to_page(migrate->dst[i]);
  2416. struct page *page = migrate_pfn_to_page(migrate->src[i]);
  2417. if (!page) {
  2418. if (newpage) {
  2419. unlock_page(newpage);
  2420. put_page(newpage);
  2421. }
  2422. continue;
  2423. }
  2424. if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE) || !newpage) {
  2425. if (newpage) {
  2426. unlock_page(newpage);
  2427. put_page(newpage);
  2428. }
  2429. newpage = page;
  2430. }
  2431. remove_migration_ptes(page, newpage, false);
  2432. unlock_page(page);
  2433. migrate->cpages--;
  2434. if (is_zone_device_page(page))
  2435. put_page(page);
  2436. else
  2437. putback_lru_page(page);
  2438. if (newpage != page) {
  2439. unlock_page(newpage);
  2440. if (is_zone_device_page(newpage))
  2441. put_page(newpage);
  2442. else
  2443. putback_lru_page(newpage);
  2444. }
  2445. }
  2446. }
  2447. /*
  2448. * migrate_vma() - migrate a range of memory inside vma
  2449. *
  2450. * @ops: migration callback for allocating destination memory and copying
  2451. * @vma: virtual memory area containing the range to be migrated
  2452. * @start: start address of the range to migrate (inclusive)
  2453. * @end: end address of the range to migrate (exclusive)
  2454. * @src: array of hmm_pfn_t containing source pfns
  2455. * @dst: array of hmm_pfn_t containing destination pfns
  2456. * @private: pointer passed back to each of the callback
  2457. * Returns: 0 on success, error code otherwise
  2458. *
  2459. * This function tries to migrate a range of memory virtual address range, using
  2460. * callbacks to allocate and copy memory from source to destination. First it
  2461. * collects all the pages backing each virtual address in the range, saving this
  2462. * inside the src array. Then it locks those pages and unmaps them. Once the pages
  2463. * are locked and unmapped, it checks whether each page is pinned or not. Pages
  2464. * that aren't pinned have the MIGRATE_PFN_MIGRATE flag set (by this function)
  2465. * in the corresponding src array entry. It then restores any pages that are
  2466. * pinned, by remapping and unlocking those pages.
  2467. *
  2468. * At this point it calls the alloc_and_copy() callback. For documentation on
  2469. * what is expected from that callback, see struct migrate_vma_ops comments in
  2470. * include/linux/migrate.h
  2471. *
  2472. * After the alloc_and_copy() callback, this function goes over each entry in
  2473. * the src array that has the MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag
  2474. * set. If the corresponding entry in dst array has MIGRATE_PFN_VALID flag set,
  2475. * then the function tries to migrate struct page information from the source
  2476. * struct page to the destination struct page. If it fails to migrate the struct
  2477. * page information, then it clears the MIGRATE_PFN_MIGRATE flag in the src
  2478. * array.
  2479. *
  2480. * At this point all successfully migrated pages have an entry in the src
  2481. * array with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag set and the dst
  2482. * array entry with MIGRATE_PFN_VALID flag set.
  2483. *
  2484. * It then calls the finalize_and_map() callback. See comments for "struct
  2485. * migrate_vma_ops", in include/linux/migrate.h for details about
  2486. * finalize_and_map() behavior.
  2487. *
  2488. * After the finalize_and_map() callback, for successfully migrated pages, this
  2489. * function updates the CPU page table to point to new pages, otherwise it
  2490. * restores the CPU page table to point to the original source pages.
  2491. *
  2492. * Function returns 0 after the above steps, even if no pages were migrated
  2493. * (The function only returns an error if any of the arguments are invalid.)
  2494. *
  2495. * Both src and dst array must be big enough for (end - start) >> PAGE_SHIFT
  2496. * unsigned long entries.
  2497. */
  2498. int migrate_vma(const struct migrate_vma_ops *ops,
  2499. struct vm_area_struct *vma,
  2500. unsigned long start,
  2501. unsigned long end,
  2502. unsigned long *src,
  2503. unsigned long *dst,
  2504. void *private)
  2505. {
  2506. struct migrate_vma migrate;
  2507. /* Sanity check the arguments */
  2508. start &= PAGE_MASK;
  2509. end &= PAGE_MASK;
  2510. if (!vma || is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL) ||
  2511. vma_is_dax(vma))
  2512. return -EINVAL;
  2513. if (start < vma->vm_start || start >= vma->vm_end)
  2514. return -EINVAL;
  2515. if (end <= vma->vm_start || end > vma->vm_end)
  2516. return -EINVAL;
  2517. if (!ops || !src || !dst || start >= end)
  2518. return -EINVAL;
  2519. memset(src, 0, sizeof(*src) * ((end - start) >> PAGE_SHIFT));
  2520. migrate.src = src;
  2521. migrate.dst = dst;
  2522. migrate.start = start;
  2523. migrate.npages = 0;
  2524. migrate.cpages = 0;
  2525. migrate.end = end;
  2526. migrate.vma = vma;
  2527. /* Collect, and try to unmap source pages */
  2528. migrate_vma_collect(&migrate);
  2529. if (!migrate.cpages)
  2530. return 0;
  2531. /* Lock and isolate page */
  2532. migrate_vma_prepare(&migrate);
  2533. if (!migrate.cpages)
  2534. return 0;
  2535. /* Unmap pages */
  2536. migrate_vma_unmap(&migrate);
  2537. if (!migrate.cpages)
  2538. return 0;
  2539. /*
  2540. * At this point pages are locked and unmapped, and thus they have
  2541. * stable content and can safely be copied to destination memory that
  2542. * is allocated by the callback.
  2543. *
  2544. * Note that migration can fail in migrate_vma_struct_page() for each
  2545. * individual page.
  2546. */
  2547. ops->alloc_and_copy(vma, src, dst, start, end, private);
  2548. /* This does the real migration of struct page */
  2549. migrate_vma_pages(&migrate);
  2550. ops->finalize_and_map(vma, src, dst, start, end, private);
  2551. /* Unlock and remap pages */
  2552. migrate_vma_finalize(&migrate);
  2553. return 0;
  2554. }
  2555. EXPORT_SYMBOL(migrate_vma);
  2556. #endif /* defined(MIGRATE_VMA_HELPER) */