migrate.c 76 KB

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