migrate.c 78 KB

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