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