migrate.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250
  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 <[email protected]>
  11. * Hirokazu Takahashi <[email protected]>
  12. * Dave Hansen <[email protected]>
  13. * Christoph Lameter
  14. */
  15. #include <linux/migrate.h>
  16. #include <linux/export.h>
  17. #include <linux/swap.h>
  18. #include <linux/swapops.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/mm_inline.h>
  22. #include <linux/nsproxy.h>
  23. #include <linux/pagevec.h>
  24. #include <linux/ksm.h>
  25. #include <linux/rmap.h>
  26. #include <linux/topology.h>
  27. #include <linux/cpu.h>
  28. #include <linux/cpuset.h>
  29. #include <linux/writeback.h>
  30. #include <linux/mempolicy.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/security.h>
  33. #include <linux/backing-dev.h>
  34. #include <linux/compaction.h>
  35. #include <linux/syscalls.h>
  36. #include <linux/compat.h>
  37. #include <linux/hugetlb.h>
  38. #include <linux/hugetlb_cgroup.h>
  39. #include <linux/gfp.h>
  40. #include <linux/pfn_t.h>
  41. #include <linux/memremap.h>
  42. #include <linux/userfaultfd_k.h>
  43. #include <linux/balloon_compaction.h>
  44. #include <linux/page_idle.h>
  45. #include <linux/page_owner.h>
  46. #include <linux/sched/mm.h>
  47. #include <linux/ptrace.h>
  48. #include <linux/oom.h>
  49. #include <linux/memory.h>
  50. #include <linux/random.h>
  51. #include <linux/sched/sysctl.h>
  52. #include <linux/memory-tiers.h>
  53. #include <asm/tlbflush.h>
  54. #include <trace/events/migrate.h>
  55. #undef CREATE_TRACE_POINTS
  56. #include <trace/hooks/mm.h>
  57. #include <trace/hooks/vmscan.h>
  58. #include "internal.h"
  59. int isolate_movable_page(struct page *page, isolate_mode_t mode)
  60. {
  61. const struct movable_operations *mops;
  62. /*
  63. * Avoid burning cycles with pages that are yet under __free_pages(),
  64. * or just got freed under us.
  65. *
  66. * In case we 'win' a race for a movable page being freed under us and
  67. * raise its refcount preventing __free_pages() from doing its job
  68. * the put_page() at the end of this block will take care of
  69. * release this page, thus avoiding a nasty leakage.
  70. */
  71. if (unlikely(!get_page_unless_zero(page)))
  72. goto out;
  73. /*
  74. * Check PageMovable before holding a PG_lock because page's owner
  75. * assumes anybody doesn't touch PG_lock of newly allocated page
  76. * so unconditionally grabbing the lock ruins page's owner side.
  77. */
  78. if (unlikely(!__PageMovable(page)))
  79. goto out_putpage;
  80. /*
  81. * As movable pages are not isolated from LRU lists, concurrent
  82. * compaction threads can race against page migration functions
  83. * as well as race against the releasing a page.
  84. *
  85. * In order to avoid having an already isolated movable page
  86. * being (wrongly) re-isolated while it is under migration,
  87. * or to avoid attempting to isolate pages being released,
  88. * lets be sure we have the page lock
  89. * before proceeding with the movable page isolation steps.
  90. */
  91. if (unlikely(!trylock_page(page)))
  92. goto out_putpage;
  93. if (!PageMovable(page) || PageIsolated(page))
  94. goto out_no_isolated;
  95. mops = page_movable_ops(page);
  96. VM_BUG_ON_PAGE(!mops, page);
  97. if (!mops->isolate_page(page, mode))
  98. goto out_no_isolated;
  99. /* Driver shouldn't use PG_isolated bit of page->flags */
  100. WARN_ON_ONCE(PageIsolated(page));
  101. SetPageIsolated(page);
  102. unlock_page(page);
  103. return 0;
  104. out_no_isolated:
  105. unlock_page(page);
  106. out_putpage:
  107. put_page(page);
  108. out:
  109. return -EBUSY;
  110. }
  111. static void putback_movable_page(struct page *page)
  112. {
  113. const struct movable_operations *mops = page_movable_ops(page);
  114. mops->putback_page(page);
  115. ClearPageIsolated(page);
  116. }
  117. /*
  118. * Put previously isolated pages back onto the appropriate lists
  119. * from where they were once taken off for compaction/migration.
  120. *
  121. * This function shall be used whenever the isolated pageset has been
  122. * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range()
  123. * and isolate_hugetlb().
  124. */
  125. void putback_movable_pages(struct list_head *l)
  126. {
  127. struct page *page;
  128. struct page *page2;
  129. list_for_each_entry_safe(page, page2, l, lru) {
  130. if (unlikely(PageHuge(page))) {
  131. putback_active_hugepage(page);
  132. continue;
  133. }
  134. list_del(&page->lru);
  135. /*
  136. * We isolated non-lru movable page so here we can use
  137. * __PageMovable because LRU page's mapping cannot have
  138. * PAGE_MAPPING_MOVABLE.
  139. */
  140. if (unlikely(__PageMovable(page))) {
  141. VM_BUG_ON_PAGE(!PageIsolated(page), page);
  142. lock_page(page);
  143. if (PageMovable(page))
  144. putback_movable_page(page);
  145. else
  146. ClearPageIsolated(page);
  147. unlock_page(page);
  148. put_page(page);
  149. } else {
  150. mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
  151. page_is_file_lru(page), -thp_nr_pages(page));
  152. putback_lru_page(page);
  153. }
  154. }
  155. }
  156. EXPORT_SYMBOL_GPL(putback_movable_pages);
  157. /*
  158. * Restore a potential migration pte to a working pte entry
  159. */
  160. static bool remove_migration_pte(struct folio *folio,
  161. struct vm_area_struct *vma, unsigned long addr, void *old)
  162. {
  163. DEFINE_FOLIO_VMA_WALK(pvmw, old, vma, addr, PVMW_SYNC | PVMW_MIGRATION);
  164. while (page_vma_mapped_walk(&pvmw)) {
  165. rmap_t rmap_flags = RMAP_NONE;
  166. pte_t pte;
  167. swp_entry_t entry;
  168. struct page *new;
  169. unsigned long idx = 0;
  170. /* pgoff is invalid for ksm pages, but they are never large */
  171. if (folio_test_large(folio) && !folio_test_hugetlb(folio))
  172. idx = linear_page_index(vma, pvmw.address) - pvmw.pgoff;
  173. new = folio_page(folio, idx);
  174. #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
  175. /* PMD-mapped THP migration entry */
  176. if (!pvmw.pte) {
  177. VM_BUG_ON_FOLIO(folio_test_hugetlb(folio) ||
  178. !folio_test_pmd_mappable(folio), folio);
  179. remove_migration_pmd(&pvmw, new);
  180. continue;
  181. }
  182. #endif
  183. folio_get(folio);
  184. pte = mk_pte(new, READ_ONCE(vma->vm_page_prot));
  185. if (pte_swp_soft_dirty(*pvmw.pte))
  186. pte = pte_mksoft_dirty(pte);
  187. /*
  188. * Recheck VMA as permissions can change since migration started
  189. */
  190. entry = pte_to_swp_entry(*pvmw.pte);
  191. if (!is_migration_entry_young(entry))
  192. pte = pte_mkold(pte);
  193. if (folio_test_dirty(folio) && is_migration_entry_dirty(entry))
  194. pte = pte_mkdirty(pte);
  195. if (is_writable_migration_entry(entry))
  196. pte = maybe_mkwrite(pte, vma);
  197. else if (pte_swp_uffd_wp(*pvmw.pte))
  198. pte = pte_mkuffd_wp(pte);
  199. else
  200. pte = pte_wrprotect(pte);
  201. if (folio_test_anon(folio) && !is_readable_migration_entry(entry))
  202. rmap_flags |= RMAP_EXCLUSIVE;
  203. if (unlikely(is_device_private_page(new))) {
  204. if (pte_write(pte))
  205. entry = make_writable_device_private_entry(
  206. page_to_pfn(new));
  207. else
  208. entry = make_readable_device_private_entry(
  209. page_to_pfn(new));
  210. pte = swp_entry_to_pte(entry);
  211. if (pte_swp_soft_dirty(*pvmw.pte))
  212. pte = pte_swp_mksoft_dirty(pte);
  213. if (pte_swp_uffd_wp(*pvmw.pte))
  214. pte = pte_swp_mkuffd_wp(pte);
  215. }
  216. #ifdef CONFIG_HUGETLB_PAGE
  217. if (folio_test_hugetlb(folio)) {
  218. unsigned int shift = huge_page_shift(hstate_vma(vma));
  219. pte = pte_mkhuge(pte);
  220. pte = arch_make_huge_pte(pte, shift, vma->vm_flags);
  221. if (folio_test_anon(folio))
  222. hugepage_add_anon_rmap(new, vma, pvmw.address,
  223. rmap_flags);
  224. else
  225. page_dup_file_rmap(new, true);
  226. set_huge_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
  227. } else
  228. #endif
  229. {
  230. if (folio_test_anon(folio))
  231. page_add_anon_rmap(new, vma, pvmw.address,
  232. rmap_flags);
  233. else
  234. page_add_file_rmap(new, vma, false);
  235. set_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
  236. }
  237. if (vma->vm_flags & VM_LOCKED)
  238. mlock_page_drain_local();
  239. trace_remove_migration_pte(pvmw.address, pte_val(pte),
  240. compound_order(new));
  241. /* No need to invalidate - it was non-present before */
  242. update_mmu_cache(vma, pvmw.address, pvmw.pte);
  243. }
  244. return true;
  245. }
  246. /*
  247. * Get rid of all migration entries and replace them by
  248. * references to the indicated page.
  249. */
  250. void remove_migration_ptes(struct folio *src, struct folio *dst, bool locked)
  251. {
  252. struct rmap_walk_control rwc = {
  253. .rmap_one = remove_migration_pte,
  254. .arg = src,
  255. };
  256. if (locked)
  257. rmap_walk_locked(dst, &rwc);
  258. else
  259. rmap_walk(dst, &rwc);
  260. }
  261. /*
  262. * Something used the pte of a page under migration. We need to
  263. * get to the page and wait until migration is finished.
  264. * When we return from this function the fault will be retried.
  265. */
  266. void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
  267. spinlock_t *ptl)
  268. {
  269. pte_t pte;
  270. swp_entry_t entry;
  271. spin_lock(ptl);
  272. pte = *ptep;
  273. if (!is_swap_pte(pte))
  274. goto out;
  275. entry = pte_to_swp_entry(pte);
  276. if (!is_migration_entry(entry))
  277. goto out;
  278. migration_entry_wait_on_locked(entry, ptep, ptl);
  279. return;
  280. out:
  281. pte_unmap_unlock(ptep, ptl);
  282. }
  283. void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
  284. unsigned long address)
  285. {
  286. spinlock_t *ptl = pte_lockptr(mm, pmd);
  287. pte_t *ptep = pte_offset_map(pmd, address);
  288. __migration_entry_wait(mm, ptep, ptl);
  289. }
  290. #ifdef CONFIG_HUGETLB_PAGE
  291. void __migration_entry_wait_huge(pte_t *ptep, spinlock_t *ptl)
  292. {
  293. pte_t pte;
  294. spin_lock(ptl);
  295. pte = huge_ptep_get(ptep);
  296. if (unlikely(!is_hugetlb_entry_migration(pte)))
  297. spin_unlock(ptl);
  298. else
  299. migration_entry_wait_on_locked(pte_to_swp_entry(pte), NULL, ptl);
  300. }
  301. void migration_entry_wait_huge(struct vm_area_struct *vma, pte_t *pte)
  302. {
  303. spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), vma->vm_mm, pte);
  304. __migration_entry_wait_huge(pte, ptl);
  305. }
  306. #endif
  307. #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
  308. void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd)
  309. {
  310. spinlock_t *ptl;
  311. ptl = pmd_lock(mm, pmd);
  312. if (!is_pmd_migration_entry(*pmd))
  313. goto unlock;
  314. migration_entry_wait_on_locked(pmd_to_swp_entry(*pmd), NULL, ptl);
  315. return;
  316. unlock:
  317. spin_unlock(ptl);
  318. }
  319. #endif
  320. static int folio_expected_refs(struct address_space *mapping,
  321. struct folio *folio)
  322. {
  323. int refs = 1;
  324. if (!mapping)
  325. return refs;
  326. refs += folio_nr_pages(folio);
  327. if (folio_test_private(folio))
  328. refs++;
  329. return refs;
  330. }
  331. /*
  332. * Replace the page in the mapping.
  333. *
  334. * The number of remaining references must be:
  335. * 1 for anonymous pages without a mapping
  336. * 2 for pages with a mapping
  337. * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
  338. */
  339. int folio_migrate_mapping(struct address_space *mapping,
  340. struct folio *newfolio, struct folio *folio, int extra_count)
  341. {
  342. XA_STATE(xas, &mapping->i_pages, folio_index(folio));
  343. struct zone *oldzone, *newzone;
  344. int dirty;
  345. int expected_count = folio_expected_refs(mapping, folio) + extra_count;
  346. long nr = folio_nr_pages(folio);
  347. long entries, i;
  348. if (!mapping) {
  349. /* Anonymous page without mapping */
  350. if (folio_ref_count(folio) != expected_count)
  351. return -EAGAIN;
  352. /* No turning back from here */
  353. newfolio->index = folio->index;
  354. newfolio->mapping = folio->mapping;
  355. if (folio_test_swapbacked(folio))
  356. __folio_set_swapbacked(newfolio);
  357. return MIGRATEPAGE_SUCCESS;
  358. }
  359. oldzone = folio_zone(folio);
  360. newzone = folio_zone(newfolio);
  361. xas_lock_irq(&xas);
  362. if (!folio_ref_freeze(folio, expected_count)) {
  363. xas_unlock_irq(&xas);
  364. return -EAGAIN;
  365. }
  366. /*
  367. * Now we know that no one else is looking at the folio:
  368. * no turning back from here.
  369. */
  370. newfolio->index = folio->index;
  371. newfolio->mapping = folio->mapping;
  372. folio_ref_add(newfolio, nr); /* add cache reference */
  373. if (folio_test_swapbacked(folio)) {
  374. __folio_set_swapbacked(newfolio);
  375. if (folio_test_swapcache(folio)) {
  376. int i;
  377. folio_set_swapcache(newfolio);
  378. for (i = 0; i < nr; i++)
  379. set_page_private(folio_page(newfolio, i),
  380. page_private(folio_page(folio, i)));
  381. }
  382. entries = nr;
  383. } else {
  384. VM_BUG_ON_FOLIO(folio_test_swapcache(folio), folio);
  385. entries = 1;
  386. }
  387. /* Move dirty while page refs frozen and newpage not yet exposed */
  388. dirty = folio_test_dirty(folio);
  389. if (dirty) {
  390. folio_clear_dirty(folio);
  391. folio_set_dirty(newfolio);
  392. }
  393. /* Swap cache still stores N entries instead of a high-order entry */
  394. for (i = 0; i < entries; i++) {
  395. xas_store(&xas, newfolio);
  396. xas_next(&xas);
  397. }
  398. /*
  399. * Drop cache reference from old page by unfreezing
  400. * to one less reference.
  401. * We know this isn't the last reference.
  402. */
  403. folio_ref_unfreeze(folio, expected_count - nr);
  404. xas_unlock(&xas);
  405. /* Leave irq disabled to prevent preemption while updating stats */
  406. /*
  407. * If moved to a different zone then also account
  408. * the page for that zone. Other VM counters will be
  409. * taken care of when we establish references to the
  410. * new page and drop references to the old page.
  411. *
  412. * Note that anonymous pages are accounted for
  413. * via NR_FILE_PAGES and NR_ANON_MAPPED if they
  414. * are mapped to swap space.
  415. */
  416. if (newzone != oldzone) {
  417. struct lruvec *old_lruvec, *new_lruvec;
  418. struct mem_cgroup *memcg;
  419. memcg = folio_memcg(folio);
  420. old_lruvec = mem_cgroup_lruvec(memcg, oldzone->zone_pgdat);
  421. new_lruvec = mem_cgroup_lruvec(memcg, newzone->zone_pgdat);
  422. __mod_lruvec_state(old_lruvec, NR_FILE_PAGES, -nr);
  423. __mod_lruvec_state(new_lruvec, NR_FILE_PAGES, nr);
  424. if (folio_test_swapbacked(folio) && !folio_test_swapcache(folio)) {
  425. __mod_lruvec_state(old_lruvec, NR_SHMEM, -nr);
  426. __mod_lruvec_state(new_lruvec, NR_SHMEM, nr);
  427. }
  428. #ifdef CONFIG_SWAP
  429. if (folio_test_swapcache(folio)) {
  430. __mod_lruvec_state(old_lruvec, NR_SWAPCACHE, -nr);
  431. __mod_lruvec_state(new_lruvec, NR_SWAPCACHE, nr);
  432. }
  433. #endif
  434. if (dirty && mapping_can_writeback(mapping)) {
  435. __mod_lruvec_state(old_lruvec, NR_FILE_DIRTY, -nr);
  436. __mod_zone_page_state(oldzone, NR_ZONE_WRITE_PENDING, -nr);
  437. __mod_lruvec_state(new_lruvec, NR_FILE_DIRTY, nr);
  438. __mod_zone_page_state(newzone, NR_ZONE_WRITE_PENDING, nr);
  439. }
  440. }
  441. local_irq_enable();
  442. return MIGRATEPAGE_SUCCESS;
  443. }
  444. EXPORT_SYMBOL(folio_migrate_mapping);
  445. /*
  446. * The expected number of remaining references is the same as that
  447. * of folio_migrate_mapping().
  448. */
  449. int migrate_huge_page_move_mapping(struct address_space *mapping,
  450. struct folio *dst, struct folio *src)
  451. {
  452. XA_STATE(xas, &mapping->i_pages, folio_index(src));
  453. int expected_count;
  454. xas_lock_irq(&xas);
  455. expected_count = 2 + folio_has_private(src);
  456. if (!folio_ref_freeze(src, expected_count)) {
  457. xas_unlock_irq(&xas);
  458. return -EAGAIN;
  459. }
  460. dst->index = src->index;
  461. dst->mapping = src->mapping;
  462. folio_get(dst);
  463. xas_store(&xas, dst);
  464. folio_ref_unfreeze(src, expected_count - 1);
  465. xas_unlock_irq(&xas);
  466. return MIGRATEPAGE_SUCCESS;
  467. }
  468. /*
  469. * Copy the flags and some other ancillary information
  470. */
  471. void folio_migrate_flags(struct folio *newfolio, struct folio *folio)
  472. {
  473. int cpupid;
  474. if (folio_test_error(folio))
  475. folio_set_error(newfolio);
  476. if (folio_test_referenced(folio))
  477. folio_set_referenced(newfolio);
  478. if (folio_test_uptodate(folio))
  479. folio_mark_uptodate(newfolio);
  480. if (folio_test_clear_active(folio)) {
  481. VM_BUG_ON_FOLIO(folio_test_unevictable(folio), folio);
  482. folio_set_active(newfolio);
  483. } else if (folio_test_clear_unevictable(folio))
  484. folio_set_unevictable(newfolio);
  485. if (folio_test_workingset(folio))
  486. folio_set_workingset(newfolio);
  487. if (folio_test_checked(folio))
  488. folio_set_checked(newfolio);
  489. /*
  490. * PG_anon_exclusive (-> PG_mappedtodisk) is always migrated via
  491. * migration entries. We can still have PG_anon_exclusive set on an
  492. * effectively unmapped and unreferenced first sub-pages of an
  493. * anonymous THP: we can simply copy it here via PG_mappedtodisk.
  494. */
  495. if (folio_test_mappedtodisk(folio))
  496. folio_set_mappedtodisk(newfolio);
  497. trace_android_vh_look_around_migrate_folio(folio, newfolio);
  498. /* Move dirty on pages not done by folio_migrate_mapping() */
  499. if (folio_test_dirty(folio))
  500. folio_set_dirty(newfolio);
  501. if (folio_test_young(folio))
  502. folio_set_young(newfolio);
  503. if (folio_test_idle(folio))
  504. folio_set_idle(newfolio);
  505. /*
  506. * Copy NUMA information to the new page, to prevent over-eager
  507. * future migrations of this same page.
  508. */
  509. cpupid = page_cpupid_xchg_last(&folio->page, -1);
  510. /*
  511. * For memory tiering mode, when migrate between slow and fast
  512. * memory node, reset cpupid, because that is used to record
  513. * page access time in slow memory node.
  514. */
  515. if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) {
  516. bool f_toptier = node_is_toptier(page_to_nid(&folio->page));
  517. bool t_toptier = node_is_toptier(page_to_nid(&newfolio->page));
  518. if (f_toptier != t_toptier)
  519. cpupid = -1;
  520. }
  521. page_cpupid_xchg_last(&newfolio->page, cpupid);
  522. folio_migrate_ksm(newfolio, folio);
  523. /*
  524. * Please do not reorder this without considering how mm/ksm.c's
  525. * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache().
  526. */
  527. if (folio_test_swapcache(folio))
  528. folio_clear_swapcache(folio);
  529. folio_clear_private(folio);
  530. /* page->private contains hugetlb specific flags */
  531. if (!folio_test_hugetlb(folio))
  532. folio->private = NULL;
  533. /*
  534. * If any waiters have accumulated on the new page then
  535. * wake them up.
  536. */
  537. if (folio_test_writeback(newfolio))
  538. folio_end_writeback(newfolio);
  539. /*
  540. * PG_readahead shares the same bit with PG_reclaim. The above
  541. * end_page_writeback() may clear PG_readahead mistakenly, so set the
  542. * bit after that.
  543. */
  544. if (folio_test_readahead(folio))
  545. folio_set_readahead(newfolio);
  546. folio_copy_owner(newfolio, folio);
  547. if (!folio_test_hugetlb(folio))
  548. mem_cgroup_migrate(folio, newfolio);
  549. }
  550. EXPORT_SYMBOL(folio_migrate_flags);
  551. void folio_migrate_copy(struct folio *newfolio, struct folio *folio)
  552. {
  553. folio_copy(newfolio, folio);
  554. folio_migrate_flags(newfolio, folio);
  555. }
  556. EXPORT_SYMBOL(folio_migrate_copy);
  557. /************************************************************
  558. * Migration functions
  559. ***********************************************************/
  560. int migrate_folio_extra(struct address_space *mapping, struct folio *dst,
  561. struct folio *src, enum migrate_mode mode, int extra_count)
  562. {
  563. int rc;
  564. BUG_ON(folio_test_writeback(src)); /* Writeback must be complete */
  565. rc = folio_migrate_mapping(mapping, dst, src, extra_count);
  566. if (rc != MIGRATEPAGE_SUCCESS)
  567. return rc;
  568. if (mode != MIGRATE_SYNC_NO_COPY)
  569. folio_migrate_copy(dst, src);
  570. else
  571. folio_migrate_flags(dst, src);
  572. return MIGRATEPAGE_SUCCESS;
  573. }
  574. /**
  575. * migrate_folio() - Simple folio migration.
  576. * @mapping: The address_space containing the folio.
  577. * @dst: The folio to migrate the data to.
  578. * @src: The folio containing the current data.
  579. * @mode: How to migrate the page.
  580. *
  581. * Common logic to directly migrate a single LRU folio suitable for
  582. * folios that do not use PagePrivate/PagePrivate2.
  583. *
  584. * Folios are locked upon entry and exit.
  585. */
  586. int migrate_folio(struct address_space *mapping, struct folio *dst,
  587. struct folio *src, enum migrate_mode mode)
  588. {
  589. return migrate_folio_extra(mapping, dst, src, mode, 0);
  590. }
  591. EXPORT_SYMBOL(migrate_folio);
  592. #ifdef CONFIG_BLOCK
  593. /* Returns true if all buffers are successfully locked */
  594. static bool buffer_migrate_lock_buffers(struct buffer_head *head,
  595. enum migrate_mode mode)
  596. {
  597. struct buffer_head *bh = head;
  598. /* Simple case, sync compaction */
  599. if (mode != MIGRATE_ASYNC) {
  600. do {
  601. lock_buffer(bh);
  602. bh = bh->b_this_page;
  603. } while (bh != head);
  604. return true;
  605. }
  606. /* async case, we cannot block on lock_buffer so use trylock_buffer */
  607. do {
  608. if (!trylock_buffer(bh)) {
  609. /*
  610. * We failed to lock the buffer and cannot stall in
  611. * async migration. Release the taken locks
  612. */
  613. struct buffer_head *failed_bh = bh;
  614. bh = head;
  615. while (bh != failed_bh) {
  616. unlock_buffer(bh);
  617. bh = bh->b_this_page;
  618. }
  619. return false;
  620. }
  621. bh = bh->b_this_page;
  622. } while (bh != head);
  623. return true;
  624. }
  625. static int __buffer_migrate_folio(struct address_space *mapping,
  626. struct folio *dst, struct folio *src, enum migrate_mode mode,
  627. bool check_refs)
  628. {
  629. struct buffer_head *bh, *head;
  630. int rc;
  631. int expected_count;
  632. head = folio_buffers(src);
  633. if (!head)
  634. return migrate_folio(mapping, dst, src, mode);
  635. /* Check whether page does not have extra refs before we do more work */
  636. expected_count = folio_expected_refs(mapping, src);
  637. if (folio_ref_count(src) != expected_count)
  638. return -EAGAIN;
  639. if (!buffer_migrate_lock_buffers(head, mode))
  640. return -EAGAIN;
  641. if (check_refs) {
  642. bool busy;
  643. bool invalidated = false;
  644. recheck_buffers:
  645. busy = false;
  646. spin_lock(&mapping->private_lock);
  647. bh = head;
  648. do {
  649. if (atomic_read(&bh->b_count)) {
  650. busy = true;
  651. break;
  652. }
  653. bh = bh->b_this_page;
  654. } while (bh != head);
  655. if (busy) {
  656. if (invalidated) {
  657. rc = -EAGAIN;
  658. goto unlock_buffers;
  659. }
  660. spin_unlock(&mapping->private_lock);
  661. invalidate_bh_lrus();
  662. invalidated = true;
  663. goto recheck_buffers;
  664. }
  665. }
  666. rc = folio_migrate_mapping(mapping, dst, src, 0);
  667. if (rc != MIGRATEPAGE_SUCCESS)
  668. goto unlock_buffers;
  669. folio_attach_private(dst, folio_detach_private(src));
  670. bh = head;
  671. do {
  672. set_bh_page(bh, &dst->page, bh_offset(bh));
  673. bh = bh->b_this_page;
  674. } while (bh != head);
  675. if (mode != MIGRATE_SYNC_NO_COPY)
  676. folio_migrate_copy(dst, src);
  677. else
  678. folio_migrate_flags(dst, src);
  679. rc = MIGRATEPAGE_SUCCESS;
  680. unlock_buffers:
  681. if (check_refs)
  682. spin_unlock(&mapping->private_lock);
  683. bh = head;
  684. do {
  685. unlock_buffer(bh);
  686. bh = bh->b_this_page;
  687. } while (bh != head);
  688. return rc;
  689. }
  690. /**
  691. * buffer_migrate_folio() - Migration function for folios with buffers.
  692. * @mapping: The address space containing @src.
  693. * @dst: The folio to migrate to.
  694. * @src: The folio to migrate from.
  695. * @mode: How to migrate the folio.
  696. *
  697. * This function can only be used if the underlying filesystem guarantees
  698. * that no other references to @src exist. For example attached buffer
  699. * heads are accessed only under the folio lock. If your filesystem cannot
  700. * provide this guarantee, buffer_migrate_folio_norefs() may be more
  701. * appropriate.
  702. *
  703. * Return: 0 on success or a negative errno on failure.
  704. */
  705. int buffer_migrate_folio(struct address_space *mapping,
  706. struct folio *dst, struct folio *src, enum migrate_mode mode)
  707. {
  708. return __buffer_migrate_folio(mapping, dst, src, mode, false);
  709. }
  710. EXPORT_SYMBOL(buffer_migrate_folio);
  711. /**
  712. * buffer_migrate_folio_norefs() - Migration function for folios with buffers.
  713. * @mapping: The address space containing @src.
  714. * @dst: The folio to migrate to.
  715. * @src: The folio to migrate from.
  716. * @mode: How to migrate the folio.
  717. *
  718. * Like buffer_migrate_folio() except that this variant is more careful
  719. * and checks that there are also no buffer head references. This function
  720. * is the right one for mappings where buffer heads are directly looked
  721. * up and referenced (such as block device mappings).
  722. *
  723. * Return: 0 on success or a negative errno on failure.
  724. */
  725. int buffer_migrate_folio_norefs(struct address_space *mapping,
  726. struct folio *dst, struct folio *src, enum migrate_mode mode)
  727. {
  728. return __buffer_migrate_folio(mapping, dst, src, mode, true);
  729. }
  730. #endif
  731. int filemap_migrate_folio(struct address_space *mapping,
  732. struct folio *dst, struct folio *src, enum migrate_mode mode)
  733. {
  734. int ret;
  735. ret = folio_migrate_mapping(mapping, dst, src, 0);
  736. if (ret != MIGRATEPAGE_SUCCESS)
  737. return ret;
  738. if (folio_get_private(src))
  739. folio_attach_private(dst, folio_detach_private(src));
  740. if (mode != MIGRATE_SYNC_NO_COPY)
  741. folio_migrate_copy(dst, src);
  742. else
  743. folio_migrate_flags(dst, src);
  744. return MIGRATEPAGE_SUCCESS;
  745. }
  746. EXPORT_SYMBOL_GPL(filemap_migrate_folio);
  747. /*
  748. * Writeback a folio to clean the dirty state
  749. */
  750. static int writeout(struct address_space *mapping, struct folio *folio)
  751. {
  752. struct writeback_control wbc = {
  753. .sync_mode = WB_SYNC_NONE,
  754. .nr_to_write = 1,
  755. .range_start = 0,
  756. .range_end = LLONG_MAX,
  757. .for_reclaim = 1
  758. };
  759. int rc;
  760. if (!mapping->a_ops->writepage)
  761. /* No write method for the address space */
  762. return -EINVAL;
  763. if (!folio_clear_dirty_for_io(folio))
  764. /* Someone else already triggered a write */
  765. return -EAGAIN;
  766. /*
  767. * A dirty folio may imply that the underlying filesystem has
  768. * the folio on some queue. So the folio must be clean for
  769. * migration. Writeout may mean we lose the lock and the
  770. * folio state is no longer what we checked for earlier.
  771. * At this point we know that the migration attempt cannot
  772. * be successful.
  773. */
  774. remove_migration_ptes(folio, folio, false);
  775. rc = mapping->a_ops->writepage(&folio->page, &wbc);
  776. if (rc != AOP_WRITEPAGE_ACTIVATE)
  777. /* unlocked. Relock */
  778. folio_lock(folio);
  779. return (rc < 0) ? -EIO : -EAGAIN;
  780. }
  781. /*
  782. * Default handling if a filesystem does not provide a migration function.
  783. */
  784. static int fallback_migrate_folio(struct address_space *mapping,
  785. struct folio *dst, struct folio *src, enum migrate_mode mode)
  786. {
  787. if (folio_test_dirty(src)) {
  788. /* Only writeback folios in full synchronous migration */
  789. switch (mode) {
  790. case MIGRATE_SYNC:
  791. case MIGRATE_SYNC_NO_COPY:
  792. break;
  793. default:
  794. return -EBUSY;
  795. }
  796. return writeout(mapping, src);
  797. }
  798. /*
  799. * Buffers may be managed in a filesystem specific way.
  800. * We must have no buffers or drop them.
  801. */
  802. if (folio_test_private(src) &&
  803. !filemap_release_folio(src, GFP_KERNEL))
  804. return mode == MIGRATE_SYNC ? -EAGAIN : -EBUSY;
  805. return migrate_folio(mapping, dst, src, mode);
  806. }
  807. /*
  808. * Move a page to a newly allocated page
  809. * The page is locked and all ptes have been successfully removed.
  810. *
  811. * The new page will have replaced the old page if this function
  812. * is successful.
  813. *
  814. * Return value:
  815. * < 0 - error code
  816. * MIGRATEPAGE_SUCCESS - success
  817. */
  818. static int move_to_new_folio(struct folio *dst, struct folio *src,
  819. enum migrate_mode mode)
  820. {
  821. int rc = -EAGAIN;
  822. bool is_lru = !__PageMovable(&src->page);
  823. VM_BUG_ON_FOLIO(!folio_test_locked(src), src);
  824. VM_BUG_ON_FOLIO(!folio_test_locked(dst), dst);
  825. if (likely(is_lru)) {
  826. struct address_space *mapping = folio_mapping(src);
  827. if (!mapping)
  828. rc = migrate_folio(mapping, dst, src, mode);
  829. else if (mapping->a_ops->migrate_folio)
  830. /*
  831. * Most folios have a mapping and most filesystems
  832. * provide a migrate_folio callback. Anonymous folios
  833. * are part of swap space which also has its own
  834. * migrate_folio callback. This is the most common path
  835. * for page migration.
  836. */
  837. rc = mapping->a_ops->migrate_folio(mapping, dst, src,
  838. mode);
  839. else
  840. rc = fallback_migrate_folio(mapping, dst, src, mode);
  841. } else {
  842. const struct movable_operations *mops;
  843. /*
  844. * In case of non-lru page, it could be released after
  845. * isolation step. In that case, we shouldn't try migration.
  846. */
  847. VM_BUG_ON_FOLIO(!folio_test_isolated(src), src);
  848. if (!folio_test_movable(src)) {
  849. rc = MIGRATEPAGE_SUCCESS;
  850. folio_clear_isolated(src);
  851. goto out;
  852. }
  853. mops = page_movable_ops(&src->page);
  854. rc = mops->migrate_page(&dst->page, &src->page, mode);
  855. WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS &&
  856. !folio_test_isolated(src));
  857. }
  858. /*
  859. * When successful, old pagecache src->mapping must be cleared before
  860. * src is freed; but stats require that PageAnon be left as PageAnon.
  861. */
  862. if (rc == MIGRATEPAGE_SUCCESS) {
  863. if (__PageMovable(&src->page)) {
  864. VM_BUG_ON_FOLIO(!folio_test_isolated(src), src);
  865. /*
  866. * We clear PG_movable under page_lock so any compactor
  867. * cannot try to migrate this page.
  868. */
  869. folio_clear_isolated(src);
  870. }
  871. /*
  872. * Anonymous and movable src->mapping will be cleared by
  873. * free_pages_prepare so don't reset it here for keeping
  874. * the type to work PageAnon, for example.
  875. */
  876. if (!folio_mapping_flags(src))
  877. src->mapping = NULL;
  878. if (likely(!folio_is_zone_device(dst)))
  879. flush_dcache_folio(dst);
  880. }
  881. out:
  882. return rc;
  883. }
  884. static int __unmap_and_move(struct folio *src, struct folio *dst,
  885. int force, enum migrate_mode mode)
  886. {
  887. int rc = -EAGAIN;
  888. bool page_was_mapped = false;
  889. struct anon_vma *anon_vma = NULL;
  890. bool is_lru = !__PageMovable(&src->page);
  891. if (!folio_trylock(src)) {
  892. if (!force || mode == MIGRATE_ASYNC)
  893. goto out;
  894. /*
  895. * It's not safe for direct compaction to call lock_page.
  896. * For example, during page readahead pages are added locked
  897. * to the LRU. Later, when the IO completes the pages are
  898. * marked uptodate and unlocked. However, the queueing
  899. * could be merging multiple pages for one bio (e.g.
  900. * mpage_readahead). If an allocation happens for the
  901. * second or third page, the process can end up locking
  902. * the same page twice and deadlocking. Rather than
  903. * trying to be clever about what pages can be locked,
  904. * avoid the use of lock_page for direct compaction
  905. * altogether.
  906. */
  907. if (current->flags & PF_MEMALLOC)
  908. goto out;
  909. folio_lock(src);
  910. }
  911. if (folio_test_writeback(src)) {
  912. /*
  913. * Only in the case of a full synchronous migration is it
  914. * necessary to wait for PageWriteback. In the async case,
  915. * the retry loop is too short and in the sync-light case,
  916. * the overhead of stalling is too much
  917. */
  918. switch (mode) {
  919. case MIGRATE_SYNC:
  920. case MIGRATE_SYNC_NO_COPY:
  921. break;
  922. default:
  923. rc = -EBUSY;
  924. goto out_unlock;
  925. }
  926. if (!force)
  927. goto out_unlock;
  928. folio_wait_writeback(src);
  929. }
  930. /*
  931. * By try_to_migrate(), src->mapcount goes down to 0 here. In this case,
  932. * we cannot notice that anon_vma is freed while we migrate a page.
  933. * This get_anon_vma() delays freeing anon_vma pointer until the end
  934. * of migration. File cache pages are no problem because of page_lock()
  935. * File Caches may use write_page() or lock_page() in migration, then,
  936. * just care Anon page here.
  937. *
  938. * Only folio_get_anon_vma() understands the subtleties of
  939. * getting a hold on an anon_vma from outside one of its mms.
  940. * But if we cannot get anon_vma, then we won't need it anyway,
  941. * because that implies that the anon page is no longer mapped
  942. * (and cannot be remapped so long as we hold the page lock).
  943. */
  944. if (folio_test_anon(src) && !folio_test_ksm(src))
  945. anon_vma = folio_get_anon_vma(src);
  946. /*
  947. * Block others from accessing the new page when we get around to
  948. * establishing additional references. We are usually the only one
  949. * holding a reference to dst at this point. We used to have a BUG
  950. * here if folio_trylock(dst) fails, but would like to allow for
  951. * cases where there might be a race with the previous use of dst.
  952. * This is much like races on refcount of oldpage: just don't BUG().
  953. */
  954. if (unlikely(!folio_trylock(dst)))
  955. goto out_unlock;
  956. if (unlikely(!is_lru)) {
  957. rc = move_to_new_folio(dst, src, mode);
  958. goto out_unlock_both;
  959. }
  960. /*
  961. * Corner case handling:
  962. * 1. When a new swap-cache page is read into, it is added to the LRU
  963. * and treated as swapcache but it has no rmap yet.
  964. * Calling try_to_unmap() against a src->mapping==NULL page will
  965. * trigger a BUG. So handle it here.
  966. * 2. An orphaned page (see truncate_cleanup_page) might have
  967. * fs-private metadata. The page can be picked up due to memory
  968. * offlining. Everywhere else except page reclaim, the page is
  969. * invisible to the vm, so the page can not be migrated. So try to
  970. * free the metadata, so the page can be freed.
  971. */
  972. if (!src->mapping) {
  973. if (folio_test_private(src)) {
  974. try_to_free_buffers(src);
  975. goto out_unlock_both;
  976. }
  977. } else if (folio_mapped(src)) {
  978. /* Establish migration ptes */
  979. VM_BUG_ON_FOLIO(folio_test_anon(src) &&
  980. !folio_test_ksm(src) && !anon_vma, src);
  981. try_to_migrate(src, 0);
  982. page_was_mapped = true;
  983. }
  984. if (!folio_mapped(src))
  985. rc = move_to_new_folio(dst, src, mode);
  986. /*
  987. * When successful, push dst to LRU immediately: so that if it
  988. * turns out to be an mlocked page, remove_migration_ptes() will
  989. * automatically build up the correct dst->mlock_count for it.
  990. *
  991. * We would like to do something similar for the old page, when
  992. * unsuccessful, and other cases when a page has been temporarily
  993. * isolated from the unevictable LRU: but this case is the easiest.
  994. */
  995. if (rc == MIGRATEPAGE_SUCCESS) {
  996. folio_add_lru(dst);
  997. if (page_was_mapped)
  998. lru_add_drain();
  999. }
  1000. if (page_was_mapped)
  1001. remove_migration_ptes(src,
  1002. rc == MIGRATEPAGE_SUCCESS ? dst : src, false);
  1003. out_unlock_both:
  1004. folio_unlock(dst);
  1005. out_unlock:
  1006. /* Drop an anon_vma reference if we took one */
  1007. if (anon_vma)
  1008. put_anon_vma(anon_vma);
  1009. folio_unlock(src);
  1010. out:
  1011. /*
  1012. * If migration is successful, decrease refcount of dst,
  1013. * which will not free the page because new page owner increased
  1014. * refcounter.
  1015. */
  1016. if (rc == MIGRATEPAGE_SUCCESS)
  1017. folio_put(dst);
  1018. return rc;
  1019. }
  1020. /*
  1021. * Obtain the lock on page, remove all ptes and migrate the page
  1022. * to the newly allocated page in newpage.
  1023. */
  1024. static int unmap_and_move(new_page_t get_new_page,
  1025. free_page_t put_new_page,
  1026. unsigned long private, struct page *page,
  1027. int force, enum migrate_mode mode,
  1028. enum migrate_reason reason,
  1029. struct list_head *ret)
  1030. {
  1031. struct folio *dst, *src = page_folio(page);
  1032. int rc = MIGRATEPAGE_SUCCESS;
  1033. struct page *newpage = NULL;
  1034. if (!thp_migration_supported() && PageTransHuge(page))
  1035. return -ENOSYS;
  1036. if (page_count(page) == 1) {
  1037. /* Page was freed from under us. So we are done. */
  1038. ClearPageActive(page);
  1039. ClearPageUnevictable(page);
  1040. /* free_pages_prepare() will clear PG_isolated. */
  1041. goto out;
  1042. }
  1043. newpage = get_new_page(page, private);
  1044. if (!newpage)
  1045. return -ENOMEM;
  1046. dst = page_folio(newpage);
  1047. newpage->private = 0;
  1048. rc = __unmap_and_move(src, dst, force, mode);
  1049. if (rc == MIGRATEPAGE_SUCCESS)
  1050. set_page_owner_migrate_reason(newpage, reason);
  1051. out:
  1052. if (rc != -EAGAIN) {
  1053. /*
  1054. * A page that has been migrated has all references
  1055. * removed and will be freed. A page that has not been
  1056. * migrated will have kept its references and be restored.
  1057. */
  1058. list_del(&page->lru);
  1059. }
  1060. /*
  1061. * If migration is successful, releases reference grabbed during
  1062. * isolation. Otherwise, restore the page to right list unless
  1063. * we want to retry.
  1064. */
  1065. if (rc == MIGRATEPAGE_SUCCESS) {
  1066. /*
  1067. * Compaction can migrate also non-LRU pages which are
  1068. * not accounted to NR_ISOLATED_*. They can be recognized
  1069. * as __PageMovable
  1070. */
  1071. if (likely(!__PageMovable(page)))
  1072. mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
  1073. page_is_file_lru(page), -thp_nr_pages(page));
  1074. if (reason != MR_MEMORY_FAILURE)
  1075. /*
  1076. * We release the page in page_handle_poison.
  1077. */
  1078. put_page(page);
  1079. } else {
  1080. if (rc != -EAGAIN)
  1081. list_add_tail(&page->lru, ret);
  1082. if (put_new_page)
  1083. put_new_page(newpage, private);
  1084. else
  1085. put_page(newpage);
  1086. }
  1087. return rc;
  1088. }
  1089. /*
  1090. * Counterpart of unmap_and_move_page() for hugepage migration.
  1091. *
  1092. * This function doesn't wait the completion of hugepage I/O
  1093. * because there is no race between I/O and migration for hugepage.
  1094. * Note that currently hugepage I/O occurs only in direct I/O
  1095. * where no lock is held and PG_writeback is irrelevant,
  1096. * and writeback status of all subpages are counted in the reference
  1097. * count of the head page (i.e. if all subpages of a 2MB hugepage are
  1098. * under direct I/O, the reference of the head page is 512 and a bit more.)
  1099. * This means that when we try to migrate hugepage whose subpages are
  1100. * doing direct I/O, some references remain after try_to_unmap() and
  1101. * hugepage migration fails without data corruption.
  1102. *
  1103. * There is also no race when direct I/O is issued on the page under migration,
  1104. * because then pte is replaced with migration swap entry and direct I/O code
  1105. * will wait in the page fault for migration to complete.
  1106. */
  1107. static int unmap_and_move_huge_page(new_page_t get_new_page,
  1108. free_page_t put_new_page, unsigned long private,
  1109. struct page *hpage, int force,
  1110. enum migrate_mode mode, int reason,
  1111. struct list_head *ret)
  1112. {
  1113. struct folio *dst, *src = page_folio(hpage);
  1114. int rc = -EAGAIN;
  1115. int page_was_mapped = 0;
  1116. struct page *new_hpage;
  1117. struct anon_vma *anon_vma = NULL;
  1118. struct address_space *mapping = NULL;
  1119. /*
  1120. * Migratability of hugepages depends on architectures and their size.
  1121. * This check is necessary because some callers of hugepage migration
  1122. * like soft offline and memory hotremove don't walk through page
  1123. * tables or check whether the hugepage is pmd-based or not before
  1124. * kicking migration.
  1125. */
  1126. if (!hugepage_migration_supported(page_hstate(hpage)))
  1127. return -ENOSYS;
  1128. if (folio_ref_count(src) == 1) {
  1129. /* page was freed from under us. So we are done. */
  1130. putback_active_hugepage(hpage);
  1131. return MIGRATEPAGE_SUCCESS;
  1132. }
  1133. new_hpage = get_new_page(hpage, private);
  1134. if (!new_hpage)
  1135. return -ENOMEM;
  1136. dst = page_folio(new_hpage);
  1137. if (!folio_trylock(src)) {
  1138. if (!force)
  1139. goto out;
  1140. switch (mode) {
  1141. case MIGRATE_SYNC:
  1142. case MIGRATE_SYNC_NO_COPY:
  1143. break;
  1144. default:
  1145. goto out;
  1146. }
  1147. folio_lock(src);
  1148. }
  1149. /*
  1150. * Check for pages which are in the process of being freed. Without
  1151. * folio_mapping() set, hugetlbfs specific move page routine will not
  1152. * be called and we could leak usage counts for subpools.
  1153. */
  1154. if (hugetlb_page_subpool(hpage) && !folio_mapping(src)) {
  1155. rc = -EBUSY;
  1156. goto out_unlock;
  1157. }
  1158. if (folio_test_anon(src))
  1159. anon_vma = folio_get_anon_vma(src);
  1160. if (unlikely(!folio_trylock(dst)))
  1161. goto put_anon;
  1162. if (folio_mapped(src)) {
  1163. enum ttu_flags ttu = 0;
  1164. if (!folio_test_anon(src)) {
  1165. /*
  1166. * In shared mappings, try_to_unmap could potentially
  1167. * call huge_pmd_unshare. Because of this, take
  1168. * semaphore in write mode here and set TTU_RMAP_LOCKED
  1169. * to let lower levels know we have taken the lock.
  1170. */
  1171. mapping = hugetlb_page_mapping_lock_write(hpage);
  1172. if (unlikely(!mapping))
  1173. goto unlock_put_anon;
  1174. ttu = TTU_RMAP_LOCKED;
  1175. }
  1176. try_to_migrate(src, ttu);
  1177. page_was_mapped = 1;
  1178. if (ttu & TTU_RMAP_LOCKED)
  1179. i_mmap_unlock_write(mapping);
  1180. }
  1181. if (!folio_mapped(src))
  1182. rc = move_to_new_folio(dst, src, mode);
  1183. if (page_was_mapped)
  1184. remove_migration_ptes(src,
  1185. rc == MIGRATEPAGE_SUCCESS ? dst : src, false);
  1186. unlock_put_anon:
  1187. folio_unlock(dst);
  1188. put_anon:
  1189. if (anon_vma)
  1190. put_anon_vma(anon_vma);
  1191. if (rc == MIGRATEPAGE_SUCCESS) {
  1192. move_hugetlb_state(hpage, new_hpage, reason);
  1193. put_new_page = NULL;
  1194. }
  1195. out_unlock:
  1196. folio_unlock(src);
  1197. out:
  1198. if (rc == MIGRATEPAGE_SUCCESS)
  1199. putback_active_hugepage(hpage);
  1200. else if (rc != -EAGAIN)
  1201. list_move_tail(&src->lru, ret);
  1202. /*
  1203. * If migration was not successful and there's a freeing callback, use
  1204. * it. Otherwise, put_page() will drop the reference grabbed during
  1205. * isolation.
  1206. */
  1207. if (put_new_page)
  1208. put_new_page(new_hpage, private);
  1209. else
  1210. putback_active_hugepage(new_hpage);
  1211. return rc;
  1212. }
  1213. static inline int try_split_thp(struct page *page, struct list_head *split_pages)
  1214. {
  1215. int rc;
  1216. lock_page(page);
  1217. rc = split_huge_page_to_list(page, split_pages);
  1218. unlock_page(page);
  1219. if (!rc)
  1220. list_move_tail(&page->lru, split_pages);
  1221. return rc;
  1222. }
  1223. /*
  1224. * migrate_pages - migrate the pages specified in a list, to the free pages
  1225. * supplied as the target for the page migration
  1226. *
  1227. * @from: The list of pages to be migrated.
  1228. * @get_new_page: The function used to allocate free pages to be used
  1229. * as the target of the page migration.
  1230. * @put_new_page: The function used to free target pages if migration
  1231. * fails, or NULL if no special handling is necessary.
  1232. * @private: Private data to be passed on to get_new_page()
  1233. * @mode: The migration mode that specifies the constraints for
  1234. * page migration, if any.
  1235. * @reason: The reason for page migration.
  1236. * @ret_succeeded: Set to the number of normal pages migrated successfully if
  1237. * the caller passes a non-NULL pointer.
  1238. *
  1239. * The function returns after 10 attempts or if no pages are movable any more
  1240. * because the list has become empty or no retryable pages exist any more.
  1241. * It is caller's responsibility to call putback_movable_pages() to return pages
  1242. * to the LRU or free list only if ret != 0.
  1243. *
  1244. * Returns the number of {normal page, THP, hugetlb} that were not migrated, or
  1245. * an error code. The number of THP splits will be considered as the number of
  1246. * non-migrated THP, no matter how many subpages of the THP are migrated successfully.
  1247. */
  1248. int migrate_pages(struct list_head *from, new_page_t get_new_page,
  1249. free_page_t put_new_page, unsigned long private,
  1250. enum migrate_mode mode, int reason, unsigned int *ret_succeeded)
  1251. {
  1252. int retry = 1;
  1253. int thp_retry = 1;
  1254. int nr_failed = 0;
  1255. int nr_failed_pages = 0;
  1256. int nr_retry_pages = 0;
  1257. int nr_succeeded = 0;
  1258. int nr_thp_succeeded = 0;
  1259. int nr_thp_failed = 0;
  1260. int nr_thp_split = 0;
  1261. int pass = 0;
  1262. bool is_thp = false;
  1263. struct page *page;
  1264. struct page *page2;
  1265. int rc, nr_subpages;
  1266. LIST_HEAD(ret_pages);
  1267. LIST_HEAD(thp_split_pages);
  1268. bool nosplit = (reason == MR_NUMA_MISPLACED);
  1269. bool no_subpage_counting = false;
  1270. trace_mm_migrate_pages_start(mode, reason);
  1271. thp_subpage_migration:
  1272. for (pass = 0; pass < 10 && (retry || thp_retry); pass++) {
  1273. retry = 0;
  1274. thp_retry = 0;
  1275. nr_retry_pages = 0;
  1276. list_for_each_entry_safe(page, page2, from, lru) {
  1277. /*
  1278. * THP statistics is based on the source huge page.
  1279. * Capture required information that might get lost
  1280. * during migration.
  1281. */
  1282. is_thp = PageTransHuge(page) && !PageHuge(page);
  1283. nr_subpages = compound_nr(page);
  1284. cond_resched();
  1285. if (PageHuge(page))
  1286. rc = unmap_and_move_huge_page(get_new_page,
  1287. put_new_page, private, page,
  1288. pass > 2, mode, reason,
  1289. &ret_pages);
  1290. else
  1291. rc = unmap_and_move(get_new_page, put_new_page,
  1292. private, page, pass > 2, mode,
  1293. reason, &ret_pages);
  1294. /*
  1295. * The rules are:
  1296. * Success: non hugetlb page will be freed, hugetlb
  1297. * page will be put back
  1298. * -EAGAIN: stay on the from list
  1299. * -ENOMEM: stay on the from list
  1300. * -ENOSYS: stay on the from list
  1301. * Other errno: put on ret_pages list then splice to
  1302. * from list
  1303. */
  1304. switch(rc) {
  1305. /*
  1306. * THP migration might be unsupported or the
  1307. * allocation could've failed so we should
  1308. * retry on the same page with the THP split
  1309. * to base pages.
  1310. *
  1311. * Sub-pages are put in thp_split_pages, and
  1312. * we will migrate them after the rest of the
  1313. * list is processed.
  1314. */
  1315. case -ENOSYS:
  1316. /* THP migration is unsupported */
  1317. if (is_thp) {
  1318. nr_thp_failed++;
  1319. if (!try_split_thp(page, &thp_split_pages)) {
  1320. nr_thp_split++;
  1321. break;
  1322. }
  1323. /* Hugetlb migration is unsupported */
  1324. } else if (!no_subpage_counting) {
  1325. nr_failed++;
  1326. }
  1327. nr_failed_pages += nr_subpages;
  1328. list_move_tail(&page->lru, &ret_pages);
  1329. break;
  1330. case -ENOMEM:
  1331. /*
  1332. * When memory is low, don't bother to try to migrate
  1333. * other pages, just exit.
  1334. */
  1335. if (is_thp) {
  1336. nr_thp_failed++;
  1337. /* THP NUMA faulting doesn't split THP to retry. */
  1338. if (!nosplit && !try_split_thp(page, &thp_split_pages)) {
  1339. nr_thp_split++;
  1340. break;
  1341. }
  1342. } else if (!no_subpage_counting) {
  1343. nr_failed++;
  1344. }
  1345. nr_failed_pages += nr_subpages + nr_retry_pages;
  1346. /*
  1347. * There might be some subpages of fail-to-migrate THPs
  1348. * left in thp_split_pages list. Move them back to migration
  1349. * list so that they could be put back to the right list by
  1350. * the caller otherwise the page refcnt will be leaked.
  1351. */
  1352. list_splice_init(&thp_split_pages, from);
  1353. /* nr_failed isn't updated for not used */
  1354. nr_thp_failed += thp_retry;
  1355. goto out;
  1356. case -EAGAIN:
  1357. if (is_thp)
  1358. thp_retry++;
  1359. else if (!no_subpage_counting)
  1360. retry++;
  1361. nr_retry_pages += nr_subpages;
  1362. break;
  1363. case MIGRATEPAGE_SUCCESS:
  1364. nr_succeeded += nr_subpages;
  1365. if (is_thp)
  1366. nr_thp_succeeded++;
  1367. break;
  1368. default:
  1369. /*
  1370. * Permanent failure (-EBUSY, etc.):
  1371. * unlike -EAGAIN case, the failed page is
  1372. * removed from migration page list and not
  1373. * retried in the next outer loop.
  1374. */
  1375. if (is_thp)
  1376. nr_thp_failed++;
  1377. else if (!no_subpage_counting)
  1378. nr_failed++;
  1379. nr_failed_pages += nr_subpages;
  1380. break;
  1381. }
  1382. }
  1383. }
  1384. nr_failed += retry;
  1385. nr_thp_failed += thp_retry;
  1386. nr_failed_pages += nr_retry_pages;
  1387. /*
  1388. * Try to migrate subpages of fail-to-migrate THPs, no nr_failed
  1389. * counting in this round, since all subpages of a THP is counted
  1390. * as 1 failure in the first round.
  1391. */
  1392. if (!list_empty(&thp_split_pages)) {
  1393. /*
  1394. * Move non-migrated pages (after 10 retries) to ret_pages
  1395. * to avoid migrating them again.
  1396. */
  1397. list_splice_init(from, &ret_pages);
  1398. list_splice_init(&thp_split_pages, from);
  1399. no_subpage_counting = true;
  1400. retry = 1;
  1401. goto thp_subpage_migration;
  1402. }
  1403. rc = nr_failed + nr_thp_failed;
  1404. out:
  1405. /*
  1406. * Put the permanent failure page back to migration list, they
  1407. * will be put back to the right list by the caller.
  1408. */
  1409. list_splice(&ret_pages, from);
  1410. /*
  1411. * Return 0 in case all subpages of fail-to-migrate THPs are
  1412. * migrated successfully.
  1413. */
  1414. if (list_empty(from))
  1415. rc = 0;
  1416. count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
  1417. count_vm_events(PGMIGRATE_FAIL, nr_failed_pages);
  1418. count_vm_events(THP_MIGRATION_SUCCESS, nr_thp_succeeded);
  1419. count_vm_events(THP_MIGRATION_FAIL, nr_thp_failed);
  1420. count_vm_events(THP_MIGRATION_SPLIT, nr_thp_split);
  1421. trace_mm_migrate_pages(nr_succeeded, nr_failed_pages, nr_thp_succeeded,
  1422. nr_thp_failed, nr_thp_split, mode, reason);
  1423. if (ret_succeeded)
  1424. *ret_succeeded = nr_succeeded;
  1425. return rc;
  1426. }
  1427. EXPORT_SYMBOL_GPL(migrate_pages);
  1428. struct page *alloc_migration_target(struct page *page, unsigned long private)
  1429. {
  1430. struct folio *folio = page_folio(page);
  1431. struct migration_target_control *mtc;
  1432. gfp_t gfp_mask;
  1433. unsigned int order = 0;
  1434. struct folio *new_folio = NULL;
  1435. int nid;
  1436. int zidx;
  1437. mtc = (struct migration_target_control *)private;
  1438. gfp_mask = mtc->gfp_mask;
  1439. nid = mtc->nid;
  1440. if (nid == NUMA_NO_NODE)
  1441. nid = folio_nid(folio);
  1442. if (folio_test_hugetlb(folio)) {
  1443. struct hstate *h = page_hstate(&folio->page);
  1444. gfp_mask = htlb_modify_alloc_mask(h, gfp_mask);
  1445. return alloc_huge_page_nodemask(h, nid, mtc->nmask, gfp_mask);
  1446. }
  1447. if (folio_test_large(folio)) {
  1448. /*
  1449. * clear __GFP_RECLAIM to make the migration callback
  1450. * consistent with regular THP allocations.
  1451. */
  1452. gfp_mask &= ~__GFP_RECLAIM;
  1453. gfp_mask |= GFP_TRANSHUGE;
  1454. order = folio_order(folio);
  1455. }
  1456. zidx = zone_idx(folio_zone(folio));
  1457. if (is_highmem_idx(zidx) || zidx == ZONE_MOVABLE)
  1458. gfp_mask |= __GFP_HIGHMEM;
  1459. new_folio = __folio_alloc(gfp_mask, order, nid, mtc->nmask);
  1460. return &new_folio->page;
  1461. }
  1462. #ifdef CONFIG_NUMA
  1463. static int store_status(int __user *status, int start, int value, int nr)
  1464. {
  1465. while (nr-- > 0) {
  1466. if (put_user(value, status + start))
  1467. return -EFAULT;
  1468. start++;
  1469. }
  1470. return 0;
  1471. }
  1472. static int do_move_pages_to_node(struct mm_struct *mm,
  1473. struct list_head *pagelist, int node)
  1474. {
  1475. int err;
  1476. struct migration_target_control mtc = {
  1477. .nid = node,
  1478. .gfp_mask = GFP_HIGHUSER_MOVABLE | __GFP_THISNODE,
  1479. };
  1480. err = migrate_pages(pagelist, alloc_migration_target, NULL,
  1481. (unsigned long)&mtc, MIGRATE_SYNC, MR_SYSCALL, NULL);
  1482. if (err)
  1483. putback_movable_pages(pagelist);
  1484. return err;
  1485. }
  1486. /*
  1487. * Resolves the given address to a struct page, isolates it from the LRU and
  1488. * puts it to the given pagelist.
  1489. * Returns:
  1490. * errno - if the page cannot be found/isolated
  1491. * 0 - when it doesn't have to be migrated because it is already on the
  1492. * target node
  1493. * 1 - when it has been queued
  1494. */
  1495. static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
  1496. int node, struct list_head *pagelist, bool migrate_all)
  1497. {
  1498. struct vm_area_struct *vma;
  1499. struct page *page;
  1500. int err;
  1501. mmap_read_lock(mm);
  1502. err = -EFAULT;
  1503. vma = vma_lookup(mm, addr);
  1504. if (!vma || !vma_migratable(vma))
  1505. goto out;
  1506. /* FOLL_DUMP to ignore special (like zero) pages */
  1507. page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP);
  1508. err = PTR_ERR(page);
  1509. if (IS_ERR(page))
  1510. goto out;
  1511. err = -ENOENT;
  1512. if (!page)
  1513. goto out;
  1514. if (is_zone_device_page(page))
  1515. goto out_putpage;
  1516. err = 0;
  1517. if (page_to_nid(page) == node)
  1518. goto out_putpage;
  1519. err = -EACCES;
  1520. if (page_mapcount(page) > 1 && !migrate_all)
  1521. goto out_putpage;
  1522. if (PageHuge(page)) {
  1523. if (PageHead(page)) {
  1524. err = isolate_hugetlb(page, pagelist);
  1525. if (!err)
  1526. err = 1;
  1527. }
  1528. } else {
  1529. struct page *head;
  1530. head = compound_head(page);
  1531. err = isolate_lru_page(head);
  1532. if (err)
  1533. goto out_putpage;
  1534. err = 1;
  1535. list_add_tail(&head->lru, pagelist);
  1536. mod_node_page_state(page_pgdat(head),
  1537. NR_ISOLATED_ANON + page_is_file_lru(head),
  1538. thp_nr_pages(head));
  1539. }
  1540. out_putpage:
  1541. /*
  1542. * Either remove the duplicate refcount from
  1543. * isolate_lru_page() or drop the page ref if it was
  1544. * not isolated.
  1545. */
  1546. put_page(page);
  1547. out:
  1548. mmap_read_unlock(mm);
  1549. return err;
  1550. }
  1551. static int move_pages_and_store_status(struct mm_struct *mm, int node,
  1552. struct list_head *pagelist, int __user *status,
  1553. int start, int i, unsigned long nr_pages)
  1554. {
  1555. int err;
  1556. if (list_empty(pagelist))
  1557. return 0;
  1558. err = do_move_pages_to_node(mm, pagelist, node);
  1559. if (err) {
  1560. /*
  1561. * Positive err means the number of failed
  1562. * pages to migrate. Since we are going to
  1563. * abort and return the number of non-migrated
  1564. * pages, so need to include the rest of the
  1565. * nr_pages that have not been attempted as
  1566. * well.
  1567. */
  1568. if (err > 0)
  1569. err += nr_pages - i;
  1570. return err;
  1571. }
  1572. return store_status(status, start, node, i - start);
  1573. }
  1574. /*
  1575. * Migrate an array of page address onto an array of nodes and fill
  1576. * the corresponding array of status.
  1577. */
  1578. static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
  1579. unsigned long nr_pages,
  1580. const void __user * __user *pages,
  1581. const int __user *nodes,
  1582. int __user *status, int flags)
  1583. {
  1584. compat_uptr_t __user *compat_pages = (void __user *)pages;
  1585. int current_node = NUMA_NO_NODE;
  1586. LIST_HEAD(pagelist);
  1587. int start, i;
  1588. int err = 0, err1;
  1589. lru_cache_disable();
  1590. for (i = start = 0; i < nr_pages; i++) {
  1591. const void __user *p;
  1592. unsigned long addr;
  1593. int node;
  1594. err = -EFAULT;
  1595. if (in_compat_syscall()) {
  1596. compat_uptr_t cp;
  1597. if (get_user(cp, compat_pages + i))
  1598. goto out_flush;
  1599. p = compat_ptr(cp);
  1600. } else {
  1601. if (get_user(p, pages + i))
  1602. goto out_flush;
  1603. }
  1604. if (get_user(node, nodes + i))
  1605. goto out_flush;
  1606. addr = (unsigned long)untagged_addr(p);
  1607. err = -ENODEV;
  1608. if (node < 0 || node >= MAX_NUMNODES)
  1609. goto out_flush;
  1610. if (!node_state(node, N_MEMORY))
  1611. goto out_flush;
  1612. err = -EACCES;
  1613. if (!node_isset(node, task_nodes))
  1614. goto out_flush;
  1615. if (current_node == NUMA_NO_NODE) {
  1616. current_node = node;
  1617. start = i;
  1618. } else if (node != current_node) {
  1619. err = move_pages_and_store_status(mm, current_node,
  1620. &pagelist, status, start, i, nr_pages);
  1621. if (err)
  1622. goto out;
  1623. start = i;
  1624. current_node = node;
  1625. }
  1626. /*
  1627. * Errors in the page lookup or isolation are not fatal and we simply
  1628. * report them via status
  1629. */
  1630. err = add_page_for_migration(mm, addr, current_node,
  1631. &pagelist, flags & MPOL_MF_MOVE_ALL);
  1632. if (err > 0) {
  1633. /* The page is successfully queued for migration */
  1634. continue;
  1635. }
  1636. /*
  1637. * The move_pages() man page does not have an -EEXIST choice, so
  1638. * use -EFAULT instead.
  1639. */
  1640. if (err == -EEXIST)
  1641. err = -EFAULT;
  1642. /*
  1643. * If the page is already on the target node (!err), store the
  1644. * node, otherwise, store the err.
  1645. */
  1646. err = store_status(status, i, err ? : current_node, 1);
  1647. if (err)
  1648. goto out_flush;
  1649. err = move_pages_and_store_status(mm, current_node, &pagelist,
  1650. status, start, i, nr_pages);
  1651. if (err) {
  1652. /* We have accounted for page i */
  1653. if (err > 0)
  1654. err--;
  1655. goto out;
  1656. }
  1657. current_node = NUMA_NO_NODE;
  1658. }
  1659. out_flush:
  1660. /* Make sure we do not overwrite the existing error */
  1661. err1 = move_pages_and_store_status(mm, current_node, &pagelist,
  1662. status, start, i, nr_pages);
  1663. if (err >= 0)
  1664. err = err1;
  1665. out:
  1666. lru_cache_enable();
  1667. return err;
  1668. }
  1669. /*
  1670. * Determine the nodes of an array of pages and store it in an array of status.
  1671. */
  1672. static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
  1673. const void __user **pages, int *status)
  1674. {
  1675. unsigned long i;
  1676. mmap_read_lock(mm);
  1677. for (i = 0; i < nr_pages; i++) {
  1678. unsigned long addr = (unsigned long)(*pages);
  1679. unsigned int foll_flags = FOLL_DUMP;
  1680. struct vm_area_struct *vma;
  1681. struct page *page;
  1682. int err = -EFAULT;
  1683. vma = vma_lookup(mm, addr);
  1684. if (!vma)
  1685. goto set_status;
  1686. /* Not all huge page follow APIs support 'FOLL_GET' */
  1687. if (!is_vm_hugetlb_page(vma))
  1688. foll_flags |= FOLL_GET;
  1689. /* FOLL_DUMP to ignore special (like zero) pages */
  1690. page = follow_page(vma, addr, foll_flags);
  1691. err = PTR_ERR(page);
  1692. if (IS_ERR(page))
  1693. goto set_status;
  1694. err = -ENOENT;
  1695. if (!page)
  1696. goto set_status;
  1697. if (!is_zone_device_page(page))
  1698. err = page_to_nid(page);
  1699. if (foll_flags & FOLL_GET)
  1700. put_page(page);
  1701. set_status:
  1702. *status = err;
  1703. pages++;
  1704. status++;
  1705. }
  1706. mmap_read_unlock(mm);
  1707. }
  1708. static int get_compat_pages_array(const void __user *chunk_pages[],
  1709. const void __user * __user *pages,
  1710. unsigned long chunk_nr)
  1711. {
  1712. compat_uptr_t __user *pages32 = (compat_uptr_t __user *)pages;
  1713. compat_uptr_t p;
  1714. int i;
  1715. for (i = 0; i < chunk_nr; i++) {
  1716. if (get_user(p, pages32 + i))
  1717. return -EFAULT;
  1718. chunk_pages[i] = compat_ptr(p);
  1719. }
  1720. return 0;
  1721. }
  1722. /*
  1723. * Determine the nodes of a user array of pages and store it in
  1724. * a user array of status.
  1725. */
  1726. static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
  1727. const void __user * __user *pages,
  1728. int __user *status)
  1729. {
  1730. #define DO_PAGES_STAT_CHUNK_NR 16UL
  1731. const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
  1732. int chunk_status[DO_PAGES_STAT_CHUNK_NR];
  1733. while (nr_pages) {
  1734. unsigned long chunk_nr = min(nr_pages, DO_PAGES_STAT_CHUNK_NR);
  1735. if (in_compat_syscall()) {
  1736. if (get_compat_pages_array(chunk_pages, pages,
  1737. chunk_nr))
  1738. break;
  1739. } else {
  1740. if (copy_from_user(chunk_pages, pages,
  1741. chunk_nr * sizeof(*chunk_pages)))
  1742. break;
  1743. }
  1744. do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
  1745. if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
  1746. break;
  1747. pages += chunk_nr;
  1748. status += chunk_nr;
  1749. nr_pages -= chunk_nr;
  1750. }
  1751. return nr_pages ? -EFAULT : 0;
  1752. }
  1753. static struct mm_struct *find_mm_struct(pid_t pid, nodemask_t *mem_nodes)
  1754. {
  1755. struct task_struct *task;
  1756. struct mm_struct *mm;
  1757. /*
  1758. * There is no need to check if current process has the right to modify
  1759. * the specified process when they are same.
  1760. */
  1761. if (!pid) {
  1762. mmget(current->mm);
  1763. *mem_nodes = cpuset_mems_allowed(current);
  1764. return current->mm;
  1765. }
  1766. /* Find the mm_struct */
  1767. rcu_read_lock();
  1768. task = find_task_by_vpid(pid);
  1769. if (!task) {
  1770. rcu_read_unlock();
  1771. return ERR_PTR(-ESRCH);
  1772. }
  1773. get_task_struct(task);
  1774. /*
  1775. * Check if this process has the right to modify the specified
  1776. * process. Use the regular "ptrace_may_access()" checks.
  1777. */
  1778. if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
  1779. rcu_read_unlock();
  1780. mm = ERR_PTR(-EPERM);
  1781. goto out;
  1782. }
  1783. rcu_read_unlock();
  1784. mm = ERR_PTR(security_task_movememory(task));
  1785. if (IS_ERR(mm))
  1786. goto out;
  1787. *mem_nodes = cpuset_mems_allowed(task);
  1788. mm = get_task_mm(task);
  1789. out:
  1790. put_task_struct(task);
  1791. if (!mm)
  1792. mm = ERR_PTR(-EINVAL);
  1793. return mm;
  1794. }
  1795. /*
  1796. * Move a list of pages in the address space of the currently executing
  1797. * process.
  1798. */
  1799. static int kernel_move_pages(pid_t pid, unsigned long nr_pages,
  1800. const void __user * __user *pages,
  1801. const int __user *nodes,
  1802. int __user *status, int flags)
  1803. {
  1804. struct mm_struct *mm;
  1805. int err;
  1806. nodemask_t task_nodes;
  1807. /* Check flags */
  1808. if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
  1809. return -EINVAL;
  1810. if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
  1811. return -EPERM;
  1812. mm = find_mm_struct(pid, &task_nodes);
  1813. if (IS_ERR(mm))
  1814. return PTR_ERR(mm);
  1815. if (nodes)
  1816. err = do_pages_move(mm, task_nodes, nr_pages, pages,
  1817. nodes, status, flags);
  1818. else
  1819. err = do_pages_stat(mm, nr_pages, pages, status);
  1820. mmput(mm);
  1821. return err;
  1822. }
  1823. SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
  1824. const void __user * __user *, pages,
  1825. const int __user *, nodes,
  1826. int __user *, status, int, flags)
  1827. {
  1828. return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags);
  1829. }
  1830. #ifdef CONFIG_NUMA_BALANCING
  1831. /*
  1832. * Returns true if this is a safe migration target node for misplaced NUMA
  1833. * pages. Currently it only checks the watermarks which is crude.
  1834. */
  1835. static bool migrate_balanced_pgdat(struct pglist_data *pgdat,
  1836. unsigned long nr_migrate_pages)
  1837. {
  1838. int z;
  1839. for (z = pgdat->nr_zones - 1; z >= 0; z--) {
  1840. struct zone *zone = pgdat->node_zones + z;
  1841. if (!managed_zone(zone))
  1842. continue;
  1843. /* Avoid waking kswapd by allocating pages_to_migrate pages. */
  1844. if (!zone_watermark_ok(zone, 0,
  1845. high_wmark_pages(zone) +
  1846. nr_migrate_pages,
  1847. ZONE_MOVABLE, 0))
  1848. continue;
  1849. return true;
  1850. }
  1851. return false;
  1852. }
  1853. static struct page *alloc_misplaced_dst_page(struct page *page,
  1854. unsigned long data)
  1855. {
  1856. int nid = (int) data;
  1857. int order = compound_order(page);
  1858. gfp_t gfp = __GFP_THISNODE;
  1859. struct folio *new;
  1860. if (order > 0)
  1861. gfp |= GFP_TRANSHUGE_LIGHT;
  1862. else {
  1863. gfp |= GFP_HIGHUSER_MOVABLE | __GFP_NOMEMALLOC | __GFP_NORETRY |
  1864. __GFP_NOWARN;
  1865. gfp &= ~__GFP_RECLAIM;
  1866. }
  1867. new = __folio_alloc_node(gfp, order, nid);
  1868. return &new->page;
  1869. }
  1870. static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
  1871. {
  1872. int nr_pages = thp_nr_pages(page);
  1873. int order = compound_order(page);
  1874. VM_BUG_ON_PAGE(order && !PageTransHuge(page), page);
  1875. /* Do not migrate THP mapped by multiple processes */
  1876. if (PageTransHuge(page) && total_mapcount(page) > 1)
  1877. return 0;
  1878. /* Avoid migrating to a node that is nearly full */
  1879. if (!migrate_balanced_pgdat(pgdat, nr_pages)) {
  1880. int z;
  1881. if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING))
  1882. return 0;
  1883. for (z = pgdat->nr_zones - 1; z >= 0; z--) {
  1884. if (managed_zone(pgdat->node_zones + z))
  1885. break;
  1886. }
  1887. wakeup_kswapd(pgdat->node_zones + z, 0, order, ZONE_MOVABLE);
  1888. return 0;
  1889. }
  1890. if (isolate_lru_page(page))
  1891. return 0;
  1892. mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_is_file_lru(page),
  1893. nr_pages);
  1894. /*
  1895. * Isolating the page has taken another reference, so the
  1896. * caller's reference can be safely dropped without the page
  1897. * disappearing underneath us during migration.
  1898. */
  1899. put_page(page);
  1900. return 1;
  1901. }
  1902. /*
  1903. * Attempt to migrate a misplaced page to the specified destination
  1904. * node. Caller is expected to have an elevated reference count on
  1905. * the page that will be dropped by this function before returning.
  1906. */
  1907. int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma,
  1908. int node)
  1909. {
  1910. pg_data_t *pgdat = NODE_DATA(node);
  1911. int isolated;
  1912. int nr_remaining;
  1913. unsigned int nr_succeeded;
  1914. LIST_HEAD(migratepages);
  1915. int nr_pages = thp_nr_pages(page);
  1916. /*
  1917. * Don't migrate file pages that are mapped in multiple processes
  1918. * with execute permissions as they are probably shared libraries.
  1919. */
  1920. if (page_mapcount(page) != 1 && page_is_file_lru(page) &&
  1921. (vma->vm_flags & VM_EXEC))
  1922. goto out;
  1923. /*
  1924. * Also do not migrate dirty pages as not all filesystems can move
  1925. * dirty pages in MIGRATE_ASYNC mode which is a waste of cycles.
  1926. */
  1927. if (page_is_file_lru(page) && PageDirty(page))
  1928. goto out;
  1929. isolated = numamigrate_isolate_page(pgdat, page);
  1930. if (!isolated)
  1931. goto out;
  1932. list_add(&page->lru, &migratepages);
  1933. nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_page,
  1934. NULL, node, MIGRATE_ASYNC,
  1935. MR_NUMA_MISPLACED, &nr_succeeded);
  1936. if (nr_remaining) {
  1937. if (!list_empty(&migratepages)) {
  1938. list_del(&page->lru);
  1939. mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
  1940. page_is_file_lru(page), -nr_pages);
  1941. putback_lru_page(page);
  1942. }
  1943. isolated = 0;
  1944. }
  1945. if (nr_succeeded) {
  1946. count_vm_numa_events(NUMA_PAGE_MIGRATE, nr_succeeded);
  1947. if (!node_is_toptier(page_to_nid(page)) && node_is_toptier(node))
  1948. mod_node_page_state(pgdat, PGPROMOTE_SUCCESS,
  1949. nr_succeeded);
  1950. }
  1951. BUG_ON(!list_empty(&migratepages));
  1952. return isolated;
  1953. out:
  1954. put_page(page);
  1955. return 0;
  1956. }
  1957. #endif /* CONFIG_NUMA_BALANCING */
  1958. #endif /* CONFIG_NUMA */