migrate_device.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Device Memory Migration functionality.
  4. *
  5. * Originally written by Jérôme Glisse.
  6. */
  7. #include <linux/export.h>
  8. #include <linux/memremap.h>
  9. #include <linux/migrate.h>
  10. #include <linux/mm.h>
  11. #include <linux/mm_inline.h>
  12. #include <linux/mmu_notifier.h>
  13. #include <linux/oom.h>
  14. #include <linux/pagewalk.h>
  15. #include <linux/rmap.h>
  16. #include <linux/leafops.h>
  17. #include <linux/pgalloc.h>
  18. #include <asm/tlbflush.h>
  19. #include "internal.h"
  20. static int migrate_vma_collect_skip(unsigned long start,
  21. unsigned long end,
  22. struct mm_walk *walk)
  23. {
  24. struct migrate_vma *migrate = walk->private;
  25. unsigned long addr;
  26. for (addr = start; addr < end; addr += PAGE_SIZE) {
  27. migrate->dst[migrate->npages] = 0;
  28. migrate->src[migrate->npages++] = 0;
  29. }
  30. return 0;
  31. }
  32. static int migrate_vma_collect_hole(unsigned long start,
  33. unsigned long end,
  34. __always_unused int depth,
  35. struct mm_walk *walk)
  36. {
  37. struct migrate_vma *migrate = walk->private;
  38. unsigned long addr;
  39. /* Only allow populating anonymous memory. */
  40. if (!vma_is_anonymous(walk->vma))
  41. return migrate_vma_collect_skip(start, end, walk);
  42. if (thp_migration_supported() &&
  43. (migrate->flags & MIGRATE_VMA_SELECT_COMPOUND) &&
  44. (IS_ALIGNED(start, HPAGE_PMD_SIZE) &&
  45. IS_ALIGNED(end, HPAGE_PMD_SIZE))) {
  46. migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE |
  47. MIGRATE_PFN_COMPOUND;
  48. migrate->dst[migrate->npages] = 0;
  49. migrate->npages++;
  50. migrate->cpages++;
  51. /*
  52. * Collect the remaining entries as holes, in case we
  53. * need to split later
  54. */
  55. return migrate_vma_collect_skip(start + PAGE_SIZE, end, walk);
  56. }
  57. for (addr = start; addr < end; addr += PAGE_SIZE) {
  58. migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE;
  59. migrate->dst[migrate->npages] = 0;
  60. migrate->npages++;
  61. migrate->cpages++;
  62. }
  63. return 0;
  64. }
  65. /**
  66. * migrate_vma_split_folio() - Helper function to split a THP folio
  67. * @folio: the folio to split
  68. * @fault_page: struct page associated with the fault if any
  69. *
  70. * Returns 0 on success
  71. */
  72. static int migrate_vma_split_folio(struct folio *folio,
  73. struct page *fault_page)
  74. {
  75. int ret;
  76. struct folio *fault_folio = fault_page ? page_folio(fault_page) : NULL;
  77. struct folio *new_fault_folio = NULL;
  78. if (folio != fault_folio) {
  79. folio_get(folio);
  80. folio_lock(folio);
  81. }
  82. ret = split_folio(folio);
  83. if (ret) {
  84. if (folio != fault_folio) {
  85. folio_unlock(folio);
  86. folio_put(folio);
  87. }
  88. return ret;
  89. }
  90. new_fault_folio = fault_page ? page_folio(fault_page) : NULL;
  91. /*
  92. * Ensure the lock is held on the correct
  93. * folio after the split
  94. */
  95. if (!new_fault_folio) {
  96. folio_unlock(folio);
  97. folio_put(folio);
  98. } else if (folio != new_fault_folio) {
  99. if (new_fault_folio != fault_folio) {
  100. folio_get(new_fault_folio);
  101. folio_lock(new_fault_folio);
  102. }
  103. folio_unlock(folio);
  104. folio_put(folio);
  105. }
  106. return 0;
  107. }
  108. /** migrate_vma_collect_huge_pmd - collect THP pages without splitting the
  109. * folio for device private pages.
  110. * @pmdp: pointer to pmd entry
  111. * @start: start address of the range for migration
  112. * @end: end address of the range for migration
  113. * @walk: mm_walk callback structure
  114. * @fault_folio: folio associated with the fault if any
  115. *
  116. * Collect the huge pmd entry at @pmdp for migration and set the
  117. * MIGRATE_PFN_COMPOUND flag in the migrate src entry to indicate that
  118. * migration will occur at HPAGE_PMD granularity
  119. */
  120. static int migrate_vma_collect_huge_pmd(pmd_t *pmdp, unsigned long start,
  121. unsigned long end, struct mm_walk *walk,
  122. struct folio *fault_folio)
  123. {
  124. struct mm_struct *mm = walk->mm;
  125. struct folio *folio;
  126. struct migrate_vma *migrate = walk->private;
  127. spinlock_t *ptl;
  128. int ret;
  129. unsigned long write = 0;
  130. ptl = pmd_lock(mm, pmdp);
  131. if (pmd_none(*pmdp)) {
  132. spin_unlock(ptl);
  133. return migrate_vma_collect_hole(start, end, -1, walk);
  134. }
  135. if (pmd_trans_huge(*pmdp)) {
  136. if (!(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) {
  137. spin_unlock(ptl);
  138. return migrate_vma_collect_skip(start, end, walk);
  139. }
  140. folio = pmd_folio(*pmdp);
  141. if (is_huge_zero_folio(folio)) {
  142. spin_unlock(ptl);
  143. return migrate_vma_collect_hole(start, end, -1, walk);
  144. }
  145. if (pmd_write(*pmdp))
  146. write = MIGRATE_PFN_WRITE;
  147. } else if (!pmd_present(*pmdp)) {
  148. const softleaf_t entry = softleaf_from_pmd(*pmdp);
  149. folio = softleaf_to_folio(entry);
  150. if (!softleaf_is_device_private(entry) ||
  151. !(migrate->flags & MIGRATE_VMA_SELECT_DEVICE_PRIVATE) ||
  152. (folio->pgmap->owner != migrate->pgmap_owner)) {
  153. spin_unlock(ptl);
  154. return migrate_vma_collect_skip(start, end, walk);
  155. }
  156. if (softleaf_is_migration(entry)) {
  157. softleaf_entry_wait_on_locked(entry, ptl);
  158. spin_unlock(ptl);
  159. return -EAGAIN;
  160. }
  161. if (softleaf_is_device_private_write(entry))
  162. write = MIGRATE_PFN_WRITE;
  163. } else {
  164. spin_unlock(ptl);
  165. return -EAGAIN;
  166. }
  167. folio_get(folio);
  168. if (folio != fault_folio && unlikely(!folio_trylock(folio))) {
  169. spin_unlock(ptl);
  170. folio_put(folio);
  171. return migrate_vma_collect_skip(start, end, walk);
  172. }
  173. if (thp_migration_supported() &&
  174. (migrate->flags & MIGRATE_VMA_SELECT_COMPOUND) &&
  175. (IS_ALIGNED(start, HPAGE_PMD_SIZE) &&
  176. IS_ALIGNED(end, HPAGE_PMD_SIZE))) {
  177. struct page_vma_mapped_walk pvmw = {
  178. .ptl = ptl,
  179. .address = start,
  180. .pmd = pmdp,
  181. .vma = walk->vma,
  182. };
  183. unsigned long pfn = page_to_pfn(folio_page(folio, 0));
  184. migrate->src[migrate->npages] = migrate_pfn(pfn) | write
  185. | MIGRATE_PFN_MIGRATE
  186. | MIGRATE_PFN_COMPOUND;
  187. migrate->dst[migrate->npages++] = 0;
  188. migrate->cpages++;
  189. ret = set_pmd_migration_entry(&pvmw, folio_page(folio, 0));
  190. if (ret) {
  191. migrate->npages--;
  192. migrate->cpages--;
  193. migrate->src[migrate->npages] = 0;
  194. migrate->dst[migrate->npages] = 0;
  195. goto fallback;
  196. }
  197. migrate_vma_collect_skip(start + PAGE_SIZE, end, walk);
  198. spin_unlock(ptl);
  199. return 0;
  200. }
  201. fallback:
  202. spin_unlock(ptl);
  203. if (!folio_test_large(folio))
  204. goto done;
  205. ret = split_folio(folio);
  206. if (fault_folio != folio)
  207. folio_unlock(folio);
  208. folio_put(folio);
  209. if (ret)
  210. return migrate_vma_collect_skip(start, end, walk);
  211. if (pmd_none(pmdp_get_lockless(pmdp)))
  212. return migrate_vma_collect_hole(start, end, -1, walk);
  213. done:
  214. return -ENOENT;
  215. }
  216. static int migrate_vma_collect_pmd(pmd_t *pmdp,
  217. unsigned long start,
  218. unsigned long end,
  219. struct mm_walk *walk)
  220. {
  221. struct migrate_vma *migrate = walk->private;
  222. struct vm_area_struct *vma = walk->vma;
  223. struct mm_struct *mm = vma->vm_mm;
  224. unsigned long addr = start, unmapped = 0;
  225. spinlock_t *ptl;
  226. struct folio *fault_folio = migrate->fault_page ?
  227. page_folio(migrate->fault_page) : NULL;
  228. pte_t *ptep;
  229. again:
  230. if (pmd_trans_huge(*pmdp) || !pmd_present(*pmdp)) {
  231. int ret = migrate_vma_collect_huge_pmd(pmdp, start, end, walk, fault_folio);
  232. if (ret == -EAGAIN)
  233. goto again;
  234. if (ret == 0)
  235. return 0;
  236. }
  237. ptep = pte_offset_map_lock(mm, pmdp, start, &ptl);
  238. if (!ptep)
  239. goto again;
  240. lazy_mmu_mode_enable();
  241. ptep += (addr - start) / PAGE_SIZE;
  242. for (; addr < end; addr += PAGE_SIZE, ptep++) {
  243. struct dev_pagemap *pgmap;
  244. unsigned long mpfn = 0, pfn;
  245. struct folio *folio;
  246. struct page *page;
  247. softleaf_t entry;
  248. pte_t pte;
  249. pte = ptep_get(ptep);
  250. if (pte_none(pte)) {
  251. if (vma_is_anonymous(vma)) {
  252. mpfn = MIGRATE_PFN_MIGRATE;
  253. migrate->cpages++;
  254. }
  255. goto next;
  256. }
  257. if (!pte_present(pte)) {
  258. /*
  259. * Only care about unaddressable device page special
  260. * page table entry. Other special swap entries are not
  261. * migratable, and we ignore regular swapped page.
  262. */
  263. entry = softleaf_from_pte(pte);
  264. if (!softleaf_is_device_private(entry))
  265. goto next;
  266. page = softleaf_to_page(entry);
  267. pgmap = page_pgmap(page);
  268. if (!(migrate->flags &
  269. MIGRATE_VMA_SELECT_DEVICE_PRIVATE) ||
  270. pgmap->owner != migrate->pgmap_owner)
  271. goto next;
  272. folio = page_folio(page);
  273. if (folio_test_large(folio)) {
  274. int ret;
  275. lazy_mmu_mode_disable();
  276. pte_unmap_unlock(ptep, ptl);
  277. ret = migrate_vma_split_folio(folio,
  278. migrate->fault_page);
  279. if (ret) {
  280. if (unmapped)
  281. flush_tlb_range(walk->vma, start, end);
  282. return migrate_vma_collect_skip(addr, end, walk);
  283. }
  284. goto again;
  285. }
  286. mpfn = migrate_pfn(page_to_pfn(page)) |
  287. MIGRATE_PFN_MIGRATE;
  288. if (softleaf_is_device_private_write(entry))
  289. mpfn |= MIGRATE_PFN_WRITE;
  290. } else {
  291. pfn = pte_pfn(pte);
  292. if (is_zero_pfn(pfn) &&
  293. (migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) {
  294. mpfn = MIGRATE_PFN_MIGRATE;
  295. migrate->cpages++;
  296. goto next;
  297. }
  298. page = vm_normal_page(migrate->vma, addr, pte);
  299. if (page && !is_zone_device_page(page) &&
  300. !(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) {
  301. goto next;
  302. } else if (page && is_device_coherent_page(page)) {
  303. pgmap = page_pgmap(page);
  304. if (!(migrate->flags &
  305. MIGRATE_VMA_SELECT_DEVICE_COHERENT) ||
  306. pgmap->owner != migrate->pgmap_owner)
  307. goto next;
  308. }
  309. folio = page ? page_folio(page) : NULL;
  310. if (folio && folio_test_large(folio)) {
  311. int ret;
  312. lazy_mmu_mode_disable();
  313. pte_unmap_unlock(ptep, ptl);
  314. ret = migrate_vma_split_folio(folio,
  315. migrate->fault_page);
  316. if (ret) {
  317. if (unmapped)
  318. flush_tlb_range(walk->vma, start, end);
  319. return migrate_vma_collect_skip(addr, end, walk);
  320. }
  321. goto again;
  322. }
  323. mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
  324. mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0;
  325. }
  326. if (!page || !page->mapping) {
  327. mpfn = 0;
  328. goto next;
  329. }
  330. /*
  331. * By getting a reference on the folio we pin it and that blocks
  332. * any kind of migration. Side effect is that it "freezes" the
  333. * pte.
  334. *
  335. * We drop this reference after isolating the folio from the lru
  336. * for non device folio (device folio are not on the lru and thus
  337. * can't be dropped from it).
  338. */
  339. folio = page_folio(page);
  340. folio_get(folio);
  341. /*
  342. * We rely on folio_trylock() to avoid deadlock between
  343. * concurrent migrations where each is waiting on the others
  344. * folio lock. If we can't immediately lock the folio we fail this
  345. * migration as it is only best effort anyway.
  346. *
  347. * If we can lock the folio it's safe to set up a migration entry
  348. * now. In the common case where the folio is mapped once in a
  349. * single process setting up the migration entry now is an
  350. * optimisation to avoid walking the rmap later with
  351. * try_to_migrate().
  352. */
  353. if (fault_folio == folio || folio_trylock(folio)) {
  354. bool anon_exclusive;
  355. pte_t swp_pte;
  356. flush_cache_page(vma, addr, pte_pfn(pte));
  357. anon_exclusive = folio_test_anon(folio) &&
  358. PageAnonExclusive(page);
  359. if (anon_exclusive) {
  360. pte = ptep_clear_flush(vma, addr, ptep);
  361. if (folio_try_share_anon_rmap_pte(folio, page)) {
  362. set_pte_at(mm, addr, ptep, pte);
  363. if (fault_folio != folio)
  364. folio_unlock(folio);
  365. folio_put(folio);
  366. mpfn = 0;
  367. goto next;
  368. }
  369. } else {
  370. pte = ptep_get_and_clear(mm, addr, ptep);
  371. }
  372. migrate->cpages++;
  373. /* Set the dirty flag on the folio now the pte is gone. */
  374. if (pte_dirty(pte))
  375. folio_mark_dirty(folio);
  376. /* Setup special migration page table entry */
  377. if (mpfn & MIGRATE_PFN_WRITE)
  378. entry = make_writable_migration_entry(
  379. page_to_pfn(page));
  380. else if (anon_exclusive)
  381. entry = make_readable_exclusive_migration_entry(
  382. page_to_pfn(page));
  383. else
  384. entry = make_readable_migration_entry(
  385. page_to_pfn(page));
  386. if (pte_present(pte)) {
  387. if (pte_young(pte))
  388. entry = make_migration_entry_young(entry);
  389. if (pte_dirty(pte))
  390. entry = make_migration_entry_dirty(entry);
  391. }
  392. swp_pte = swp_entry_to_pte(entry);
  393. if (pte_present(pte)) {
  394. if (pte_soft_dirty(pte))
  395. swp_pte = pte_swp_mksoft_dirty(swp_pte);
  396. if (pte_uffd_wp(pte))
  397. swp_pte = pte_swp_mkuffd_wp(swp_pte);
  398. } else {
  399. if (pte_swp_soft_dirty(pte))
  400. swp_pte = pte_swp_mksoft_dirty(swp_pte);
  401. if (pte_swp_uffd_wp(pte))
  402. swp_pte = pte_swp_mkuffd_wp(swp_pte);
  403. }
  404. set_pte_at(mm, addr, ptep, swp_pte);
  405. /*
  406. * This is like regular unmap: we remove the rmap and
  407. * drop the folio refcount. The folio won't be freed, as
  408. * we took a reference just above.
  409. */
  410. folio_remove_rmap_pte(folio, page, vma);
  411. folio_put(folio);
  412. if (pte_present(pte))
  413. unmapped++;
  414. } else {
  415. folio_put(folio);
  416. mpfn = 0;
  417. }
  418. next:
  419. migrate->dst[migrate->npages] = 0;
  420. migrate->src[migrate->npages++] = mpfn;
  421. }
  422. /* Only flush the TLB if we actually modified any entries */
  423. if (unmapped)
  424. flush_tlb_range(walk->vma, start, end);
  425. lazy_mmu_mode_disable();
  426. pte_unmap_unlock(ptep - 1, ptl);
  427. return 0;
  428. }
  429. static const struct mm_walk_ops migrate_vma_walk_ops = {
  430. .pmd_entry = migrate_vma_collect_pmd,
  431. .pte_hole = migrate_vma_collect_hole,
  432. .walk_lock = PGWALK_RDLOCK,
  433. };
  434. /*
  435. * migrate_vma_collect() - collect pages over a range of virtual addresses
  436. * @migrate: migrate struct containing all migration information
  437. *
  438. * This will walk the CPU page table. For each virtual address backed by a
  439. * valid page, it updates the src array and takes a reference on the page, in
  440. * order to pin the page until we lock it and unmap it.
  441. */
  442. static void migrate_vma_collect(struct migrate_vma *migrate)
  443. {
  444. struct mmu_notifier_range range;
  445. /*
  446. * Note that the pgmap_owner is passed to the mmu notifier callback so
  447. * that the registered device driver can skip invalidating device
  448. * private page mappings that won't be migrated.
  449. */
  450. mmu_notifier_range_init_owner(&range, MMU_NOTIFY_MIGRATE, 0,
  451. migrate->vma->vm_mm, migrate->start, migrate->end,
  452. migrate->pgmap_owner);
  453. mmu_notifier_invalidate_range_start(&range);
  454. walk_page_range(migrate->vma->vm_mm, migrate->start, migrate->end,
  455. &migrate_vma_walk_ops, migrate);
  456. mmu_notifier_invalidate_range_end(&range);
  457. migrate->end = migrate->start + (migrate->npages << PAGE_SHIFT);
  458. }
  459. /*
  460. * migrate_vma_check_page() - check if page is pinned or not
  461. * @page: struct page to check
  462. *
  463. * Pinned pages cannot be migrated. This is the same test as in
  464. * folio_migrate_mapping(), except that here we allow migration of a
  465. * ZONE_DEVICE page.
  466. */
  467. static bool migrate_vma_check_page(struct page *page, struct page *fault_page)
  468. {
  469. struct folio *folio = page_folio(page);
  470. /*
  471. * One extra ref because caller holds an extra reference, either from
  472. * folio_isolate_lru() for a regular folio, or migrate_vma_collect() for
  473. * a device folio.
  474. */
  475. int extra = 1 + (page == fault_page);
  476. /* Page from ZONE_DEVICE have one extra reference */
  477. if (folio_is_zone_device(folio))
  478. extra++;
  479. /* For file back page */
  480. if (folio_mapping(folio))
  481. extra += 1 + folio_has_private(folio);
  482. if ((folio_ref_count(folio) - extra) > folio_mapcount(folio))
  483. return false;
  484. return true;
  485. }
  486. /*
  487. * Unmaps pages for migration. Returns number of source pfns marked as
  488. * migrating.
  489. */
  490. static unsigned long migrate_device_unmap(unsigned long *src_pfns,
  491. unsigned long npages,
  492. struct page *fault_page)
  493. {
  494. struct folio *fault_folio = fault_page ?
  495. page_folio(fault_page) : NULL;
  496. unsigned long i, restore = 0;
  497. bool allow_drain = true;
  498. unsigned long unmapped = 0;
  499. lru_add_drain();
  500. for (i = 0; i < npages; ) {
  501. struct page *page = migrate_pfn_to_page(src_pfns[i]);
  502. struct folio *folio;
  503. unsigned int nr = 1;
  504. if (!page) {
  505. if (src_pfns[i] & MIGRATE_PFN_MIGRATE)
  506. unmapped++;
  507. goto next;
  508. }
  509. folio = page_folio(page);
  510. nr = folio_nr_pages(folio);
  511. if (nr > 1)
  512. src_pfns[i] |= MIGRATE_PFN_COMPOUND;
  513. /* ZONE_DEVICE folios are not on LRU */
  514. if (!folio_is_zone_device(folio)) {
  515. if (!folio_test_lru(folio) && allow_drain) {
  516. /* Drain CPU's lru cache */
  517. lru_add_drain_all();
  518. allow_drain = false;
  519. }
  520. if (!folio_isolate_lru(folio)) {
  521. src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
  522. restore++;
  523. goto next;
  524. }
  525. /* Drop the reference we took in collect */
  526. folio_put(folio);
  527. }
  528. if (folio_mapped(folio))
  529. try_to_migrate(folio, 0);
  530. if (folio_mapped(folio) ||
  531. !migrate_vma_check_page(page, fault_page)) {
  532. if (!folio_is_zone_device(folio)) {
  533. folio_get(folio);
  534. folio_putback_lru(folio);
  535. }
  536. src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
  537. restore++;
  538. goto next;
  539. }
  540. unmapped++;
  541. next:
  542. i += nr;
  543. }
  544. for (i = 0; i < npages && restore; i++) {
  545. struct page *page = migrate_pfn_to_page(src_pfns[i]);
  546. struct folio *folio;
  547. if (!page || (src_pfns[i] & MIGRATE_PFN_MIGRATE))
  548. continue;
  549. folio = page_folio(page);
  550. remove_migration_ptes(folio, folio, 0);
  551. src_pfns[i] = 0;
  552. if (fault_folio != folio)
  553. folio_unlock(folio);
  554. folio_put(folio);
  555. restore--;
  556. }
  557. return unmapped;
  558. }
  559. /*
  560. * migrate_vma_unmap() - replace page mapping with special migration pte entry
  561. * @migrate: migrate struct containing all migration information
  562. *
  563. * Isolate pages from the LRU and replace mappings (CPU page table pte) with a
  564. * special migration pte entry and check if it has been pinned. Pinned pages are
  565. * restored because we cannot migrate them.
  566. *
  567. * This is the last step before we call the device driver callback to allocate
  568. * destination memory and copy contents of original page over to new page.
  569. */
  570. static void migrate_vma_unmap(struct migrate_vma *migrate)
  571. {
  572. migrate->cpages = migrate_device_unmap(migrate->src, migrate->npages,
  573. migrate->fault_page);
  574. }
  575. /**
  576. * migrate_vma_setup() - prepare to migrate a range of memory
  577. * @args: contains the vma, start, and pfns arrays for the migration
  578. *
  579. * Returns: negative errno on failures, 0 when 0 or more pages were migrated
  580. * without an error.
  581. *
  582. * Prepare to migrate a range of memory virtual address range by collecting all
  583. * the pages backing each virtual address in the range, saving them inside the
  584. * src array. Then lock those pages and unmap them. Once the pages are locked
  585. * and unmapped, check whether each page is pinned or not. Pages that aren't
  586. * pinned have the MIGRATE_PFN_MIGRATE flag set (by this function) in the
  587. * corresponding src array entry. Then restores any pages that are pinned, by
  588. * remapping and unlocking those pages.
  589. *
  590. * The caller should then allocate destination memory and copy source memory to
  591. * it for all those entries (ie with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE
  592. * flag set). Once these are allocated and copied, the caller must update each
  593. * corresponding entry in the dst array with the pfn value of the destination
  594. * page and with MIGRATE_PFN_VALID. Destination pages must be locked via
  595. * lock_page().
  596. *
  597. * Note that the caller does not have to migrate all the pages that are marked
  598. * with MIGRATE_PFN_MIGRATE flag in src array unless this is a migration from
  599. * device memory to system memory. If the caller cannot migrate a device page
  600. * back to system memory, then it must return VM_FAULT_SIGBUS, which has severe
  601. * consequences for the userspace process, so it must be avoided if at all
  602. * possible.
  603. *
  604. * For empty entries inside CPU page table (pte_none() or pmd_none() is true) we
  605. * do set MIGRATE_PFN_MIGRATE flag inside the corresponding source array thus
  606. * allowing the caller to allocate device memory for those unbacked virtual
  607. * addresses. For this the caller simply has to allocate device memory and
  608. * properly set the destination entry like for regular migration. Note that
  609. * this can still fail, and thus inside the device driver you must check if the
  610. * migration was successful for those entries after calling migrate_vma_pages(),
  611. * just like for regular migration.
  612. *
  613. * After that, the callers must call migrate_vma_pages() to go over each entry
  614. * in the src array that has the MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag
  615. * set. If the corresponding entry in dst array has MIGRATE_PFN_VALID flag set,
  616. * then migrate_vma_pages() to migrate struct page information from the source
  617. * struct page to the destination struct page. If it fails to migrate the
  618. * struct page information, then it clears the MIGRATE_PFN_MIGRATE flag in the
  619. * src array.
  620. *
  621. * At this point all successfully migrated pages have an entry in the src
  622. * array with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag set and the dst
  623. * array entry with MIGRATE_PFN_VALID flag set.
  624. *
  625. * Once migrate_vma_pages() returns the caller may inspect which pages were
  626. * successfully migrated, and which were not. Successfully migrated pages will
  627. * have the MIGRATE_PFN_MIGRATE flag set for their src array entry.
  628. *
  629. * It is safe to update device page table after migrate_vma_pages() because
  630. * both destination and source page are still locked, and the mmap_lock is held
  631. * in read mode (hence no one can unmap the range being migrated).
  632. *
  633. * Once the caller is done cleaning up things and updating its page table (if it
  634. * chose to do so, this is not an obligation) it finally calls
  635. * migrate_vma_finalize() to update the CPU page table to point to new pages
  636. * for successfully migrated pages or otherwise restore the CPU page table to
  637. * point to the original source pages.
  638. */
  639. int migrate_vma_setup(struct migrate_vma *args)
  640. {
  641. long nr_pages = (args->end - args->start) >> PAGE_SHIFT;
  642. args->start &= PAGE_MASK;
  643. args->end &= PAGE_MASK;
  644. if (!args->vma || is_vm_hugetlb_page(args->vma) ||
  645. (args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma))
  646. return -EINVAL;
  647. if (nr_pages <= 0)
  648. return -EINVAL;
  649. if (args->start < args->vma->vm_start ||
  650. args->start >= args->vma->vm_end)
  651. return -EINVAL;
  652. if (args->end <= args->vma->vm_start || args->end > args->vma->vm_end)
  653. return -EINVAL;
  654. if (!args->src || !args->dst)
  655. return -EINVAL;
  656. if (args->fault_page && !is_device_private_page(args->fault_page))
  657. return -EINVAL;
  658. if (args->fault_page && !PageLocked(args->fault_page))
  659. return -EINVAL;
  660. memset(args->src, 0, sizeof(*args->src) * nr_pages);
  661. args->cpages = 0;
  662. args->npages = 0;
  663. migrate_vma_collect(args);
  664. if (args->cpages)
  665. migrate_vma_unmap(args);
  666. /*
  667. * At this point pages are locked and unmapped, and thus they have
  668. * stable content and can safely be copied to destination memory that
  669. * is allocated by the drivers.
  670. */
  671. return 0;
  672. }
  673. EXPORT_SYMBOL(migrate_vma_setup);
  674. #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
  675. /**
  676. * migrate_vma_insert_huge_pmd_page: Insert a huge folio into @migrate->vma->vm_mm
  677. * at @addr. folio is already allocated as a part of the migration process with
  678. * large page.
  679. *
  680. * @page needs to be initialized and setup after it's allocated. The code bits
  681. * here follow closely the code in __do_huge_pmd_anonymous_page(). This API does
  682. * not support THP zero pages.
  683. *
  684. * @migrate: migrate_vma arguments
  685. * @addr: address where the folio will be inserted
  686. * @page: page to be inserted at @addr
  687. * @src: src pfn which is being migrated
  688. * @pmdp: pointer to the pmd
  689. */
  690. static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
  691. unsigned long addr,
  692. struct page *page,
  693. unsigned long *src,
  694. pmd_t *pmdp)
  695. {
  696. struct vm_area_struct *vma = migrate->vma;
  697. gfp_t gfp = vma_thp_gfp_mask(vma);
  698. struct folio *folio = page_folio(page);
  699. int ret;
  700. vm_fault_t csa_ret;
  701. spinlock_t *ptl;
  702. pgtable_t pgtable;
  703. pmd_t entry;
  704. bool flush = false;
  705. unsigned long i;
  706. VM_WARN_ON_FOLIO(!folio, folio);
  707. VM_WARN_ON_ONCE(!pmd_none(*pmdp) && !is_huge_zero_pmd(*pmdp));
  708. if (!thp_vma_suitable_order(vma, addr, HPAGE_PMD_ORDER))
  709. return -EINVAL;
  710. ret = anon_vma_prepare(vma);
  711. if (ret)
  712. return ret;
  713. folio_set_order(folio, HPAGE_PMD_ORDER);
  714. folio_set_large_rmappable(folio);
  715. if (mem_cgroup_charge(folio, migrate->vma->vm_mm, gfp)) {
  716. count_vm_event(THP_FAULT_FALLBACK);
  717. count_mthp_stat(HPAGE_PMD_ORDER, MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE);
  718. ret = -ENOMEM;
  719. goto abort;
  720. }
  721. __folio_mark_uptodate(folio);
  722. pgtable = pte_alloc_one(vma->vm_mm);
  723. if (unlikely(!pgtable))
  724. goto abort;
  725. if (folio_is_device_private(folio)) {
  726. swp_entry_t swp_entry;
  727. if (vma->vm_flags & VM_WRITE)
  728. swp_entry = make_writable_device_private_entry(
  729. page_to_pfn(page));
  730. else
  731. swp_entry = make_readable_device_private_entry(
  732. page_to_pfn(page));
  733. entry = swp_entry_to_pmd(swp_entry);
  734. } else {
  735. if (folio_is_zone_device(folio) &&
  736. !folio_is_device_coherent(folio)) {
  737. goto abort;
  738. }
  739. entry = folio_mk_pmd(folio, vma->vm_page_prot);
  740. if (vma->vm_flags & VM_WRITE)
  741. entry = pmd_mkwrite(pmd_mkdirty(entry), vma);
  742. }
  743. ptl = pmd_lock(vma->vm_mm, pmdp);
  744. csa_ret = check_stable_address_space(vma->vm_mm);
  745. if (csa_ret)
  746. goto abort;
  747. /*
  748. * Check for userfaultfd but do not deliver the fault. Instead,
  749. * just back off.
  750. */
  751. if (userfaultfd_missing(vma))
  752. goto unlock_abort;
  753. if (!pmd_none(*pmdp)) {
  754. if (!is_huge_zero_pmd(*pmdp))
  755. goto unlock_abort;
  756. flush = true;
  757. } else if (!pmd_none(*pmdp))
  758. goto unlock_abort;
  759. add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
  760. folio_add_new_anon_rmap(folio, vma, addr, RMAP_EXCLUSIVE);
  761. if (!folio_is_zone_device(folio))
  762. folio_add_lru_vma(folio, vma);
  763. folio_get(folio);
  764. if (flush) {
  765. pte_free(vma->vm_mm, pgtable);
  766. flush_cache_page(vma, addr, addr + HPAGE_PMD_SIZE);
  767. pmdp_invalidate(vma, addr, pmdp);
  768. } else {
  769. pgtable_trans_huge_deposit(vma->vm_mm, pmdp, pgtable);
  770. mm_inc_nr_ptes(vma->vm_mm);
  771. }
  772. set_pmd_at(vma->vm_mm, addr, pmdp, entry);
  773. update_mmu_cache_pmd(vma, addr, pmdp);
  774. spin_unlock(ptl);
  775. count_vm_event(THP_FAULT_ALLOC);
  776. count_mthp_stat(HPAGE_PMD_ORDER, MTHP_STAT_ANON_FAULT_ALLOC);
  777. count_memcg_event_mm(vma->vm_mm, THP_FAULT_ALLOC);
  778. return 0;
  779. unlock_abort:
  780. spin_unlock(ptl);
  781. abort:
  782. for (i = 0; i < HPAGE_PMD_NR; i++)
  783. src[i] &= ~MIGRATE_PFN_MIGRATE;
  784. return 0;
  785. }
  786. static int migrate_vma_split_unmapped_folio(struct migrate_vma *migrate,
  787. unsigned long idx, unsigned long addr,
  788. struct folio *folio)
  789. {
  790. unsigned long i;
  791. unsigned long pfn;
  792. unsigned long flags;
  793. int ret = 0;
  794. folio_get(folio);
  795. split_huge_pmd_address(migrate->vma, addr, true);
  796. ret = folio_split_unmapped(folio, 0);
  797. if (ret)
  798. return ret;
  799. migrate->src[idx] &= ~MIGRATE_PFN_COMPOUND;
  800. flags = migrate->src[idx] & ((1UL << MIGRATE_PFN_SHIFT) - 1);
  801. pfn = migrate->src[idx] >> MIGRATE_PFN_SHIFT;
  802. for (i = 1; i < HPAGE_PMD_NR; i++)
  803. migrate->src[i+idx] = migrate_pfn(pfn + i) | flags;
  804. return ret;
  805. }
  806. #else /* !CONFIG_ARCH_ENABLE_THP_MIGRATION */
  807. static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
  808. unsigned long addr,
  809. struct page *page,
  810. unsigned long *src,
  811. pmd_t *pmdp)
  812. {
  813. return 0;
  814. }
  815. static int migrate_vma_split_unmapped_folio(struct migrate_vma *migrate,
  816. unsigned long idx, unsigned long addr,
  817. struct folio *folio)
  818. {
  819. return 0;
  820. }
  821. #endif
  822. static unsigned long migrate_vma_nr_pages(unsigned long *src)
  823. {
  824. unsigned long nr = 1;
  825. #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
  826. if (*src & MIGRATE_PFN_COMPOUND)
  827. nr = HPAGE_PMD_NR;
  828. #else
  829. if (*src & MIGRATE_PFN_COMPOUND)
  830. VM_WARN_ON_ONCE(true);
  831. #endif
  832. return nr;
  833. }
  834. /*
  835. * This code closely matches the code in:
  836. * __handle_mm_fault()
  837. * handle_pte_fault()
  838. * do_anonymous_page()
  839. * to map in an anonymous zero page but the struct page will be a ZONE_DEVICE
  840. * private or coherent page.
  841. */
  842. static void migrate_vma_insert_page(struct migrate_vma *migrate,
  843. unsigned long addr,
  844. unsigned long *dst,
  845. unsigned long *src)
  846. {
  847. struct page *page = migrate_pfn_to_page(*dst);
  848. struct folio *folio = page_folio(page);
  849. struct vm_area_struct *vma = migrate->vma;
  850. struct mm_struct *mm = vma->vm_mm;
  851. bool flush = false;
  852. spinlock_t *ptl;
  853. pte_t entry;
  854. pgd_t *pgdp;
  855. p4d_t *p4dp;
  856. pud_t *pudp;
  857. pmd_t *pmdp;
  858. pte_t *ptep;
  859. pte_t orig_pte;
  860. /* Only allow populating anonymous memory */
  861. if (!vma_is_anonymous(vma))
  862. goto abort;
  863. pgdp = pgd_offset(mm, addr);
  864. p4dp = p4d_alloc(mm, pgdp, addr);
  865. if (!p4dp)
  866. goto abort;
  867. pudp = pud_alloc(mm, p4dp, addr);
  868. if (!pudp)
  869. goto abort;
  870. pmdp = pmd_alloc(mm, pudp, addr);
  871. if (!pmdp)
  872. goto abort;
  873. if (thp_migration_supported() && (*dst & MIGRATE_PFN_COMPOUND)) {
  874. int ret = migrate_vma_insert_huge_pmd_page(migrate, addr, page,
  875. src, pmdp);
  876. if (ret)
  877. goto abort;
  878. return;
  879. }
  880. if (!pmd_none(*pmdp)) {
  881. if (pmd_trans_huge(*pmdp)) {
  882. if (!is_huge_zero_pmd(*pmdp))
  883. goto abort;
  884. split_huge_pmd(vma, pmdp, addr);
  885. } else if (pmd_leaf(*pmdp))
  886. goto abort;
  887. }
  888. if (pte_alloc(mm, pmdp))
  889. goto abort;
  890. if (unlikely(anon_vma_prepare(vma)))
  891. goto abort;
  892. if (mem_cgroup_charge(folio, vma->vm_mm, GFP_KERNEL))
  893. goto abort;
  894. /*
  895. * The memory barrier inside __folio_mark_uptodate makes sure that
  896. * preceding stores to the folio contents become visible before
  897. * the set_pte_at() write.
  898. */
  899. __folio_mark_uptodate(folio);
  900. if (folio_is_device_private(folio)) {
  901. swp_entry_t swp_entry;
  902. if (vma->vm_flags & VM_WRITE)
  903. swp_entry = make_writable_device_private_entry(
  904. page_to_pfn(page));
  905. else
  906. swp_entry = make_readable_device_private_entry(
  907. page_to_pfn(page));
  908. entry = swp_entry_to_pte(swp_entry);
  909. } else {
  910. if (folio_is_zone_device(folio) &&
  911. !folio_is_device_coherent(folio)) {
  912. pr_warn_once("Unsupported ZONE_DEVICE page type.\n");
  913. goto abort;
  914. }
  915. entry = mk_pte(page, vma->vm_page_prot);
  916. if (vma->vm_flags & VM_WRITE)
  917. entry = pte_mkwrite(pte_mkdirty(entry), vma);
  918. }
  919. ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
  920. if (!ptep)
  921. goto abort;
  922. orig_pte = ptep_get(ptep);
  923. if (check_stable_address_space(mm))
  924. goto unlock_abort;
  925. if (pte_present(orig_pte)) {
  926. unsigned long pfn = pte_pfn(orig_pte);
  927. if (!is_zero_pfn(pfn))
  928. goto unlock_abort;
  929. flush = true;
  930. } else if (!pte_none(orig_pte))
  931. goto unlock_abort;
  932. /*
  933. * Check for userfaultfd but do not deliver the fault. Instead,
  934. * just back off.
  935. */
  936. if (userfaultfd_missing(vma))
  937. goto unlock_abort;
  938. inc_mm_counter(mm, MM_ANONPAGES);
  939. folio_add_new_anon_rmap(folio, vma, addr, RMAP_EXCLUSIVE);
  940. if (!folio_is_zone_device(folio))
  941. folio_add_lru_vma(folio, vma);
  942. folio_get(folio);
  943. if (flush) {
  944. flush_cache_page(vma, addr, pte_pfn(orig_pte));
  945. ptep_clear_flush(vma, addr, ptep);
  946. }
  947. set_pte_at(mm, addr, ptep, entry);
  948. update_mmu_cache(vma, addr, ptep);
  949. pte_unmap_unlock(ptep, ptl);
  950. *src = MIGRATE_PFN_MIGRATE;
  951. return;
  952. unlock_abort:
  953. pte_unmap_unlock(ptep, ptl);
  954. abort:
  955. *src &= ~MIGRATE_PFN_MIGRATE;
  956. }
  957. static void __migrate_device_pages(unsigned long *src_pfns,
  958. unsigned long *dst_pfns, unsigned long npages,
  959. struct migrate_vma *migrate)
  960. {
  961. struct mmu_notifier_range range;
  962. unsigned long i, j;
  963. bool notified = false;
  964. unsigned long addr;
  965. for (i = 0; i < npages; ) {
  966. struct page *newpage = migrate_pfn_to_page(dst_pfns[i]);
  967. struct page *page = migrate_pfn_to_page(src_pfns[i]);
  968. struct address_space *mapping;
  969. struct folio *newfolio, *folio;
  970. int r, extra_cnt = 0;
  971. unsigned long nr = 1;
  972. if (!newpage) {
  973. src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
  974. goto next;
  975. }
  976. if (!page) {
  977. unsigned long addr;
  978. if (!(src_pfns[i] & MIGRATE_PFN_MIGRATE))
  979. goto next;
  980. /*
  981. * The only time there is no vma is when called from
  982. * migrate_device_coherent_folio(). However this isn't
  983. * called if the page could not be unmapped.
  984. */
  985. VM_BUG_ON(!migrate);
  986. addr = migrate->start + i*PAGE_SIZE;
  987. if (!notified) {
  988. notified = true;
  989. mmu_notifier_range_init_owner(&range,
  990. MMU_NOTIFY_MIGRATE, 0,
  991. migrate->vma->vm_mm, addr, migrate->end,
  992. migrate->pgmap_owner);
  993. mmu_notifier_invalidate_range_start(&range);
  994. }
  995. if ((src_pfns[i] & MIGRATE_PFN_COMPOUND) &&
  996. (!(dst_pfns[i] & MIGRATE_PFN_COMPOUND))) {
  997. nr = migrate_vma_nr_pages(&src_pfns[i]);
  998. src_pfns[i] &= ~MIGRATE_PFN_COMPOUND;
  999. } else {
  1000. nr = 1;
  1001. }
  1002. for (j = 0; j < nr && i + j < npages; j++) {
  1003. src_pfns[i+j] |= MIGRATE_PFN_MIGRATE;
  1004. migrate_vma_insert_page(migrate,
  1005. addr + j * PAGE_SIZE,
  1006. &dst_pfns[i+j], &src_pfns[i+j]);
  1007. }
  1008. goto next;
  1009. }
  1010. newfolio = page_folio(newpage);
  1011. folio = page_folio(page);
  1012. mapping = folio_mapping(folio);
  1013. /*
  1014. * If THP migration is enabled, check if both src and dst
  1015. * can migrate large pages
  1016. */
  1017. if (thp_migration_supported()) {
  1018. if ((src_pfns[i] & MIGRATE_PFN_MIGRATE) &&
  1019. (src_pfns[i] & MIGRATE_PFN_COMPOUND) &&
  1020. !(dst_pfns[i] & MIGRATE_PFN_COMPOUND)) {
  1021. if (!migrate) {
  1022. src_pfns[i] &= ~(MIGRATE_PFN_MIGRATE |
  1023. MIGRATE_PFN_COMPOUND);
  1024. goto next;
  1025. }
  1026. nr = 1 << folio_order(folio);
  1027. addr = migrate->start + i * PAGE_SIZE;
  1028. if (migrate_vma_split_unmapped_folio(migrate, i, addr, folio)) {
  1029. src_pfns[i] &= ~(MIGRATE_PFN_MIGRATE |
  1030. MIGRATE_PFN_COMPOUND);
  1031. goto next;
  1032. }
  1033. } else if ((src_pfns[i] & MIGRATE_PFN_MIGRATE) &&
  1034. (dst_pfns[i] & MIGRATE_PFN_COMPOUND) &&
  1035. !(src_pfns[i] & MIGRATE_PFN_COMPOUND)) {
  1036. src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
  1037. }
  1038. }
  1039. if (folio_is_device_private(newfolio) ||
  1040. folio_is_device_coherent(newfolio)) {
  1041. if (mapping) {
  1042. /*
  1043. * For now only support anonymous memory migrating to
  1044. * device private or coherent memory.
  1045. *
  1046. * Try to get rid of swap cache if possible.
  1047. */
  1048. if (!folio_test_anon(folio) ||
  1049. !folio_free_swap(folio)) {
  1050. src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
  1051. goto next;
  1052. }
  1053. }
  1054. } else if (folio_is_zone_device(newfolio)) {
  1055. /*
  1056. * Other types of ZONE_DEVICE page are not supported.
  1057. */
  1058. src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
  1059. goto next;
  1060. }
  1061. BUG_ON(folio_test_writeback(folio));
  1062. if (migrate && migrate->fault_page == page)
  1063. extra_cnt = 1;
  1064. for (j = 0; j < nr && i + j < npages; j++) {
  1065. folio = page_folio(migrate_pfn_to_page(src_pfns[i+j]));
  1066. newfolio = page_folio(migrate_pfn_to_page(dst_pfns[i+j]));
  1067. r = folio_migrate_mapping(mapping, newfolio, folio, extra_cnt);
  1068. if (r)
  1069. src_pfns[i+j] &= ~MIGRATE_PFN_MIGRATE;
  1070. else
  1071. folio_migrate_flags(newfolio, folio);
  1072. }
  1073. next:
  1074. i += nr;
  1075. }
  1076. if (notified)
  1077. mmu_notifier_invalidate_range_end(&range);
  1078. }
  1079. /**
  1080. * migrate_device_pages() - migrate meta-data from src page to dst page
  1081. * @src_pfns: src_pfns returned from migrate_device_range()
  1082. * @dst_pfns: array of pfns allocated by the driver to migrate memory to
  1083. * @npages: number of pages in the range
  1084. *
  1085. * Equivalent to migrate_vma_pages(). This is called to migrate struct page
  1086. * meta-data from source struct page to destination.
  1087. */
  1088. void migrate_device_pages(unsigned long *src_pfns, unsigned long *dst_pfns,
  1089. unsigned long npages)
  1090. {
  1091. __migrate_device_pages(src_pfns, dst_pfns, npages, NULL);
  1092. }
  1093. EXPORT_SYMBOL(migrate_device_pages);
  1094. /**
  1095. * migrate_vma_pages() - migrate meta-data from src page to dst page
  1096. * @migrate: migrate struct containing all migration information
  1097. *
  1098. * This migrates struct page meta-data from source struct page to destination
  1099. * struct page. This effectively finishes the migration from source page to the
  1100. * destination page.
  1101. */
  1102. void migrate_vma_pages(struct migrate_vma *migrate)
  1103. {
  1104. __migrate_device_pages(migrate->src, migrate->dst, migrate->npages, migrate);
  1105. }
  1106. EXPORT_SYMBOL(migrate_vma_pages);
  1107. static void __migrate_device_finalize(unsigned long *src_pfns,
  1108. unsigned long *dst_pfns,
  1109. unsigned long npages,
  1110. struct page *fault_page)
  1111. {
  1112. struct folio *fault_folio = fault_page ?
  1113. page_folio(fault_page) : NULL;
  1114. unsigned long i;
  1115. for (i = 0; i < npages; i++) {
  1116. struct folio *dst = NULL, *src = NULL;
  1117. struct page *newpage = migrate_pfn_to_page(dst_pfns[i]);
  1118. struct page *page = migrate_pfn_to_page(src_pfns[i]);
  1119. if (newpage)
  1120. dst = page_folio(newpage);
  1121. if (!page) {
  1122. if (dst) {
  1123. WARN_ON_ONCE(fault_folio == dst);
  1124. folio_unlock(dst);
  1125. folio_put(dst);
  1126. }
  1127. continue;
  1128. }
  1129. src = page_folio(page);
  1130. if (!(src_pfns[i] & MIGRATE_PFN_MIGRATE) || !dst) {
  1131. if (dst) {
  1132. WARN_ON_ONCE(fault_folio == dst);
  1133. folio_unlock(dst);
  1134. folio_put(dst);
  1135. }
  1136. dst = src;
  1137. }
  1138. if (!folio_is_zone_device(dst))
  1139. folio_add_lru(dst);
  1140. remove_migration_ptes(src, dst, 0);
  1141. if (fault_folio != src)
  1142. folio_unlock(src);
  1143. folio_put(src);
  1144. if (dst != src) {
  1145. WARN_ON_ONCE(fault_folio == dst);
  1146. folio_unlock(dst);
  1147. folio_put(dst);
  1148. }
  1149. }
  1150. }
  1151. /*
  1152. * migrate_device_finalize() - complete page migration
  1153. * @src_pfns: src_pfns returned from migrate_device_range()
  1154. * @dst_pfns: array of pfns allocated by the driver to migrate memory to
  1155. * @npages: number of pages in the range
  1156. *
  1157. * Completes migration of the page by removing special migration entries.
  1158. * Drivers must ensure copying of page data is complete and visible to the CPU
  1159. * before calling this.
  1160. */
  1161. void migrate_device_finalize(unsigned long *src_pfns,
  1162. unsigned long *dst_pfns, unsigned long npages)
  1163. {
  1164. return __migrate_device_finalize(src_pfns, dst_pfns, npages, NULL);
  1165. }
  1166. EXPORT_SYMBOL(migrate_device_finalize);
  1167. /**
  1168. * migrate_vma_finalize() - restore CPU page table entry
  1169. * @migrate: migrate struct containing all migration information
  1170. *
  1171. * This replaces the special migration pte entry with either a mapping to the
  1172. * new page if migration was successful for that page, or to the original page
  1173. * otherwise.
  1174. *
  1175. * This also unlocks the pages and puts them back on the lru, or drops the extra
  1176. * refcount, for device pages.
  1177. */
  1178. void migrate_vma_finalize(struct migrate_vma *migrate)
  1179. {
  1180. __migrate_device_finalize(migrate->src, migrate->dst, migrate->npages,
  1181. migrate->fault_page);
  1182. }
  1183. EXPORT_SYMBOL(migrate_vma_finalize);
  1184. static unsigned long migrate_device_pfn_lock(unsigned long pfn)
  1185. {
  1186. struct folio *folio;
  1187. folio = folio_get_nontail_page(pfn_to_page(pfn));
  1188. if (!folio)
  1189. return 0;
  1190. if (!folio_trylock(folio)) {
  1191. folio_put(folio);
  1192. return 0;
  1193. }
  1194. return migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
  1195. }
  1196. /**
  1197. * migrate_device_range() - migrate device private pfns to normal memory.
  1198. * @src_pfns: array large enough to hold migrating source device private pfns.
  1199. * @start: starting pfn in the range to migrate.
  1200. * @npages: number of pages to migrate.
  1201. *
  1202. * migrate_vma_setup() is similar in concept to migrate_vma_setup() except that
  1203. * instead of looking up pages based on virtual address mappings a range of
  1204. * device pfns that should be migrated to system memory is used instead.
  1205. *
  1206. * This is useful when a driver needs to free device memory but doesn't know the
  1207. * virtual mappings of every page that may be in device memory. For example this
  1208. * is often the case when a driver is being unloaded or unbound from a device.
  1209. *
  1210. * Like migrate_vma_setup() this function will take a reference and lock any
  1211. * migrating pages that aren't free before unmapping them. Drivers may then
  1212. * allocate destination pages and start copying data from the device to CPU
  1213. * memory before calling migrate_device_pages().
  1214. */
  1215. int migrate_device_range(unsigned long *src_pfns, unsigned long start,
  1216. unsigned long npages)
  1217. {
  1218. unsigned long i, j, pfn;
  1219. for (pfn = start, i = 0; i < npages; pfn++, i++) {
  1220. struct page *page = pfn_to_page(pfn);
  1221. struct folio *folio = page_folio(page);
  1222. unsigned int nr = 1;
  1223. src_pfns[i] = migrate_device_pfn_lock(pfn);
  1224. nr = folio_nr_pages(folio);
  1225. if (nr > 1) {
  1226. src_pfns[i] |= MIGRATE_PFN_COMPOUND;
  1227. for (j = 1; j < nr; j++)
  1228. src_pfns[i+j] = 0;
  1229. i += j - 1;
  1230. pfn += j - 1;
  1231. }
  1232. }
  1233. migrate_device_unmap(src_pfns, npages, NULL);
  1234. return 0;
  1235. }
  1236. EXPORT_SYMBOL(migrate_device_range);
  1237. /**
  1238. * migrate_device_pfns() - migrate device private pfns to normal memory.
  1239. * @src_pfns: pre-populated array of source device private pfns to migrate.
  1240. * @npages: number of pages to migrate.
  1241. *
  1242. * Similar to migrate_device_range() but supports non-contiguous pre-populated
  1243. * array of device pages to migrate.
  1244. */
  1245. int migrate_device_pfns(unsigned long *src_pfns, unsigned long npages)
  1246. {
  1247. unsigned long i, j;
  1248. for (i = 0; i < npages; i++) {
  1249. struct page *page = pfn_to_page(src_pfns[i]);
  1250. struct folio *folio = page_folio(page);
  1251. unsigned int nr = 1;
  1252. src_pfns[i] = migrate_device_pfn_lock(src_pfns[i]);
  1253. nr = folio_nr_pages(folio);
  1254. if (nr > 1) {
  1255. src_pfns[i] |= MIGRATE_PFN_COMPOUND;
  1256. for (j = 1; j < nr; j++)
  1257. src_pfns[i+j] = 0;
  1258. i += j - 1;
  1259. }
  1260. }
  1261. migrate_device_unmap(src_pfns, npages, NULL);
  1262. return 0;
  1263. }
  1264. EXPORT_SYMBOL(migrate_device_pfns);
  1265. /*
  1266. * Migrate a device coherent folio back to normal memory. The caller should have
  1267. * a reference on folio which will be copied to the new folio if migration is
  1268. * successful or dropped on failure.
  1269. */
  1270. int migrate_device_coherent_folio(struct folio *folio)
  1271. {
  1272. unsigned long src_pfn, dst_pfn = 0;
  1273. struct folio *dfolio;
  1274. WARN_ON_ONCE(folio_test_large(folio));
  1275. folio_lock(folio);
  1276. src_pfn = migrate_pfn(folio_pfn(folio)) | MIGRATE_PFN_MIGRATE;
  1277. /*
  1278. * We don't have a VMA and don't need to walk the page tables to find
  1279. * the source folio. So call migrate_vma_unmap() directly to unmap the
  1280. * folio as migrate_vma_setup() will fail if args.vma == NULL.
  1281. */
  1282. migrate_device_unmap(&src_pfn, 1, NULL);
  1283. if (!(src_pfn & MIGRATE_PFN_MIGRATE))
  1284. return -EBUSY;
  1285. dfolio = folio_alloc(GFP_USER | __GFP_NOWARN, 0);
  1286. if (dfolio) {
  1287. folio_lock(dfolio);
  1288. dst_pfn = migrate_pfn(folio_pfn(dfolio));
  1289. }
  1290. migrate_device_pages(&src_pfn, &dst_pfn, 1);
  1291. if (src_pfn & MIGRATE_PFN_MIGRATE)
  1292. folio_copy(dfolio, folio);
  1293. migrate_device_finalize(&src_pfn, &dst_pfn, 1);
  1294. if (src_pfn & MIGRATE_PFN_MIGRATE)
  1295. return 0;
  1296. return -EBUSY;
  1297. }