migrate.c 77 KB

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