migrate.c 78 KB

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