migrate.c 78 KB

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