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 defined(CONFIG_ARM) && \
  986. defined(GCC_VERSION) && GCC_VERSION < 40900 && GCC_VERSION >= 40700
  987. #define ICE_noinline noinline
  988. #else
  989. #define ICE_noinline
  990. #endif
  991. /*
  992. * Obtain the lock on page, remove all ptes and migrate the page
  993. * to the newly allocated page in newpage.
  994. */
  995. static ICE_noinline int unmap_and_move(new_page_t get_new_page,
  996. free_page_t put_new_page,
  997. unsigned long private, struct page *page,
  998. int force, enum migrate_mode mode,
  999. enum migrate_reason reason)
  1000. {
  1001. int rc = MIGRATEPAGE_SUCCESS;
  1002. struct page *newpage;
  1003. if (!thp_migration_supported() && PageTransHuge(page))
  1004. return -ENOMEM;
  1005. newpage = get_new_page(page, private);
  1006. if (!newpage)
  1007. return -ENOMEM;
  1008. if (page_count(page) == 1) {
  1009. /* page was freed from under us. So we are done. */
  1010. ClearPageActive(page);
  1011. ClearPageUnevictable(page);
  1012. if (unlikely(__PageMovable(page))) {
  1013. lock_page(page);
  1014. if (!PageMovable(page))
  1015. __ClearPageIsolated(page);
  1016. unlock_page(page);
  1017. }
  1018. if (put_new_page)
  1019. put_new_page(newpage, private);
  1020. else
  1021. put_page(newpage);
  1022. goto out;
  1023. }
  1024. rc = __unmap_and_move(page, newpage, force, mode);
  1025. if (rc == MIGRATEPAGE_SUCCESS)
  1026. set_page_owner_migrate_reason(newpage, reason);
  1027. out:
  1028. if (rc != -EAGAIN) {
  1029. /*
  1030. * A page that has been migrated has all references
  1031. * removed and will be freed. A page that has not been
  1032. * migrated will have kepts its references and be
  1033. * restored.
  1034. */
  1035. list_del(&page->lru);
  1036. /*
  1037. * Compaction can migrate also non-LRU pages which are
  1038. * not accounted to NR_ISOLATED_*. They can be recognized
  1039. * as __PageMovable
  1040. */
  1041. if (likely(!__PageMovable(page)))
  1042. mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
  1043. page_is_file_cache(page), -hpage_nr_pages(page));
  1044. }
  1045. /*
  1046. * If migration is successful, releases reference grabbed during
  1047. * isolation. Otherwise, restore the page to right list unless
  1048. * we want to retry.
  1049. */
  1050. if (rc == MIGRATEPAGE_SUCCESS) {
  1051. put_page(page);
  1052. if (reason == MR_MEMORY_FAILURE) {
  1053. /*
  1054. * Set PG_HWPoison on just freed page
  1055. * intentionally. Although it's rather weird,
  1056. * it's how HWPoison flag works at the moment.
  1057. */
  1058. if (set_hwpoison_free_buddy_page(page))
  1059. num_poisoned_pages_inc();
  1060. }
  1061. } else {
  1062. if (rc != -EAGAIN) {
  1063. if (likely(!__PageMovable(page))) {
  1064. putback_lru_page(page);
  1065. goto put_new;
  1066. }
  1067. lock_page(page);
  1068. if (PageMovable(page))
  1069. putback_movable_page(page);
  1070. else
  1071. __ClearPageIsolated(page);
  1072. unlock_page(page);
  1073. put_page(page);
  1074. }
  1075. put_new:
  1076. if (put_new_page)
  1077. put_new_page(newpage, private);
  1078. else
  1079. put_page(newpage);
  1080. }
  1081. return rc;
  1082. }
  1083. /*
  1084. * Counterpart of unmap_and_move_page() for hugepage migration.
  1085. *
  1086. * This function doesn't wait the completion of hugepage I/O
  1087. * because there is no race between I/O and migration for hugepage.
  1088. * Note that currently hugepage I/O occurs only in direct I/O
  1089. * where no lock is held and PG_writeback is irrelevant,
  1090. * and writeback status of all subpages are counted in the reference
  1091. * count of the head page (i.e. if all subpages of a 2MB hugepage are
  1092. * under direct I/O, the reference of the head page is 512 and a bit more.)
  1093. * This means that when we try to migrate hugepage whose subpages are
  1094. * doing direct I/O, some references remain after try_to_unmap() and
  1095. * hugepage migration fails without data corruption.
  1096. *
  1097. * There is also no race when direct I/O is issued on the page under migration,
  1098. * because then pte is replaced with migration swap entry and direct I/O code
  1099. * will wait in the page fault for migration to complete.
  1100. */
  1101. static int unmap_and_move_huge_page(new_page_t get_new_page,
  1102. free_page_t put_new_page, unsigned long private,
  1103. struct page *hpage, int force,
  1104. enum migrate_mode mode, int reason)
  1105. {
  1106. int rc = -EAGAIN;
  1107. int page_was_mapped = 0;
  1108. struct page *new_hpage;
  1109. struct anon_vma *anon_vma = NULL;
  1110. /*
  1111. * Movability of hugepages depends on architectures and hugepage size.
  1112. * This check is necessary because some callers of hugepage migration
  1113. * like soft offline and memory hotremove don't walk through page
  1114. * tables or check whether the hugepage is pmd-based or not before
  1115. * kicking migration.
  1116. */
  1117. if (!hugepage_migration_supported(page_hstate(hpage))) {
  1118. putback_active_hugepage(hpage);
  1119. return -ENOSYS;
  1120. }
  1121. new_hpage = get_new_page(hpage, private);
  1122. if (!new_hpage)
  1123. return -ENOMEM;
  1124. if (!trylock_page(hpage)) {
  1125. if (!force)
  1126. goto out;
  1127. switch (mode) {
  1128. case MIGRATE_SYNC:
  1129. case MIGRATE_SYNC_NO_COPY:
  1130. break;
  1131. default:
  1132. goto out;
  1133. }
  1134. lock_page(hpage);
  1135. }
  1136. if (PageAnon(hpage))
  1137. anon_vma = page_get_anon_vma(hpage);
  1138. if (unlikely(!trylock_page(new_hpage)))
  1139. goto put_anon;
  1140. if (page_mapped(hpage)) {
  1141. try_to_unmap(hpage,
  1142. TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
  1143. page_was_mapped = 1;
  1144. }
  1145. if (!page_mapped(hpage))
  1146. rc = move_to_new_page(new_hpage, hpage, mode);
  1147. if (page_was_mapped)
  1148. remove_migration_ptes(hpage,
  1149. rc == MIGRATEPAGE_SUCCESS ? new_hpage : hpage, false);
  1150. unlock_page(new_hpage);
  1151. put_anon:
  1152. if (anon_vma)
  1153. put_anon_vma(anon_vma);
  1154. if (rc == MIGRATEPAGE_SUCCESS) {
  1155. move_hugetlb_state(hpage, new_hpage, reason);
  1156. put_new_page = NULL;
  1157. }
  1158. unlock_page(hpage);
  1159. out:
  1160. if (rc != -EAGAIN)
  1161. putback_active_hugepage(hpage);
  1162. /*
  1163. * If migration was not successful and there's a freeing callback, use
  1164. * it. Otherwise, put_page() will drop the reference grabbed during
  1165. * isolation.
  1166. */
  1167. if (put_new_page)
  1168. put_new_page(new_hpage, private);
  1169. else
  1170. putback_active_hugepage(new_hpage);
  1171. return rc;
  1172. }
  1173. /*
  1174. * migrate_pages - migrate the pages specified in a list, to the free pages
  1175. * supplied as the target for the page migration
  1176. *
  1177. * @from: The list of pages to be migrated.
  1178. * @get_new_page: The function used to allocate free pages to be used
  1179. * as the target of the page migration.
  1180. * @put_new_page: The function used to free target pages if migration
  1181. * fails, or NULL if no special handling is necessary.
  1182. * @private: Private data to be passed on to get_new_page()
  1183. * @mode: The migration mode that specifies the constraints for
  1184. * page migration, if any.
  1185. * @reason: The reason for page migration.
  1186. *
  1187. * The function returns after 10 attempts or if no pages are movable any more
  1188. * because the list has become empty or no retryable pages exist any more.
  1189. * The caller should call putback_movable_pages() to return pages to the LRU
  1190. * or free list only if ret != 0.
  1191. *
  1192. * Returns the number of pages that were not migrated, or an error code.
  1193. */
  1194. int migrate_pages(struct list_head *from, new_page_t get_new_page,
  1195. free_page_t put_new_page, unsigned long private,
  1196. enum migrate_mode mode, int reason)
  1197. {
  1198. int retry = 1;
  1199. int nr_failed = 0;
  1200. int nr_succeeded = 0;
  1201. int pass = 0;
  1202. struct page *page;
  1203. struct page *page2;
  1204. int swapwrite = current->flags & PF_SWAPWRITE;
  1205. int rc;
  1206. if (!swapwrite)
  1207. current->flags |= PF_SWAPWRITE;
  1208. for(pass = 0; pass < 10 && retry; pass++) {
  1209. retry = 0;
  1210. list_for_each_entry_safe(page, page2, from, lru) {
  1211. retry:
  1212. cond_resched();
  1213. if (PageHuge(page))
  1214. rc = unmap_and_move_huge_page(get_new_page,
  1215. put_new_page, private, page,
  1216. pass > 2, mode, reason);
  1217. else
  1218. rc = unmap_and_move(get_new_page, put_new_page,
  1219. private, page, pass > 2, mode,
  1220. reason);
  1221. switch(rc) {
  1222. case -ENOMEM:
  1223. /*
  1224. * THP migration might be unsupported or the
  1225. * allocation could've failed so we should
  1226. * retry on the same page with the THP split
  1227. * to base pages.
  1228. *
  1229. * Head page is retried immediately and tail
  1230. * pages are added to the tail of the list so
  1231. * we encounter them after the rest of the list
  1232. * is processed.
  1233. */
  1234. if (PageTransHuge(page)) {
  1235. lock_page(page);
  1236. rc = split_huge_page_to_list(page, from);
  1237. unlock_page(page);
  1238. if (!rc) {
  1239. list_safe_reset_next(page, page2, lru);
  1240. goto retry;
  1241. }
  1242. }
  1243. nr_failed++;
  1244. goto out;
  1245. case -EAGAIN:
  1246. retry++;
  1247. break;
  1248. case MIGRATEPAGE_SUCCESS:
  1249. nr_succeeded++;
  1250. break;
  1251. default:
  1252. /*
  1253. * Permanent failure (-EBUSY, -ENOSYS, etc.):
  1254. * unlike -EAGAIN case, the failed page is
  1255. * removed from migration page list and not
  1256. * retried in the next outer loop.
  1257. */
  1258. nr_failed++;
  1259. break;
  1260. }
  1261. }
  1262. }
  1263. nr_failed += retry;
  1264. rc = nr_failed;
  1265. out:
  1266. if (nr_succeeded)
  1267. count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
  1268. if (nr_failed)
  1269. count_vm_events(PGMIGRATE_FAIL, nr_failed);
  1270. trace_mm_migrate_pages(nr_succeeded, nr_failed, mode, reason);
  1271. if (!swapwrite)
  1272. current->flags &= ~PF_SWAPWRITE;
  1273. return rc;
  1274. }
  1275. #ifdef CONFIG_NUMA
  1276. static int store_status(int __user *status, int start, int value, int nr)
  1277. {
  1278. while (nr-- > 0) {
  1279. if (put_user(value, status + start))
  1280. return -EFAULT;
  1281. start++;
  1282. }
  1283. return 0;
  1284. }
  1285. static int do_move_pages_to_node(struct mm_struct *mm,
  1286. struct list_head *pagelist, int node)
  1287. {
  1288. int err;
  1289. if (list_empty(pagelist))
  1290. return 0;
  1291. err = migrate_pages(pagelist, alloc_new_node_page, NULL, node,
  1292. MIGRATE_SYNC, MR_SYSCALL);
  1293. if (err)
  1294. putback_movable_pages(pagelist);
  1295. return err;
  1296. }
  1297. /*
  1298. * Resolves the given address to a struct page, isolates it from the LRU and
  1299. * puts it to the given pagelist.
  1300. * Returns -errno if the page cannot be found/isolated or 0 when it has been
  1301. * queued or the page doesn't need to be migrated because it is already on
  1302. * the target node
  1303. */
  1304. static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
  1305. int node, struct list_head *pagelist, bool migrate_all)
  1306. {
  1307. struct vm_area_struct *vma;
  1308. struct page *page;
  1309. unsigned int follflags;
  1310. int err;
  1311. down_read(&mm->mmap_sem);
  1312. err = -EFAULT;
  1313. vma = find_vma(mm, addr);
  1314. if (!vma || addr < vma->vm_start || !vma_migratable(vma))
  1315. goto out;
  1316. /* FOLL_DUMP to ignore special (like zero) pages */
  1317. follflags = FOLL_GET | FOLL_DUMP;
  1318. page = follow_page(vma, addr, follflags);
  1319. err = PTR_ERR(page);
  1320. if (IS_ERR(page))
  1321. goto out;
  1322. err = -ENOENT;
  1323. if (!page)
  1324. goto out;
  1325. err = 0;
  1326. if (page_to_nid(page) == node)
  1327. goto out_putpage;
  1328. err = -EACCES;
  1329. if (page_mapcount(page) > 1 && !migrate_all)
  1330. goto out_putpage;
  1331. if (PageHuge(page)) {
  1332. if (PageHead(page)) {
  1333. isolate_huge_page(page, pagelist);
  1334. err = 0;
  1335. }
  1336. } else {
  1337. struct page *head;
  1338. head = compound_head(page);
  1339. err = isolate_lru_page(head);
  1340. if (err)
  1341. goto out_putpage;
  1342. err = 0;
  1343. list_add_tail(&head->lru, pagelist);
  1344. mod_node_page_state(page_pgdat(head),
  1345. NR_ISOLATED_ANON + page_is_file_cache(head),
  1346. hpage_nr_pages(head));
  1347. }
  1348. out_putpage:
  1349. /*
  1350. * Either remove the duplicate refcount from
  1351. * isolate_lru_page() or drop the page ref if it was
  1352. * not isolated.
  1353. */
  1354. put_page(page);
  1355. out:
  1356. up_read(&mm->mmap_sem);
  1357. return err;
  1358. }
  1359. /*
  1360. * Migrate an array of page address onto an array of nodes and fill
  1361. * the corresponding array of status.
  1362. */
  1363. static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
  1364. unsigned long nr_pages,
  1365. const void __user * __user *pages,
  1366. const int __user *nodes,
  1367. int __user *status, int flags)
  1368. {
  1369. int current_node = NUMA_NO_NODE;
  1370. LIST_HEAD(pagelist);
  1371. int start, i;
  1372. int err = 0, err1;
  1373. migrate_prep();
  1374. for (i = start = 0; i < nr_pages; i++) {
  1375. const void __user *p;
  1376. unsigned long addr;
  1377. int node;
  1378. err = -EFAULT;
  1379. if (get_user(p, pages + i))
  1380. goto out_flush;
  1381. if (get_user(node, nodes + i))
  1382. goto out_flush;
  1383. addr = (unsigned long)p;
  1384. err = -ENODEV;
  1385. if (node < 0 || node >= MAX_NUMNODES)
  1386. goto out_flush;
  1387. if (!node_state(node, N_MEMORY))
  1388. goto out_flush;
  1389. err = -EACCES;
  1390. if (!node_isset(node, task_nodes))
  1391. goto out_flush;
  1392. if (current_node == NUMA_NO_NODE) {
  1393. current_node = node;
  1394. start = i;
  1395. } else if (node != current_node) {
  1396. err = do_move_pages_to_node(mm, &pagelist, current_node);
  1397. if (err)
  1398. goto out;
  1399. err = store_status(status, start, current_node, i - start);
  1400. if (err)
  1401. goto out;
  1402. start = i;
  1403. current_node = node;
  1404. }
  1405. /*
  1406. * Errors in the page lookup or isolation are not fatal and we simply
  1407. * report them via status
  1408. */
  1409. err = add_page_for_migration(mm, addr, current_node,
  1410. &pagelist, flags & MPOL_MF_MOVE_ALL);
  1411. if (!err)
  1412. continue;
  1413. err = store_status(status, i, err, 1);
  1414. if (err)
  1415. goto out_flush;
  1416. err = do_move_pages_to_node(mm, &pagelist, current_node);
  1417. if (err)
  1418. goto out;
  1419. if (i > start) {
  1420. err = store_status(status, start, current_node, i - start);
  1421. if (err)
  1422. goto out;
  1423. }
  1424. current_node = NUMA_NO_NODE;
  1425. }
  1426. out_flush:
  1427. if (list_empty(&pagelist))
  1428. return err;
  1429. /* Make sure we do not overwrite the existing error */
  1430. err1 = do_move_pages_to_node(mm, &pagelist, current_node);
  1431. if (!err1)
  1432. err1 = store_status(status, start, current_node, i - start);
  1433. if (!err)
  1434. err = err1;
  1435. out:
  1436. return err;
  1437. }
  1438. /*
  1439. * Determine the nodes of an array of pages and store it in an array of status.
  1440. */
  1441. static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
  1442. const void __user **pages, int *status)
  1443. {
  1444. unsigned long i;
  1445. down_read(&mm->mmap_sem);
  1446. for (i = 0; i < nr_pages; i++) {
  1447. unsigned long addr = (unsigned long)(*pages);
  1448. struct vm_area_struct *vma;
  1449. struct page *page;
  1450. int err = -EFAULT;
  1451. vma = find_vma(mm, addr);
  1452. if (!vma || addr < vma->vm_start)
  1453. goto set_status;
  1454. /* FOLL_DUMP to ignore special (like zero) pages */
  1455. page = follow_page(vma, addr, FOLL_DUMP);
  1456. err = PTR_ERR(page);
  1457. if (IS_ERR(page))
  1458. goto set_status;
  1459. err = page ? page_to_nid(page) : -ENOENT;
  1460. set_status:
  1461. *status = err;
  1462. pages++;
  1463. status++;
  1464. }
  1465. up_read(&mm->mmap_sem);
  1466. }
  1467. /*
  1468. * Determine the nodes of a user array of pages and store it in
  1469. * a user array of status.
  1470. */
  1471. static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
  1472. const void __user * __user *pages,
  1473. int __user *status)
  1474. {
  1475. #define DO_PAGES_STAT_CHUNK_NR 16
  1476. const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
  1477. int chunk_status[DO_PAGES_STAT_CHUNK_NR];
  1478. while (nr_pages) {
  1479. unsigned long chunk_nr;
  1480. chunk_nr = nr_pages;
  1481. if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
  1482. chunk_nr = DO_PAGES_STAT_CHUNK_NR;
  1483. if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
  1484. break;
  1485. do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
  1486. if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
  1487. break;
  1488. pages += chunk_nr;
  1489. status += chunk_nr;
  1490. nr_pages -= chunk_nr;
  1491. }
  1492. return nr_pages ? -EFAULT : 0;
  1493. }
  1494. /*
  1495. * Move a list of pages in the address space of the currently executing
  1496. * process.
  1497. */
  1498. static int kernel_move_pages(pid_t pid, unsigned long nr_pages,
  1499. const void __user * __user *pages,
  1500. const int __user *nodes,
  1501. int __user *status, int flags)
  1502. {
  1503. struct task_struct *task;
  1504. struct mm_struct *mm;
  1505. int err;
  1506. nodemask_t task_nodes;
  1507. /* Check flags */
  1508. if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
  1509. return -EINVAL;
  1510. if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
  1511. return -EPERM;
  1512. /* Find the mm_struct */
  1513. rcu_read_lock();
  1514. task = pid ? find_task_by_vpid(pid) : current;
  1515. if (!task) {
  1516. rcu_read_unlock();
  1517. return -ESRCH;
  1518. }
  1519. get_task_struct(task);
  1520. /*
  1521. * Check if this process has the right to modify the specified
  1522. * process. Use the regular "ptrace_may_access()" checks.
  1523. */
  1524. if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
  1525. rcu_read_unlock();
  1526. err = -EPERM;
  1527. goto out;
  1528. }
  1529. rcu_read_unlock();
  1530. err = security_task_movememory(task);
  1531. if (err)
  1532. goto out;
  1533. task_nodes = cpuset_mems_allowed(task);
  1534. mm = get_task_mm(task);
  1535. put_task_struct(task);
  1536. if (!mm)
  1537. return -EINVAL;
  1538. if (nodes)
  1539. err = do_pages_move(mm, task_nodes, nr_pages, pages,
  1540. nodes, status, flags);
  1541. else
  1542. err = do_pages_stat(mm, nr_pages, pages, status);
  1543. mmput(mm);
  1544. return err;
  1545. out:
  1546. put_task_struct(task);
  1547. return err;
  1548. }
  1549. SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
  1550. const void __user * __user *, pages,
  1551. const int __user *, nodes,
  1552. int __user *, status, int, flags)
  1553. {
  1554. return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags);
  1555. }
  1556. #ifdef CONFIG_COMPAT
  1557. COMPAT_SYSCALL_DEFINE6(move_pages, pid_t, pid, compat_ulong_t, nr_pages,
  1558. compat_uptr_t __user *, pages32,
  1559. const int __user *, nodes,
  1560. int __user *, status,
  1561. int, flags)
  1562. {
  1563. const void __user * __user *pages;
  1564. int i;
  1565. pages = compat_alloc_user_space(nr_pages * sizeof(void *));
  1566. for (i = 0; i < nr_pages; i++) {
  1567. compat_uptr_t p;
  1568. if (get_user(p, pages32 + i) ||
  1569. put_user(compat_ptr(p), pages + i))
  1570. return -EFAULT;
  1571. }
  1572. return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags);
  1573. }
  1574. #endif /* CONFIG_COMPAT */
  1575. #ifdef CONFIG_NUMA_BALANCING
  1576. /*
  1577. * Returns true if this is a safe migration target node for misplaced NUMA
  1578. * pages. Currently it only checks the watermarks which crude
  1579. */
  1580. static bool migrate_balanced_pgdat(struct pglist_data *pgdat,
  1581. unsigned long nr_migrate_pages)
  1582. {
  1583. int z;
  1584. for (z = pgdat->nr_zones - 1; z >= 0; z--) {
  1585. struct zone *zone = pgdat->node_zones + z;
  1586. if (!populated_zone(zone))
  1587. continue;
  1588. /* Avoid waking kswapd by allocating pages_to_migrate pages. */
  1589. if (!zone_watermark_ok(zone, 0,
  1590. high_wmark_pages(zone) +
  1591. nr_migrate_pages,
  1592. 0, 0))
  1593. continue;
  1594. return true;
  1595. }
  1596. return false;
  1597. }
  1598. static struct page *alloc_misplaced_dst_page(struct page *page,
  1599. unsigned long data)
  1600. {
  1601. int nid = (int) data;
  1602. struct page *newpage;
  1603. newpage = __alloc_pages_node(nid,
  1604. (GFP_HIGHUSER_MOVABLE |
  1605. __GFP_THISNODE | __GFP_NOMEMALLOC |
  1606. __GFP_NORETRY | __GFP_NOWARN) &
  1607. ~__GFP_RECLAIM, 0);
  1608. return newpage;
  1609. }
  1610. /*
  1611. * page migration rate limiting control.
  1612. * Do not migrate more than @pages_to_migrate in a @migrate_interval_millisecs
  1613. * window of time. Default here says do not migrate more than 1280M per second.
  1614. */
  1615. static unsigned int migrate_interval_millisecs __read_mostly = 100;
  1616. static unsigned int ratelimit_pages __read_mostly = 128 << (20 - PAGE_SHIFT);
  1617. /* Returns true if the node is migrate rate-limited after the update */
  1618. static bool numamigrate_update_ratelimit(pg_data_t *pgdat,
  1619. unsigned long nr_pages)
  1620. {
  1621. /*
  1622. * Rate-limit the amount of data that is being migrated to a node.
  1623. * Optimal placement is no good if the memory bus is saturated and
  1624. * all the time is being spent migrating!
  1625. */
  1626. if (time_after(jiffies, pgdat->numabalancing_migrate_next_window)) {
  1627. spin_lock(&pgdat->numabalancing_migrate_lock);
  1628. pgdat->numabalancing_migrate_nr_pages = 0;
  1629. pgdat->numabalancing_migrate_next_window = jiffies +
  1630. msecs_to_jiffies(migrate_interval_millisecs);
  1631. spin_unlock(&pgdat->numabalancing_migrate_lock);
  1632. }
  1633. if (pgdat->numabalancing_migrate_nr_pages > ratelimit_pages) {
  1634. trace_mm_numa_migrate_ratelimit(current, pgdat->node_id,
  1635. nr_pages);
  1636. return true;
  1637. }
  1638. /*
  1639. * This is an unlocked non-atomic update so errors are possible.
  1640. * The consequences are failing to migrate when we potentiall should
  1641. * have which is not severe enough to warrant locking. If it is ever
  1642. * a problem, it can be converted to a per-cpu counter.
  1643. */
  1644. pgdat->numabalancing_migrate_nr_pages += nr_pages;
  1645. return false;
  1646. }
  1647. static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
  1648. {
  1649. int page_lru;
  1650. VM_BUG_ON_PAGE(compound_order(page) && !PageTransHuge(page), page);
  1651. /* Avoid migrating to a node that is nearly full */
  1652. if (!migrate_balanced_pgdat(pgdat, 1UL << compound_order(page)))
  1653. return 0;
  1654. if (isolate_lru_page(page))
  1655. return 0;
  1656. /*
  1657. * migrate_misplaced_transhuge_page() skips page migration's usual
  1658. * check on page_count(), so we must do it here, now that the page
  1659. * has been isolated: a GUP pin, or any other pin, prevents migration.
  1660. * The expected page count is 3: 1 for page's mapcount and 1 for the
  1661. * caller's pin and 1 for the reference taken by isolate_lru_page().
  1662. */
  1663. if (PageTransHuge(page) && page_count(page) != 3) {
  1664. putback_lru_page(page);
  1665. return 0;
  1666. }
  1667. page_lru = page_is_file_cache(page);
  1668. mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_lru,
  1669. hpage_nr_pages(page));
  1670. /*
  1671. * Isolating the page has taken another reference, so the
  1672. * caller's reference can be safely dropped without the page
  1673. * disappearing underneath us during migration.
  1674. */
  1675. put_page(page);
  1676. return 1;
  1677. }
  1678. bool pmd_trans_migrating(pmd_t pmd)
  1679. {
  1680. struct page *page = pmd_page(pmd);
  1681. return PageLocked(page);
  1682. }
  1683. /*
  1684. * Attempt to migrate a misplaced page to the specified destination
  1685. * node. Caller is expected to have an elevated reference count on
  1686. * the page that will be dropped by this function before returning.
  1687. */
  1688. int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma,
  1689. int node)
  1690. {
  1691. pg_data_t *pgdat = NODE_DATA(node);
  1692. int isolated;
  1693. int nr_remaining;
  1694. LIST_HEAD(migratepages);
  1695. /*
  1696. * Don't migrate file pages that are mapped in multiple processes
  1697. * with execute permissions as they are probably shared libraries.
  1698. */
  1699. if (page_mapcount(page) != 1 && page_is_file_cache(page) &&
  1700. (vma->vm_flags & VM_EXEC))
  1701. goto out;
  1702. /*
  1703. * Also do not migrate dirty pages as not all filesystems can move
  1704. * dirty pages in MIGRATE_ASYNC mode which is a waste of cycles.
  1705. */
  1706. if (page_is_file_cache(page) && PageDirty(page))
  1707. goto out;
  1708. /*
  1709. * Rate-limit the amount of data that is being migrated to a node.
  1710. * Optimal placement is no good if the memory bus is saturated and
  1711. * all the time is being spent migrating!
  1712. */
  1713. if (numamigrate_update_ratelimit(pgdat, 1))
  1714. goto out;
  1715. isolated = numamigrate_isolate_page(pgdat, page);
  1716. if (!isolated)
  1717. goto out;
  1718. list_add(&page->lru, &migratepages);
  1719. nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_page,
  1720. NULL, node, MIGRATE_ASYNC,
  1721. MR_NUMA_MISPLACED);
  1722. if (nr_remaining) {
  1723. if (!list_empty(&migratepages)) {
  1724. list_del(&page->lru);
  1725. dec_node_page_state(page, NR_ISOLATED_ANON +
  1726. page_is_file_cache(page));
  1727. putback_lru_page(page);
  1728. }
  1729. isolated = 0;
  1730. } else
  1731. count_vm_numa_event(NUMA_PAGE_MIGRATE);
  1732. BUG_ON(!list_empty(&migratepages));
  1733. return isolated;
  1734. out:
  1735. put_page(page);
  1736. return 0;
  1737. }
  1738. #endif /* CONFIG_NUMA_BALANCING */
  1739. #if defined(CONFIG_NUMA_BALANCING) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
  1740. /*
  1741. * Migrates a THP to a given target node. page must be locked and is unlocked
  1742. * before returning.
  1743. */
  1744. int migrate_misplaced_transhuge_page(struct mm_struct *mm,
  1745. struct vm_area_struct *vma,
  1746. pmd_t *pmd, pmd_t entry,
  1747. unsigned long address,
  1748. struct page *page, int node)
  1749. {
  1750. spinlock_t *ptl;
  1751. pg_data_t *pgdat = NODE_DATA(node);
  1752. int isolated = 0;
  1753. struct page *new_page = NULL;
  1754. int page_lru = page_is_file_cache(page);
  1755. unsigned long mmun_start = address & HPAGE_PMD_MASK;
  1756. unsigned long mmun_end = mmun_start + HPAGE_PMD_SIZE;
  1757. /*
  1758. * Rate-limit the amount of data that is being migrated to a node.
  1759. * Optimal placement is no good if the memory bus is saturated and
  1760. * all the time is being spent migrating!
  1761. */
  1762. if (numamigrate_update_ratelimit(pgdat, HPAGE_PMD_NR))
  1763. goto out_dropref;
  1764. new_page = alloc_pages_node(node,
  1765. (GFP_TRANSHUGE_LIGHT | __GFP_THISNODE),
  1766. HPAGE_PMD_ORDER);
  1767. if (!new_page)
  1768. goto out_fail;
  1769. prep_transhuge_page(new_page);
  1770. isolated = numamigrate_isolate_page(pgdat, page);
  1771. if (!isolated) {
  1772. put_page(new_page);
  1773. goto out_fail;
  1774. }
  1775. /* Prepare a page as a migration target */
  1776. __SetPageLocked(new_page);
  1777. if (PageSwapBacked(page))
  1778. __SetPageSwapBacked(new_page);
  1779. /* anon mapping, we can simply copy page->mapping to the new page: */
  1780. new_page->mapping = page->mapping;
  1781. new_page->index = page->index;
  1782. migrate_page_copy(new_page, page);
  1783. WARN_ON(PageLRU(new_page));
  1784. /* Recheck the target PMD */
  1785. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  1786. ptl = pmd_lock(mm, pmd);
  1787. if (unlikely(!pmd_same(*pmd, entry) || !page_ref_freeze(page, 2))) {
  1788. spin_unlock(ptl);
  1789. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  1790. /* Reverse changes made by migrate_page_copy() */
  1791. if (TestClearPageActive(new_page))
  1792. SetPageActive(page);
  1793. if (TestClearPageUnevictable(new_page))
  1794. SetPageUnevictable(page);
  1795. unlock_page(new_page);
  1796. put_page(new_page); /* Free it */
  1797. /* Retake the callers reference and putback on LRU */
  1798. get_page(page);
  1799. putback_lru_page(page);
  1800. mod_node_page_state(page_pgdat(page),
  1801. NR_ISOLATED_ANON + page_lru, -HPAGE_PMD_NR);
  1802. goto out_unlock;
  1803. }
  1804. entry = mk_huge_pmd(new_page, vma->vm_page_prot);
  1805. entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
  1806. /*
  1807. * Clear the old entry under pagetable lock and establish the new PTE.
  1808. * Any parallel GUP will either observe the old page blocking on the
  1809. * page lock, block on the page table lock or observe the new page.
  1810. * The SetPageUptodate on the new page and page_add_new_anon_rmap
  1811. * guarantee the copy is visible before the pagetable update.
  1812. */
  1813. flush_cache_range(vma, mmun_start, mmun_end);
  1814. page_add_anon_rmap(new_page, vma, mmun_start, true);
  1815. pmdp_huge_clear_flush_notify(vma, mmun_start, pmd);
  1816. set_pmd_at(mm, mmun_start, pmd, entry);
  1817. update_mmu_cache_pmd(vma, address, &entry);
  1818. page_ref_unfreeze(page, 2);
  1819. mlock_migrate_page(new_page, page);
  1820. page_remove_rmap(page, true);
  1821. set_page_owner_migrate_reason(new_page, MR_NUMA_MISPLACED);
  1822. spin_unlock(ptl);
  1823. /*
  1824. * No need to double call mmu_notifier->invalidate_range() callback as
  1825. * the above pmdp_huge_clear_flush_notify() did already call it.
  1826. */
  1827. mmu_notifier_invalidate_range_only_end(mm, mmun_start, mmun_end);
  1828. /* Take an "isolate" reference and put new page on the LRU. */
  1829. get_page(new_page);
  1830. putback_lru_page(new_page);
  1831. unlock_page(new_page);
  1832. unlock_page(page);
  1833. put_page(page); /* Drop the rmap reference */
  1834. put_page(page); /* Drop the LRU isolation reference */
  1835. count_vm_events(PGMIGRATE_SUCCESS, HPAGE_PMD_NR);
  1836. count_vm_numa_events(NUMA_PAGE_MIGRATE, HPAGE_PMD_NR);
  1837. mod_node_page_state(page_pgdat(page),
  1838. NR_ISOLATED_ANON + page_lru,
  1839. -HPAGE_PMD_NR);
  1840. return isolated;
  1841. out_fail:
  1842. count_vm_events(PGMIGRATE_FAIL, HPAGE_PMD_NR);
  1843. out_dropref:
  1844. ptl = pmd_lock(mm, pmd);
  1845. if (pmd_same(*pmd, entry)) {
  1846. entry = pmd_modify(entry, vma->vm_page_prot);
  1847. set_pmd_at(mm, mmun_start, pmd, entry);
  1848. update_mmu_cache_pmd(vma, address, &entry);
  1849. }
  1850. spin_unlock(ptl);
  1851. out_unlock:
  1852. unlock_page(page);
  1853. put_page(page);
  1854. return 0;
  1855. }
  1856. #endif /* CONFIG_NUMA_BALANCING */
  1857. #endif /* CONFIG_NUMA */
  1858. #if defined(CONFIG_MIGRATE_VMA_HELPER)
  1859. struct migrate_vma {
  1860. struct vm_area_struct *vma;
  1861. unsigned long *dst;
  1862. unsigned long *src;
  1863. unsigned long cpages;
  1864. unsigned long npages;
  1865. unsigned long start;
  1866. unsigned long end;
  1867. };
  1868. static int migrate_vma_collect_hole(unsigned long start,
  1869. unsigned long end,
  1870. struct mm_walk *walk)
  1871. {
  1872. struct migrate_vma *migrate = walk->private;
  1873. unsigned long addr;
  1874. for (addr = start & PAGE_MASK; addr < end; addr += PAGE_SIZE) {
  1875. migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE;
  1876. migrate->dst[migrate->npages] = 0;
  1877. migrate->npages++;
  1878. migrate->cpages++;
  1879. }
  1880. return 0;
  1881. }
  1882. static int migrate_vma_collect_skip(unsigned long start,
  1883. unsigned long end,
  1884. struct mm_walk *walk)
  1885. {
  1886. struct migrate_vma *migrate = walk->private;
  1887. unsigned long addr;
  1888. for (addr = start & PAGE_MASK; addr < end; addr += PAGE_SIZE) {
  1889. migrate->dst[migrate->npages] = 0;
  1890. migrate->src[migrate->npages++] = 0;
  1891. }
  1892. return 0;
  1893. }
  1894. static int migrate_vma_collect_pmd(pmd_t *pmdp,
  1895. unsigned long start,
  1896. unsigned long end,
  1897. struct mm_walk *walk)
  1898. {
  1899. struct migrate_vma *migrate = walk->private;
  1900. struct vm_area_struct *vma = walk->vma;
  1901. struct mm_struct *mm = vma->vm_mm;
  1902. unsigned long addr = start, unmapped = 0;
  1903. spinlock_t *ptl;
  1904. pte_t *ptep;
  1905. again:
  1906. if (pmd_none(*pmdp))
  1907. return migrate_vma_collect_hole(start, end, walk);
  1908. if (pmd_trans_huge(*pmdp)) {
  1909. struct page *page;
  1910. ptl = pmd_lock(mm, pmdp);
  1911. if (unlikely(!pmd_trans_huge(*pmdp))) {
  1912. spin_unlock(ptl);
  1913. goto again;
  1914. }
  1915. page = pmd_page(*pmdp);
  1916. if (is_huge_zero_page(page)) {
  1917. spin_unlock(ptl);
  1918. split_huge_pmd(vma, pmdp, addr);
  1919. if (pmd_trans_unstable(pmdp))
  1920. return migrate_vma_collect_skip(start, end,
  1921. walk);
  1922. } else {
  1923. int ret;
  1924. get_page(page);
  1925. spin_unlock(ptl);
  1926. if (unlikely(!trylock_page(page)))
  1927. return migrate_vma_collect_skip(start, end,
  1928. walk);
  1929. ret = split_huge_page(page);
  1930. unlock_page(page);
  1931. put_page(page);
  1932. if (ret)
  1933. return migrate_vma_collect_skip(start, end,
  1934. walk);
  1935. if (pmd_none(*pmdp))
  1936. return migrate_vma_collect_hole(start, end,
  1937. walk);
  1938. }
  1939. }
  1940. if (unlikely(pmd_bad(*pmdp)))
  1941. return migrate_vma_collect_skip(start, end, walk);
  1942. ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
  1943. arch_enter_lazy_mmu_mode();
  1944. for (; addr < end; addr += PAGE_SIZE, ptep++) {
  1945. unsigned long mpfn, pfn;
  1946. struct page *page;
  1947. swp_entry_t entry;
  1948. pte_t pte;
  1949. pte = *ptep;
  1950. pfn = pte_pfn(pte);
  1951. if (pte_none(pte)) {
  1952. mpfn = MIGRATE_PFN_MIGRATE;
  1953. migrate->cpages++;
  1954. pfn = 0;
  1955. goto next;
  1956. }
  1957. if (!pte_present(pte)) {
  1958. mpfn = pfn = 0;
  1959. /*
  1960. * Only care about unaddressable device page special
  1961. * page table entry. Other special swap entries are not
  1962. * migratable, and we ignore regular swapped page.
  1963. */
  1964. entry = pte_to_swp_entry(pte);
  1965. if (!is_device_private_entry(entry))
  1966. goto next;
  1967. page = device_private_entry_to_page(entry);
  1968. mpfn = migrate_pfn(page_to_pfn(page))|
  1969. MIGRATE_PFN_DEVICE | MIGRATE_PFN_MIGRATE;
  1970. if (is_write_device_private_entry(entry))
  1971. mpfn |= MIGRATE_PFN_WRITE;
  1972. } else {
  1973. if (is_zero_pfn(pfn)) {
  1974. mpfn = MIGRATE_PFN_MIGRATE;
  1975. migrate->cpages++;
  1976. pfn = 0;
  1977. goto next;
  1978. }
  1979. page = _vm_normal_page(migrate->vma, addr, pte, true);
  1980. mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
  1981. mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0;
  1982. }
  1983. /* FIXME support THP */
  1984. if (!page || !page->mapping || PageTransCompound(page)) {
  1985. mpfn = pfn = 0;
  1986. goto next;
  1987. }
  1988. pfn = page_to_pfn(page);
  1989. /*
  1990. * By getting a reference on the page we pin it and that blocks
  1991. * any kind of migration. Side effect is that it "freezes" the
  1992. * pte.
  1993. *
  1994. * We drop this reference after isolating the page from the lru
  1995. * for non device page (device page are not on the lru and thus
  1996. * can't be dropped from it).
  1997. */
  1998. get_page(page);
  1999. migrate->cpages++;
  2000. /*
  2001. * Optimize for the common case where page is only mapped once
  2002. * in one process. If we can lock the page, then we can safely
  2003. * set up a special migration page table entry now.
  2004. */
  2005. if (trylock_page(page)) {
  2006. pte_t swp_pte;
  2007. mpfn |= MIGRATE_PFN_LOCKED;
  2008. ptep_get_and_clear(mm, addr, ptep);
  2009. /* Setup special migration page table entry */
  2010. entry = make_migration_entry(page, mpfn &
  2011. MIGRATE_PFN_WRITE);
  2012. swp_pte = swp_entry_to_pte(entry);
  2013. if (pte_soft_dirty(pte))
  2014. swp_pte = pte_swp_mksoft_dirty(swp_pte);
  2015. set_pte_at(mm, addr, ptep, swp_pte);
  2016. /*
  2017. * This is like regular unmap: we remove the rmap and
  2018. * drop page refcount. Page won't be freed, as we took
  2019. * a reference just above.
  2020. */
  2021. page_remove_rmap(page, false);
  2022. put_page(page);
  2023. if (pte_present(pte))
  2024. unmapped++;
  2025. }
  2026. next:
  2027. migrate->dst[migrate->npages] = 0;
  2028. migrate->src[migrate->npages++] = mpfn;
  2029. }
  2030. arch_leave_lazy_mmu_mode();
  2031. pte_unmap_unlock(ptep - 1, ptl);
  2032. /* Only flush the TLB if we actually modified any entries */
  2033. if (unmapped)
  2034. flush_tlb_range(walk->vma, start, end);
  2035. return 0;
  2036. }
  2037. /*
  2038. * migrate_vma_collect() - collect pages over a range of virtual addresses
  2039. * @migrate: migrate struct containing all migration information
  2040. *
  2041. * This will walk the CPU page table. For each virtual address backed by a
  2042. * valid page, it updates the src array and takes a reference on the page, in
  2043. * order to pin the page until we lock it and unmap it.
  2044. */
  2045. static void migrate_vma_collect(struct migrate_vma *migrate)
  2046. {
  2047. struct mm_walk mm_walk;
  2048. mm_walk.pmd_entry = migrate_vma_collect_pmd;
  2049. mm_walk.pte_entry = NULL;
  2050. mm_walk.pte_hole = migrate_vma_collect_hole;
  2051. mm_walk.hugetlb_entry = NULL;
  2052. mm_walk.test_walk = NULL;
  2053. mm_walk.vma = migrate->vma;
  2054. mm_walk.mm = migrate->vma->vm_mm;
  2055. mm_walk.private = migrate;
  2056. mmu_notifier_invalidate_range_start(mm_walk.mm,
  2057. migrate->start,
  2058. migrate->end);
  2059. walk_page_range(migrate->start, migrate->end, &mm_walk);
  2060. mmu_notifier_invalidate_range_end(mm_walk.mm,
  2061. migrate->start,
  2062. migrate->end);
  2063. migrate->end = migrate->start + (migrate->npages << PAGE_SHIFT);
  2064. }
  2065. /*
  2066. * migrate_vma_check_page() - check if page is pinned or not
  2067. * @page: struct page to check
  2068. *
  2069. * Pinned pages cannot be migrated. This is the same test as in
  2070. * migrate_page_move_mapping(), except that here we allow migration of a
  2071. * ZONE_DEVICE page.
  2072. */
  2073. static bool migrate_vma_check_page(struct page *page)
  2074. {
  2075. /*
  2076. * One extra ref because caller holds an extra reference, either from
  2077. * isolate_lru_page() for a regular page, or migrate_vma_collect() for
  2078. * a device page.
  2079. */
  2080. int extra = 1;
  2081. /*
  2082. * FIXME support THP (transparent huge page), it is bit more complex to
  2083. * check them than regular pages, because they can be mapped with a pmd
  2084. * or with a pte (split pte mapping).
  2085. */
  2086. if (PageCompound(page))
  2087. return false;
  2088. /* Page from ZONE_DEVICE have one extra reference */
  2089. if (is_zone_device_page(page)) {
  2090. /*
  2091. * Private page can never be pin as they have no valid pte and
  2092. * GUP will fail for those. Yet if there is a pending migration
  2093. * a thread might try to wait on the pte migration entry and
  2094. * will bump the page reference count. Sadly there is no way to
  2095. * differentiate a regular pin from migration wait. Hence to
  2096. * avoid 2 racing thread trying to migrate back to CPU to enter
  2097. * infinite loop (one stoping migration because the other is
  2098. * waiting on pte migration entry). We always return true here.
  2099. *
  2100. * FIXME proper solution is to rework migration_entry_wait() so
  2101. * it does not need to take a reference on page.
  2102. */
  2103. if (is_device_private_page(page))
  2104. return true;
  2105. /*
  2106. * Only allow device public page to be migrated and account for
  2107. * the extra reference count imply by ZONE_DEVICE pages.
  2108. */
  2109. if (!is_device_public_page(page))
  2110. return false;
  2111. extra++;
  2112. }
  2113. /* For file back page */
  2114. if (page_mapping(page))
  2115. extra += 1 + page_has_private(page);
  2116. if ((page_count(page) - extra) > page_mapcount(page))
  2117. return false;
  2118. return true;
  2119. }
  2120. /*
  2121. * migrate_vma_prepare() - lock pages and isolate them from the lru
  2122. * @migrate: migrate struct containing all migration information
  2123. *
  2124. * This locks pages that have been collected by migrate_vma_collect(). Once each
  2125. * page is locked it is isolated from the lru (for non-device pages). Finally,
  2126. * the ref taken by migrate_vma_collect() is dropped, as locked pages cannot be
  2127. * migrated by concurrent kernel threads.
  2128. */
  2129. static void migrate_vma_prepare(struct migrate_vma *migrate)
  2130. {
  2131. const unsigned long npages = migrate->npages;
  2132. const unsigned long start = migrate->start;
  2133. unsigned long addr, i, restore = 0;
  2134. bool allow_drain = true;
  2135. lru_add_drain();
  2136. for (i = 0; (i < npages) && migrate->cpages; i++) {
  2137. struct page *page = migrate_pfn_to_page(migrate->src[i]);
  2138. bool remap = true;
  2139. if (!page)
  2140. continue;
  2141. if (!(migrate->src[i] & MIGRATE_PFN_LOCKED)) {
  2142. /*
  2143. * Because we are migrating several pages there can be
  2144. * a deadlock between 2 concurrent migration where each
  2145. * are waiting on each other page lock.
  2146. *
  2147. * Make migrate_vma() a best effort thing and backoff
  2148. * for any page we can not lock right away.
  2149. */
  2150. if (!trylock_page(page)) {
  2151. migrate->src[i] = 0;
  2152. migrate->cpages--;
  2153. put_page(page);
  2154. continue;
  2155. }
  2156. remap = false;
  2157. migrate->src[i] |= MIGRATE_PFN_LOCKED;
  2158. }
  2159. /* ZONE_DEVICE pages are not on LRU */
  2160. if (!is_zone_device_page(page)) {
  2161. if (!PageLRU(page) && allow_drain) {
  2162. /* Drain CPU's pagevec */
  2163. lru_add_drain_all();
  2164. allow_drain = false;
  2165. }
  2166. if (isolate_lru_page(page)) {
  2167. if (remap) {
  2168. migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
  2169. migrate->cpages--;
  2170. restore++;
  2171. } else {
  2172. migrate->src[i] = 0;
  2173. unlock_page(page);
  2174. migrate->cpages--;
  2175. put_page(page);
  2176. }
  2177. continue;
  2178. }
  2179. /* Drop the reference we took in collect */
  2180. put_page(page);
  2181. }
  2182. if (!migrate_vma_check_page(page)) {
  2183. if (remap) {
  2184. migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
  2185. migrate->cpages--;
  2186. restore++;
  2187. if (!is_zone_device_page(page)) {
  2188. get_page(page);
  2189. putback_lru_page(page);
  2190. }
  2191. } else {
  2192. migrate->src[i] = 0;
  2193. unlock_page(page);
  2194. migrate->cpages--;
  2195. if (!is_zone_device_page(page))
  2196. putback_lru_page(page);
  2197. else
  2198. put_page(page);
  2199. }
  2200. }
  2201. }
  2202. for (i = 0, addr = start; i < npages && restore; i++, addr += PAGE_SIZE) {
  2203. struct page *page = migrate_pfn_to_page(migrate->src[i]);
  2204. if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE))
  2205. continue;
  2206. remove_migration_pte(page, migrate->vma, addr, page);
  2207. migrate->src[i] = 0;
  2208. unlock_page(page);
  2209. put_page(page);
  2210. restore--;
  2211. }
  2212. }
  2213. /*
  2214. * migrate_vma_unmap() - replace page mapping with special migration pte entry
  2215. * @migrate: migrate struct containing all migration information
  2216. *
  2217. * Replace page mapping (CPU page table pte) with a special migration pte entry
  2218. * and check again if it has been pinned. Pinned pages are restored because we
  2219. * cannot migrate them.
  2220. *
  2221. * This is the last step before we call the device driver callback to allocate
  2222. * destination memory and copy contents of original page over to new page.
  2223. */
  2224. static void migrate_vma_unmap(struct migrate_vma *migrate)
  2225. {
  2226. int flags = TTU_MIGRATION | TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS;
  2227. const unsigned long npages = migrate->npages;
  2228. const unsigned long start = migrate->start;
  2229. unsigned long addr, i, restore = 0;
  2230. for (i = 0; i < npages; i++) {
  2231. struct page *page = migrate_pfn_to_page(migrate->src[i]);
  2232. if (!page || !(migrate->src[i] & MIGRATE_PFN_MIGRATE))
  2233. continue;
  2234. if (page_mapped(page)) {
  2235. try_to_unmap(page, flags);
  2236. if (page_mapped(page))
  2237. goto restore;
  2238. }
  2239. if (migrate_vma_check_page(page))
  2240. continue;
  2241. restore:
  2242. migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
  2243. migrate->cpages--;
  2244. restore++;
  2245. }
  2246. for (addr = start, i = 0; i < npages && restore; addr += PAGE_SIZE, i++) {
  2247. struct page *page = migrate_pfn_to_page(migrate->src[i]);
  2248. if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE))
  2249. continue;
  2250. remove_migration_ptes(page, page, false);
  2251. migrate->src[i] = 0;
  2252. unlock_page(page);
  2253. restore--;
  2254. if (is_zone_device_page(page))
  2255. put_page(page);
  2256. else
  2257. putback_lru_page(page);
  2258. }
  2259. }
  2260. static void migrate_vma_insert_page(struct migrate_vma *migrate,
  2261. unsigned long addr,
  2262. struct page *page,
  2263. unsigned long *src,
  2264. unsigned long *dst)
  2265. {
  2266. struct vm_area_struct *vma = migrate->vma;
  2267. struct mm_struct *mm = vma->vm_mm;
  2268. struct mem_cgroup *memcg;
  2269. bool flush = false;
  2270. spinlock_t *ptl;
  2271. pte_t entry;
  2272. pgd_t *pgdp;
  2273. p4d_t *p4dp;
  2274. pud_t *pudp;
  2275. pmd_t *pmdp;
  2276. pte_t *ptep;
  2277. /* Only allow populating anonymous memory */
  2278. if (!vma_is_anonymous(vma))
  2279. goto abort;
  2280. pgdp = pgd_offset(mm, addr);
  2281. p4dp = p4d_alloc(mm, pgdp, addr);
  2282. if (!p4dp)
  2283. goto abort;
  2284. pudp = pud_alloc(mm, p4dp, addr);
  2285. if (!pudp)
  2286. goto abort;
  2287. pmdp = pmd_alloc(mm, pudp, addr);
  2288. if (!pmdp)
  2289. goto abort;
  2290. if (pmd_trans_huge(*pmdp) || pmd_devmap(*pmdp))
  2291. goto abort;
  2292. /*
  2293. * Use pte_alloc() instead of pte_alloc_map(). We can't run
  2294. * pte_offset_map() on pmds where a huge pmd might be created
  2295. * from a different thread.
  2296. *
  2297. * pte_alloc_map() is safe to use under down_write(mmap_sem) or when
  2298. * parallel threads are excluded by other means.
  2299. *
  2300. * Here we only have down_read(mmap_sem).
  2301. */
  2302. if (pte_alloc(mm, pmdp, addr))
  2303. goto abort;
  2304. /* See the comment in pte_alloc_one_map() */
  2305. if (unlikely(pmd_trans_unstable(pmdp)))
  2306. goto abort;
  2307. if (unlikely(anon_vma_prepare(vma)))
  2308. goto abort;
  2309. if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL, &memcg, false))
  2310. goto abort;
  2311. /*
  2312. * The memory barrier inside __SetPageUptodate makes sure that
  2313. * preceding stores to the page contents become visible before
  2314. * the set_pte_at() write.
  2315. */
  2316. __SetPageUptodate(page);
  2317. if (is_zone_device_page(page)) {
  2318. if (is_device_private_page(page)) {
  2319. swp_entry_t swp_entry;
  2320. swp_entry = make_device_private_entry(page, vma->vm_flags & VM_WRITE);
  2321. entry = swp_entry_to_pte(swp_entry);
  2322. } else if (is_device_public_page(page)) {
  2323. entry = pte_mkold(mk_pte(page, READ_ONCE(vma->vm_page_prot)));
  2324. if (vma->vm_flags & VM_WRITE)
  2325. entry = pte_mkwrite(pte_mkdirty(entry));
  2326. entry = pte_mkdevmap(entry);
  2327. }
  2328. } else {
  2329. entry = mk_pte(page, vma->vm_page_prot);
  2330. if (vma->vm_flags & VM_WRITE)
  2331. entry = pte_mkwrite(pte_mkdirty(entry));
  2332. }
  2333. ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
  2334. if (pte_present(*ptep)) {
  2335. unsigned long pfn = pte_pfn(*ptep);
  2336. if (!is_zero_pfn(pfn)) {
  2337. pte_unmap_unlock(ptep, ptl);
  2338. mem_cgroup_cancel_charge(page, memcg, false);
  2339. goto abort;
  2340. }
  2341. flush = true;
  2342. } else if (!pte_none(*ptep)) {
  2343. pte_unmap_unlock(ptep, ptl);
  2344. mem_cgroup_cancel_charge(page, memcg, false);
  2345. goto abort;
  2346. }
  2347. /*
  2348. * Check for usefaultfd but do not deliver the fault. Instead,
  2349. * just back off.
  2350. */
  2351. if (userfaultfd_missing(vma)) {
  2352. pte_unmap_unlock(ptep, ptl);
  2353. mem_cgroup_cancel_charge(page, memcg, false);
  2354. goto abort;
  2355. }
  2356. inc_mm_counter(mm, MM_ANONPAGES);
  2357. page_add_new_anon_rmap(page, vma, addr, false);
  2358. mem_cgroup_commit_charge(page, memcg, false, false);
  2359. if (!is_zone_device_page(page))
  2360. lru_cache_add_active_or_unevictable(page, vma);
  2361. get_page(page);
  2362. if (flush) {
  2363. flush_cache_page(vma, addr, pte_pfn(*ptep));
  2364. ptep_clear_flush_notify(vma, addr, ptep);
  2365. set_pte_at_notify(mm, addr, ptep, entry);
  2366. update_mmu_cache(vma, addr, ptep);
  2367. } else {
  2368. /* No need to invalidate - it was non-present before */
  2369. set_pte_at(mm, addr, ptep, entry);
  2370. update_mmu_cache(vma, addr, ptep);
  2371. }
  2372. pte_unmap_unlock(ptep, ptl);
  2373. *src = MIGRATE_PFN_MIGRATE;
  2374. return;
  2375. abort:
  2376. *src &= ~MIGRATE_PFN_MIGRATE;
  2377. }
  2378. /*
  2379. * migrate_vma_pages() - migrate meta-data from src page to dst page
  2380. * @migrate: migrate struct containing all migration information
  2381. *
  2382. * This migrates struct page meta-data from source struct page to destination
  2383. * struct page. This effectively finishes the migration from source page to the
  2384. * destination page.
  2385. */
  2386. static void migrate_vma_pages(struct migrate_vma *migrate)
  2387. {
  2388. const unsigned long npages = migrate->npages;
  2389. const unsigned long start = migrate->start;
  2390. struct vm_area_struct *vma = migrate->vma;
  2391. struct mm_struct *mm = vma->vm_mm;
  2392. unsigned long addr, i, mmu_start;
  2393. bool notified = false;
  2394. for (i = 0, addr = start; i < npages; addr += PAGE_SIZE, i++) {
  2395. struct page *newpage = migrate_pfn_to_page(migrate->dst[i]);
  2396. struct page *page = migrate_pfn_to_page(migrate->src[i]);
  2397. struct address_space *mapping;
  2398. int r;
  2399. if (!newpage) {
  2400. migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
  2401. continue;
  2402. }
  2403. if (!page) {
  2404. if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE)) {
  2405. continue;
  2406. }
  2407. if (!notified) {
  2408. mmu_start = addr;
  2409. notified = true;
  2410. mmu_notifier_invalidate_range_start(mm,
  2411. mmu_start,
  2412. migrate->end);
  2413. }
  2414. migrate_vma_insert_page(migrate, addr, newpage,
  2415. &migrate->src[i],
  2416. &migrate->dst[i]);
  2417. continue;
  2418. }
  2419. mapping = page_mapping(page);
  2420. if (is_zone_device_page(newpage)) {
  2421. if (is_device_private_page(newpage)) {
  2422. /*
  2423. * For now only support private anonymous when
  2424. * migrating to un-addressable device memory.
  2425. */
  2426. if (mapping) {
  2427. migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
  2428. continue;
  2429. }
  2430. } else if (!is_device_public_page(newpage)) {
  2431. /*
  2432. * Other types of ZONE_DEVICE page are not
  2433. * supported.
  2434. */
  2435. migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
  2436. continue;
  2437. }
  2438. }
  2439. r = migrate_page(mapping, newpage, page, MIGRATE_SYNC_NO_COPY);
  2440. if (r != MIGRATEPAGE_SUCCESS)
  2441. migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
  2442. }
  2443. /*
  2444. * No need to double call mmu_notifier->invalidate_range() callback as
  2445. * the above ptep_clear_flush_notify() inside migrate_vma_insert_page()
  2446. * did already call it.
  2447. */
  2448. if (notified)
  2449. mmu_notifier_invalidate_range_only_end(mm, mmu_start,
  2450. migrate->end);
  2451. }
  2452. /*
  2453. * migrate_vma_finalize() - restore CPU page table entry
  2454. * @migrate: migrate struct containing all migration information
  2455. *
  2456. * This replaces the special migration pte entry with either a mapping to the
  2457. * new page if migration was successful for that page, or to the original page
  2458. * otherwise.
  2459. *
  2460. * This also unlocks the pages and puts them back on the lru, or drops the extra
  2461. * refcount, for device pages.
  2462. */
  2463. static void migrate_vma_finalize(struct migrate_vma *migrate)
  2464. {
  2465. const unsigned long npages = migrate->npages;
  2466. unsigned long i;
  2467. for (i = 0; i < npages; i++) {
  2468. struct page *newpage = migrate_pfn_to_page(migrate->dst[i]);
  2469. struct page *page = migrate_pfn_to_page(migrate->src[i]);
  2470. if (!page) {
  2471. if (newpage) {
  2472. unlock_page(newpage);
  2473. put_page(newpage);
  2474. }
  2475. continue;
  2476. }
  2477. if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE) || !newpage) {
  2478. if (newpage) {
  2479. unlock_page(newpage);
  2480. put_page(newpage);
  2481. }
  2482. newpage = page;
  2483. }
  2484. remove_migration_ptes(page, newpage, false);
  2485. unlock_page(page);
  2486. migrate->cpages--;
  2487. if (is_zone_device_page(page))
  2488. put_page(page);
  2489. else
  2490. putback_lru_page(page);
  2491. if (newpage != page) {
  2492. unlock_page(newpage);
  2493. if (is_zone_device_page(newpage))
  2494. put_page(newpage);
  2495. else
  2496. putback_lru_page(newpage);
  2497. }
  2498. }
  2499. }
  2500. /*
  2501. * migrate_vma() - migrate a range of memory inside vma
  2502. *
  2503. * @ops: migration callback for allocating destination memory and copying
  2504. * @vma: virtual memory area containing the range to be migrated
  2505. * @start: start address of the range to migrate (inclusive)
  2506. * @end: end address of the range to migrate (exclusive)
  2507. * @src: array of hmm_pfn_t containing source pfns
  2508. * @dst: array of hmm_pfn_t containing destination pfns
  2509. * @private: pointer passed back to each of the callback
  2510. * Returns: 0 on success, error code otherwise
  2511. *
  2512. * This function tries to migrate a range of memory virtual address range, using
  2513. * callbacks to allocate and copy memory from source to destination. First it
  2514. * collects all the pages backing each virtual address in the range, saving this
  2515. * inside the src array. Then it locks those pages and unmaps them. Once the pages
  2516. * are locked and unmapped, it checks whether each page is pinned or not. Pages
  2517. * that aren't pinned have the MIGRATE_PFN_MIGRATE flag set (by this function)
  2518. * in the corresponding src array entry. It then restores any pages that are
  2519. * pinned, by remapping and unlocking those pages.
  2520. *
  2521. * At this point it calls the alloc_and_copy() callback. For documentation on
  2522. * what is expected from that callback, see struct migrate_vma_ops comments in
  2523. * include/linux/migrate.h
  2524. *
  2525. * After the alloc_and_copy() callback, this function goes over each entry in
  2526. * the src array that has the MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag
  2527. * set. If the corresponding entry in dst array has MIGRATE_PFN_VALID flag set,
  2528. * then the function tries to migrate struct page information from the source
  2529. * struct page to the destination struct page. If it fails to migrate the struct
  2530. * page information, then it clears the MIGRATE_PFN_MIGRATE flag in the src
  2531. * array.
  2532. *
  2533. * At this point all successfully migrated pages have an entry in the src
  2534. * array with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag set and the dst
  2535. * array entry with MIGRATE_PFN_VALID flag set.
  2536. *
  2537. * It then calls the finalize_and_map() callback. See comments for "struct
  2538. * migrate_vma_ops", in include/linux/migrate.h for details about
  2539. * finalize_and_map() behavior.
  2540. *
  2541. * After the finalize_and_map() callback, for successfully migrated pages, this
  2542. * function updates the CPU page table to point to new pages, otherwise it
  2543. * restores the CPU page table to point to the original source pages.
  2544. *
  2545. * Function returns 0 after the above steps, even if no pages were migrated
  2546. * (The function only returns an error if any of the arguments are invalid.)
  2547. *
  2548. * Both src and dst array must be big enough for (end - start) >> PAGE_SHIFT
  2549. * unsigned long entries.
  2550. */
  2551. int migrate_vma(const struct migrate_vma_ops *ops,
  2552. struct vm_area_struct *vma,
  2553. unsigned long start,
  2554. unsigned long end,
  2555. unsigned long *src,
  2556. unsigned long *dst,
  2557. void *private)
  2558. {
  2559. struct migrate_vma migrate;
  2560. /* Sanity check the arguments */
  2561. start &= PAGE_MASK;
  2562. end &= PAGE_MASK;
  2563. if (!vma || is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL) ||
  2564. vma_is_dax(vma))
  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) */