migrate_device.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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/swapops.h>
  17. #include <asm/tlbflush.h>
  18. #include "internal.h"
  19. static int migrate_vma_collect_skip(unsigned long start,
  20. unsigned long end,
  21. struct mm_walk *walk)
  22. {
  23. struct migrate_vma *migrate = walk->private;
  24. unsigned long addr;
  25. for (addr = start; addr < end; addr += PAGE_SIZE) {
  26. migrate->dst[migrate->npages] = 0;
  27. migrate->src[migrate->npages++] = 0;
  28. }
  29. return 0;
  30. }
  31. static int migrate_vma_collect_hole(unsigned long start,
  32. unsigned long end,
  33. __always_unused int depth,
  34. struct mm_walk *walk)
  35. {
  36. struct migrate_vma *migrate = walk->private;
  37. unsigned long addr;
  38. /* Only allow populating anonymous memory. */
  39. if (!vma_is_anonymous(walk->vma))
  40. return migrate_vma_collect_skip(start, end, walk);
  41. for (addr = start; addr < end; addr += PAGE_SIZE) {
  42. migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE;
  43. migrate->dst[migrate->npages] = 0;
  44. migrate->npages++;
  45. migrate->cpages++;
  46. }
  47. return 0;
  48. }
  49. static int migrate_vma_collect_pmd(pmd_t *pmdp,
  50. unsigned long start,
  51. unsigned long end,
  52. struct mm_walk *walk)
  53. {
  54. struct migrate_vma *migrate = walk->private;
  55. struct vm_area_struct *vma = walk->vma;
  56. struct mm_struct *mm = vma->vm_mm;
  57. unsigned long addr = start, unmapped = 0;
  58. spinlock_t *ptl;
  59. pte_t *ptep;
  60. again:
  61. if (pmd_none(*pmdp))
  62. return migrate_vma_collect_hole(start, end, -1, walk);
  63. if (pmd_trans_huge(*pmdp)) {
  64. struct page *page;
  65. ptl = pmd_lock(mm, pmdp);
  66. if (unlikely(!pmd_trans_huge(*pmdp))) {
  67. spin_unlock(ptl);
  68. goto again;
  69. }
  70. page = pmd_page(*pmdp);
  71. if (is_huge_zero_page(page)) {
  72. spin_unlock(ptl);
  73. split_huge_pmd(vma, pmdp, addr);
  74. if (pmd_trans_unstable(pmdp))
  75. return migrate_vma_collect_skip(start, end,
  76. walk);
  77. } else {
  78. int ret;
  79. get_page(page);
  80. spin_unlock(ptl);
  81. if (unlikely(!trylock_page(page)))
  82. return migrate_vma_collect_skip(start, end,
  83. walk);
  84. ret = split_huge_page(page);
  85. unlock_page(page);
  86. put_page(page);
  87. if (ret)
  88. return migrate_vma_collect_skip(start, end,
  89. walk);
  90. if (pmd_none(*pmdp))
  91. return migrate_vma_collect_hole(start, end, -1,
  92. walk);
  93. }
  94. }
  95. if (unlikely(pmd_bad(*pmdp)))
  96. return migrate_vma_collect_skip(start, end, walk);
  97. ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
  98. arch_enter_lazy_mmu_mode();
  99. for (; addr < end; addr += PAGE_SIZE, ptep++) {
  100. unsigned long mpfn = 0, pfn;
  101. struct page *page;
  102. swp_entry_t entry;
  103. pte_t pte;
  104. pte = *ptep;
  105. if (pte_none(pte)) {
  106. if (vma_is_anonymous(vma)) {
  107. mpfn = MIGRATE_PFN_MIGRATE;
  108. migrate->cpages++;
  109. }
  110. goto next;
  111. }
  112. if (!pte_present(pte)) {
  113. /*
  114. * Only care about unaddressable device page special
  115. * page table entry. Other special swap entries are not
  116. * migratable, and we ignore regular swapped page.
  117. */
  118. entry = pte_to_swp_entry(pte);
  119. if (!is_device_private_entry(entry))
  120. goto next;
  121. page = pfn_swap_entry_to_page(entry);
  122. if (!(migrate->flags &
  123. MIGRATE_VMA_SELECT_DEVICE_PRIVATE) ||
  124. page->pgmap->owner != migrate->pgmap_owner)
  125. goto next;
  126. mpfn = migrate_pfn(page_to_pfn(page)) |
  127. MIGRATE_PFN_MIGRATE;
  128. if (is_writable_device_private_entry(entry))
  129. mpfn |= MIGRATE_PFN_WRITE;
  130. } else {
  131. pfn = pte_pfn(pte);
  132. if (is_zero_pfn(pfn) &&
  133. (migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) {
  134. mpfn = MIGRATE_PFN_MIGRATE;
  135. migrate->cpages++;
  136. goto next;
  137. }
  138. page = vm_normal_page(migrate->vma, addr, pte);
  139. if (page && !is_zone_device_page(page) &&
  140. !(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM))
  141. goto next;
  142. else if (page && is_device_coherent_page(page) &&
  143. (!(migrate->flags & MIGRATE_VMA_SELECT_DEVICE_COHERENT) ||
  144. page->pgmap->owner != migrate->pgmap_owner))
  145. goto next;
  146. mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
  147. mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0;
  148. }
  149. /* FIXME support THP */
  150. if (!page || !page->mapping || PageTransCompound(page)) {
  151. mpfn = 0;
  152. goto next;
  153. }
  154. /*
  155. * By getting a reference on the page we pin it and that blocks
  156. * any kind of migration. Side effect is that it "freezes" the
  157. * pte.
  158. *
  159. * We drop this reference after isolating the page from the lru
  160. * for non device page (device page are not on the lru and thus
  161. * can't be dropped from it).
  162. */
  163. get_page(page);
  164. /*
  165. * We rely on trylock_page() to avoid deadlock between
  166. * concurrent migrations where each is waiting on the others
  167. * page lock. If we can't immediately lock the page we fail this
  168. * migration as it is only best effort anyway.
  169. *
  170. * If we can lock the page it's safe to set up a migration entry
  171. * now. In the common case where the page is mapped once in a
  172. * single process setting up the migration entry now is an
  173. * optimisation to avoid walking the rmap later with
  174. * try_to_migrate().
  175. */
  176. if (trylock_page(page)) {
  177. bool anon_exclusive;
  178. pte_t swp_pte;
  179. flush_cache_page(vma, addr, pte_pfn(*ptep));
  180. anon_exclusive = PageAnon(page) && PageAnonExclusive(page);
  181. if (anon_exclusive) {
  182. pte = ptep_clear_flush(vma, addr, ptep);
  183. if (page_try_share_anon_rmap(page)) {
  184. set_pte_at(mm, addr, ptep, pte);
  185. unlock_page(page);
  186. put_page(page);
  187. mpfn = 0;
  188. goto next;
  189. }
  190. } else {
  191. pte = ptep_get_and_clear(mm, addr, ptep);
  192. }
  193. migrate->cpages++;
  194. /* Set the dirty flag on the folio now the pte is gone. */
  195. if (pte_dirty(pte))
  196. folio_mark_dirty(page_folio(page));
  197. /* Setup special migration page table entry */
  198. if (mpfn & MIGRATE_PFN_WRITE)
  199. entry = make_writable_migration_entry(
  200. page_to_pfn(page));
  201. else if (anon_exclusive)
  202. entry = make_readable_exclusive_migration_entry(
  203. page_to_pfn(page));
  204. else
  205. entry = make_readable_migration_entry(
  206. page_to_pfn(page));
  207. if (pte_present(pte)) {
  208. if (pte_young(pte))
  209. entry = make_migration_entry_young(entry);
  210. if (pte_dirty(pte))
  211. entry = make_migration_entry_dirty(entry);
  212. }
  213. swp_pte = swp_entry_to_pte(entry);
  214. if (pte_present(pte)) {
  215. if (pte_soft_dirty(pte))
  216. swp_pte = pte_swp_mksoft_dirty(swp_pte);
  217. if (pte_uffd_wp(pte))
  218. swp_pte = pte_swp_mkuffd_wp(swp_pte);
  219. } else {
  220. if (pte_swp_soft_dirty(pte))
  221. swp_pte = pte_swp_mksoft_dirty(swp_pte);
  222. if (pte_swp_uffd_wp(pte))
  223. swp_pte = pte_swp_mkuffd_wp(swp_pte);
  224. }
  225. set_pte_at(mm, addr, ptep, swp_pte);
  226. /*
  227. * This is like regular unmap: we remove the rmap and
  228. * drop page refcount. Page won't be freed, as we took
  229. * a reference just above.
  230. */
  231. page_remove_rmap(page, vma, false);
  232. put_page(page);
  233. if (pte_present(pte))
  234. unmapped++;
  235. } else {
  236. put_page(page);
  237. mpfn = 0;
  238. }
  239. next:
  240. migrate->dst[migrate->npages] = 0;
  241. migrate->src[migrate->npages++] = mpfn;
  242. }
  243. /* Only flush the TLB if we actually modified any entries */
  244. if (unmapped)
  245. flush_tlb_range(walk->vma, start, end);
  246. arch_leave_lazy_mmu_mode();
  247. pte_unmap_unlock(ptep - 1, ptl);
  248. return 0;
  249. }
  250. static const struct mm_walk_ops migrate_vma_walk_ops = {
  251. .pmd_entry = migrate_vma_collect_pmd,
  252. .pte_hole = migrate_vma_collect_hole,
  253. .walk_lock = PGWALK_RDLOCK,
  254. };
  255. /*
  256. * migrate_vma_collect() - collect pages over a range of virtual addresses
  257. * @migrate: migrate struct containing all migration information
  258. *
  259. * This will walk the CPU page table. For each virtual address backed by a
  260. * valid page, it updates the src array and takes a reference on the page, in
  261. * order to pin the page until we lock it and unmap it.
  262. */
  263. static void migrate_vma_collect(struct migrate_vma *migrate)
  264. {
  265. struct mmu_notifier_range range;
  266. /*
  267. * Note that the pgmap_owner is passed to the mmu notifier callback so
  268. * that the registered device driver can skip invalidating device
  269. * private page mappings that won't be migrated.
  270. */
  271. mmu_notifier_range_init_owner(&range, MMU_NOTIFY_MIGRATE, 0,
  272. migrate->vma, migrate->vma->vm_mm, migrate->start, migrate->end,
  273. migrate->pgmap_owner);
  274. mmu_notifier_invalidate_range_start(&range);
  275. walk_page_range(migrate->vma->vm_mm, migrate->start, migrate->end,
  276. &migrate_vma_walk_ops, migrate);
  277. mmu_notifier_invalidate_range_end(&range);
  278. migrate->end = migrate->start + (migrate->npages << PAGE_SHIFT);
  279. }
  280. /*
  281. * migrate_vma_check_page() - check if page is pinned or not
  282. * @page: struct page to check
  283. *
  284. * Pinned pages cannot be migrated. This is the same test as in
  285. * folio_migrate_mapping(), except that here we allow migration of a
  286. * ZONE_DEVICE page.
  287. */
  288. static bool migrate_vma_check_page(struct page *page, struct page *fault_page)
  289. {
  290. /*
  291. * One extra ref because caller holds an extra reference, either from
  292. * isolate_lru_page() for a regular page, or migrate_vma_collect() for
  293. * a device page.
  294. */
  295. int extra = 1 + (page == fault_page);
  296. /*
  297. * FIXME support THP (transparent huge page), it is bit more complex to
  298. * check them than regular pages, because they can be mapped with a pmd
  299. * or with a pte (split pte mapping).
  300. */
  301. if (PageCompound(page))
  302. return false;
  303. /* Page from ZONE_DEVICE have one extra reference */
  304. if (is_zone_device_page(page))
  305. extra++;
  306. /* For file back page */
  307. if (page_mapping(page))
  308. extra += 1 + page_has_private(page);
  309. if ((page_count(page) - extra) > page_mapcount(page))
  310. return false;
  311. return true;
  312. }
  313. /*
  314. * Unmaps pages for migration. Returns number of source pfns marked as
  315. * migrating.
  316. */
  317. static unsigned long migrate_device_unmap(unsigned long *src_pfns,
  318. unsigned long npages,
  319. struct page *fault_page)
  320. {
  321. unsigned long i, restore = 0;
  322. bool allow_drain = true;
  323. unsigned long unmapped = 0;
  324. lru_add_drain();
  325. for (i = 0; i < npages; i++) {
  326. struct page *page = migrate_pfn_to_page(src_pfns[i]);
  327. struct folio *folio;
  328. if (!page) {
  329. if (src_pfns[i] & MIGRATE_PFN_MIGRATE)
  330. unmapped++;
  331. continue;
  332. }
  333. /* ZONE_DEVICE pages are not on LRU */
  334. if (!is_zone_device_page(page)) {
  335. if (!PageLRU(page) && allow_drain) {
  336. /* Drain CPU's pagevec */
  337. lru_add_drain_all();
  338. allow_drain = false;
  339. }
  340. if (isolate_lru_page(page)) {
  341. src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
  342. restore++;
  343. continue;
  344. }
  345. /* Drop the reference we took in collect */
  346. put_page(page);
  347. }
  348. folio = page_folio(page);
  349. if (folio_mapped(folio))
  350. try_to_migrate(folio, 0);
  351. if (page_mapped(page) ||
  352. !migrate_vma_check_page(page, fault_page)) {
  353. if (!is_zone_device_page(page)) {
  354. get_page(page);
  355. putback_lru_page(page);
  356. }
  357. src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
  358. restore++;
  359. continue;
  360. }
  361. unmapped++;
  362. }
  363. for (i = 0; i < npages && restore; i++) {
  364. struct page *page = migrate_pfn_to_page(src_pfns[i]);
  365. struct folio *folio;
  366. if (!page || (src_pfns[i] & MIGRATE_PFN_MIGRATE))
  367. continue;
  368. folio = page_folio(page);
  369. remove_migration_ptes(folio, folio, false);
  370. src_pfns[i] = 0;
  371. folio_unlock(folio);
  372. folio_put(folio);
  373. restore--;
  374. }
  375. return unmapped;
  376. }
  377. /*
  378. * migrate_vma_unmap() - replace page mapping with special migration pte entry
  379. * @migrate: migrate struct containing all migration information
  380. *
  381. * Isolate pages from the LRU and replace mappings (CPU page table pte) with a
  382. * special migration pte entry and check if it has been pinned. Pinned pages are
  383. * restored because we cannot migrate them.
  384. *
  385. * This is the last step before we call the device driver callback to allocate
  386. * destination memory and copy contents of original page over to new page.
  387. */
  388. static void migrate_vma_unmap(struct migrate_vma *migrate)
  389. {
  390. migrate->cpages = migrate_device_unmap(migrate->src, migrate->npages,
  391. migrate->fault_page);
  392. }
  393. /**
  394. * migrate_vma_setup() - prepare to migrate a range of memory
  395. * @args: contains the vma, start, and pfns arrays for the migration
  396. *
  397. * Returns: negative errno on failures, 0 when 0 or more pages were migrated
  398. * without an error.
  399. *
  400. * Prepare to migrate a range of memory virtual address range by collecting all
  401. * the pages backing each virtual address in the range, saving them inside the
  402. * src array. Then lock those pages and unmap them. Once the pages are locked
  403. * and unmapped, check whether each page is pinned or not. Pages that aren't
  404. * pinned have the MIGRATE_PFN_MIGRATE flag set (by this function) in the
  405. * corresponding src array entry. Then restores any pages that are pinned, by
  406. * remapping and unlocking those pages.
  407. *
  408. * The caller should then allocate destination memory and copy source memory to
  409. * it for all those entries (ie with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE
  410. * flag set). Once these are allocated and copied, the caller must update each
  411. * corresponding entry in the dst array with the pfn value of the destination
  412. * page and with MIGRATE_PFN_VALID. Destination pages must be locked via
  413. * lock_page().
  414. *
  415. * Note that the caller does not have to migrate all the pages that are marked
  416. * with MIGRATE_PFN_MIGRATE flag in src array unless this is a migration from
  417. * device memory to system memory. If the caller cannot migrate a device page
  418. * back to system memory, then it must return VM_FAULT_SIGBUS, which has severe
  419. * consequences for the userspace process, so it must be avoided if at all
  420. * possible.
  421. *
  422. * For empty entries inside CPU page table (pte_none() or pmd_none() is true) we
  423. * do set MIGRATE_PFN_MIGRATE flag inside the corresponding source array thus
  424. * allowing the caller to allocate device memory for those unbacked virtual
  425. * addresses. For this the caller simply has to allocate device memory and
  426. * properly set the destination entry like for regular migration. Note that
  427. * this can still fail, and thus inside the device driver you must check if the
  428. * migration was successful for those entries after calling migrate_vma_pages(),
  429. * just like for regular migration.
  430. *
  431. * After that, the callers must call migrate_vma_pages() to go over each entry
  432. * in the src array that has the MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag
  433. * set. If the corresponding entry in dst array has MIGRATE_PFN_VALID flag set,
  434. * then migrate_vma_pages() to migrate struct page information from the source
  435. * struct page to the destination struct page. If it fails to migrate the
  436. * struct page information, then it clears the MIGRATE_PFN_MIGRATE flag in the
  437. * src array.
  438. *
  439. * At this point all successfully migrated pages have an entry in the src
  440. * array with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag set and the dst
  441. * array entry with MIGRATE_PFN_VALID flag set.
  442. *
  443. * Once migrate_vma_pages() returns the caller may inspect which pages were
  444. * successfully migrated, and which were not. Successfully migrated pages will
  445. * have the MIGRATE_PFN_MIGRATE flag set for their src array entry.
  446. *
  447. * It is safe to update device page table after migrate_vma_pages() because
  448. * both destination and source page are still locked, and the mmap_lock is held
  449. * in read mode (hence no one can unmap the range being migrated).
  450. *
  451. * Once the caller is done cleaning up things and updating its page table (if it
  452. * chose to do so, this is not an obligation) it finally calls
  453. * migrate_vma_finalize() to update the CPU page table to point to new pages
  454. * for successfully migrated pages or otherwise restore the CPU page table to
  455. * point to the original source pages.
  456. */
  457. int migrate_vma_setup(struct migrate_vma *args)
  458. {
  459. long nr_pages = (args->end - args->start) >> PAGE_SHIFT;
  460. args->start &= PAGE_MASK;
  461. args->end &= PAGE_MASK;
  462. if (!args->vma || is_vm_hugetlb_page(args->vma) ||
  463. (args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma))
  464. return -EINVAL;
  465. if (nr_pages <= 0)
  466. return -EINVAL;
  467. if (args->start < args->vma->vm_start ||
  468. args->start >= args->vma->vm_end)
  469. return -EINVAL;
  470. if (args->end <= args->vma->vm_start || args->end > args->vma->vm_end)
  471. return -EINVAL;
  472. if (!args->src || !args->dst)
  473. return -EINVAL;
  474. if (args->fault_page && !is_device_private_page(args->fault_page))
  475. return -EINVAL;
  476. memset(args->src, 0, sizeof(*args->src) * nr_pages);
  477. args->cpages = 0;
  478. args->npages = 0;
  479. migrate_vma_collect(args);
  480. if (args->cpages)
  481. migrate_vma_unmap(args);
  482. /*
  483. * At this point pages are locked and unmapped, and thus they have
  484. * stable content and can safely be copied to destination memory that
  485. * is allocated by the drivers.
  486. */
  487. return 0;
  488. }
  489. EXPORT_SYMBOL(migrate_vma_setup);
  490. /*
  491. * This code closely matches the code in:
  492. * __handle_mm_fault()
  493. * handle_pte_fault()
  494. * do_anonymous_page()
  495. * to map in an anonymous zero page but the struct page will be a ZONE_DEVICE
  496. * private or coherent page.
  497. */
  498. static void migrate_vma_insert_page(struct migrate_vma *migrate,
  499. unsigned long addr,
  500. struct page *page,
  501. unsigned long *src)
  502. {
  503. struct vm_area_struct *vma = migrate->vma;
  504. struct mm_struct *mm = vma->vm_mm;
  505. bool flush = false;
  506. spinlock_t *ptl;
  507. pte_t entry;
  508. pgd_t *pgdp;
  509. p4d_t *p4dp;
  510. pud_t *pudp;
  511. pmd_t *pmdp;
  512. pte_t *ptep;
  513. /* Only allow populating anonymous memory */
  514. if (!vma_is_anonymous(vma))
  515. goto abort;
  516. pgdp = pgd_offset(mm, addr);
  517. p4dp = p4d_alloc(mm, pgdp, addr);
  518. if (!p4dp)
  519. goto abort;
  520. pudp = pud_alloc(mm, p4dp, addr);
  521. if (!pudp)
  522. goto abort;
  523. pmdp = pmd_alloc(mm, pudp, addr);
  524. if (!pmdp)
  525. goto abort;
  526. if (pmd_trans_huge(*pmdp) || pmd_devmap(*pmdp))
  527. goto abort;
  528. /*
  529. * Use pte_alloc() instead of pte_alloc_map(). We can't run
  530. * pte_offset_map() on pmds where a huge pmd might be created
  531. * from a different thread.
  532. *
  533. * pte_alloc_map() is safe to use under mmap_write_lock(mm) or when
  534. * parallel threads are excluded by other means.
  535. *
  536. * Here we only have mmap_read_lock(mm).
  537. */
  538. if (pte_alloc(mm, pmdp))
  539. goto abort;
  540. /* See the comment in pte_alloc_one_map() */
  541. if (unlikely(pmd_trans_unstable(pmdp)))
  542. goto abort;
  543. if (unlikely(anon_vma_prepare(vma)))
  544. goto abort;
  545. if (mem_cgroup_charge(page_folio(page), vma->vm_mm, GFP_KERNEL))
  546. goto abort;
  547. /*
  548. * The memory barrier inside __SetPageUptodate makes sure that
  549. * preceding stores to the page contents become visible before
  550. * the set_pte_at() write.
  551. */
  552. __SetPageUptodate(page);
  553. if (is_device_private_page(page)) {
  554. swp_entry_t swp_entry;
  555. if (vma->vm_flags & VM_WRITE)
  556. swp_entry = make_writable_device_private_entry(
  557. page_to_pfn(page));
  558. else
  559. swp_entry = make_readable_device_private_entry(
  560. page_to_pfn(page));
  561. entry = swp_entry_to_pte(swp_entry);
  562. } else {
  563. if (is_zone_device_page(page) &&
  564. !is_device_coherent_page(page)) {
  565. pr_warn_once("Unsupported ZONE_DEVICE page type.\n");
  566. goto abort;
  567. }
  568. entry = mk_pte(page, vma->vm_page_prot);
  569. if (vma->vm_flags & VM_WRITE)
  570. entry = pte_mkwrite(pte_mkdirty(entry));
  571. }
  572. ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
  573. if (check_stable_address_space(mm))
  574. goto unlock_abort;
  575. if (pte_present(*ptep)) {
  576. unsigned long pfn = pte_pfn(*ptep);
  577. if (!is_zero_pfn(pfn))
  578. goto unlock_abort;
  579. flush = true;
  580. } else if (!pte_none(*ptep))
  581. goto unlock_abort;
  582. /*
  583. * Check for userfaultfd but do not deliver the fault. Instead,
  584. * just back off.
  585. */
  586. if (userfaultfd_missing(vma))
  587. goto unlock_abort;
  588. inc_mm_counter(mm, MM_ANONPAGES);
  589. page_add_new_anon_rmap(page, vma, addr);
  590. if (!is_zone_device_page(page))
  591. lru_cache_add_inactive_or_unevictable(page, vma);
  592. get_page(page);
  593. if (flush) {
  594. flush_cache_page(vma, addr, pte_pfn(*ptep));
  595. ptep_clear_flush_notify(vma, addr, ptep);
  596. set_pte_at_notify(mm, addr, ptep, entry);
  597. update_mmu_cache(vma, addr, ptep);
  598. } else {
  599. /* No need to invalidate - it was non-present before */
  600. set_pte_at(mm, addr, ptep, entry);
  601. update_mmu_cache(vma, addr, ptep);
  602. }
  603. pte_unmap_unlock(ptep, ptl);
  604. *src = MIGRATE_PFN_MIGRATE;
  605. return;
  606. unlock_abort:
  607. pte_unmap_unlock(ptep, ptl);
  608. abort:
  609. *src &= ~MIGRATE_PFN_MIGRATE;
  610. }
  611. static void __migrate_device_pages(unsigned long *src_pfns,
  612. unsigned long *dst_pfns, unsigned long npages,
  613. struct migrate_vma *migrate)
  614. {
  615. struct mmu_notifier_range range;
  616. unsigned long i;
  617. bool notified = false;
  618. for (i = 0; i < npages; i++) {
  619. struct page *newpage = migrate_pfn_to_page(dst_pfns[i]);
  620. struct page *page = migrate_pfn_to_page(src_pfns[i]);
  621. struct address_space *mapping;
  622. int r;
  623. if (!newpage) {
  624. src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
  625. continue;
  626. }
  627. if (!page) {
  628. unsigned long addr;
  629. if (!(src_pfns[i] & MIGRATE_PFN_MIGRATE))
  630. continue;
  631. /*
  632. * The only time there is no vma is when called from
  633. * migrate_device_coherent_page(). However this isn't
  634. * called if the page could not be unmapped.
  635. */
  636. VM_BUG_ON(!migrate);
  637. addr = migrate->start + i*PAGE_SIZE;
  638. if (!notified) {
  639. notified = true;
  640. mmu_notifier_range_init_owner(&range,
  641. MMU_NOTIFY_MIGRATE, 0, migrate->vma,
  642. migrate->vma->vm_mm, addr, migrate->end,
  643. migrate->pgmap_owner);
  644. mmu_notifier_invalidate_range_start(&range);
  645. }
  646. migrate_vma_insert_page(migrate, addr, newpage,
  647. &src_pfns[i]);
  648. continue;
  649. }
  650. mapping = page_mapping(page);
  651. if (is_device_private_page(newpage) ||
  652. is_device_coherent_page(newpage)) {
  653. /*
  654. * For now only support anonymous memory migrating to
  655. * device private or coherent memory.
  656. */
  657. if (mapping) {
  658. src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
  659. continue;
  660. }
  661. } else if (is_zone_device_page(newpage)) {
  662. /*
  663. * Other types of ZONE_DEVICE page are not supported.
  664. */
  665. src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
  666. continue;
  667. }
  668. if (migrate && migrate->fault_page == page)
  669. r = migrate_folio_extra(mapping, page_folio(newpage),
  670. page_folio(page),
  671. MIGRATE_SYNC_NO_COPY, 1);
  672. else
  673. r = migrate_folio(mapping, page_folio(newpage),
  674. page_folio(page), MIGRATE_SYNC_NO_COPY);
  675. if (r != MIGRATEPAGE_SUCCESS)
  676. src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
  677. }
  678. /*
  679. * No need to double call mmu_notifier->invalidate_range() callback as
  680. * the above ptep_clear_flush_notify() inside migrate_vma_insert_page()
  681. * did already call it.
  682. */
  683. if (notified)
  684. mmu_notifier_invalidate_range_only_end(&range);
  685. }
  686. /**
  687. * migrate_device_pages() - migrate meta-data from src page to dst page
  688. * @src_pfns: src_pfns returned from migrate_device_range()
  689. * @dst_pfns: array of pfns allocated by the driver to migrate memory to
  690. * @npages: number of pages in the range
  691. *
  692. * Equivalent to migrate_vma_pages(). This is called to migrate struct page
  693. * meta-data from source struct page to destination.
  694. */
  695. void migrate_device_pages(unsigned long *src_pfns, unsigned long *dst_pfns,
  696. unsigned long npages)
  697. {
  698. __migrate_device_pages(src_pfns, dst_pfns, npages, NULL);
  699. }
  700. EXPORT_SYMBOL(migrate_device_pages);
  701. /**
  702. * migrate_vma_pages() - migrate meta-data from src page to dst page
  703. * @migrate: migrate struct containing all migration information
  704. *
  705. * This migrates struct page meta-data from source struct page to destination
  706. * struct page. This effectively finishes the migration from source page to the
  707. * destination page.
  708. */
  709. void migrate_vma_pages(struct migrate_vma *migrate)
  710. {
  711. __migrate_device_pages(migrate->src, migrate->dst, migrate->npages, migrate);
  712. }
  713. EXPORT_SYMBOL(migrate_vma_pages);
  714. /*
  715. * migrate_device_finalize() - complete page migration
  716. * @src_pfns: src_pfns returned from migrate_device_range()
  717. * @dst_pfns: array of pfns allocated by the driver to migrate memory to
  718. * @npages: number of pages in the range
  719. *
  720. * Completes migration of the page by removing special migration entries.
  721. * Drivers must ensure copying of page data is complete and visible to the CPU
  722. * before calling this.
  723. */
  724. void migrate_device_finalize(unsigned long *src_pfns,
  725. unsigned long *dst_pfns, unsigned long npages)
  726. {
  727. unsigned long i;
  728. for (i = 0; i < npages; i++) {
  729. struct folio *dst, *src;
  730. struct page *newpage = migrate_pfn_to_page(dst_pfns[i]);
  731. struct page *page = migrate_pfn_to_page(src_pfns[i]);
  732. if (!page) {
  733. if (newpage) {
  734. unlock_page(newpage);
  735. put_page(newpage);
  736. }
  737. continue;
  738. }
  739. if (!(src_pfns[i] & MIGRATE_PFN_MIGRATE) || !newpage) {
  740. if (newpage) {
  741. unlock_page(newpage);
  742. put_page(newpage);
  743. }
  744. newpage = page;
  745. }
  746. src = page_folio(page);
  747. dst = page_folio(newpage);
  748. remove_migration_ptes(src, dst, false);
  749. folio_unlock(src);
  750. if (is_zone_device_page(page))
  751. put_page(page);
  752. else
  753. putback_lru_page(page);
  754. if (newpage != page) {
  755. unlock_page(newpage);
  756. if (is_zone_device_page(newpage))
  757. put_page(newpage);
  758. else
  759. putback_lru_page(newpage);
  760. }
  761. }
  762. }
  763. EXPORT_SYMBOL(migrate_device_finalize);
  764. /**
  765. * migrate_vma_finalize() - restore CPU page table entry
  766. * @migrate: migrate struct containing all migration information
  767. *
  768. * This replaces the special migration pte entry with either a mapping to the
  769. * new page if migration was successful for that page, or to the original page
  770. * otherwise.
  771. *
  772. * This also unlocks the pages and puts them back on the lru, or drops the extra
  773. * refcount, for device pages.
  774. */
  775. void migrate_vma_finalize(struct migrate_vma *migrate)
  776. {
  777. migrate_device_finalize(migrate->src, migrate->dst, migrate->npages);
  778. }
  779. EXPORT_SYMBOL(migrate_vma_finalize);
  780. /**
  781. * migrate_device_range() - migrate device private pfns to normal memory.
  782. * @src_pfns: array large enough to hold migrating source device private pfns.
  783. * @start: starting pfn in the range to migrate.
  784. * @npages: number of pages to migrate.
  785. *
  786. * migrate_vma_setup() is similar in concept to migrate_vma_setup() except that
  787. * instead of looking up pages based on virtual address mappings a range of
  788. * device pfns that should be migrated to system memory is used instead.
  789. *
  790. * This is useful when a driver needs to free device memory but doesn't know the
  791. * virtual mappings of every page that may be in device memory. For example this
  792. * is often the case when a driver is being unloaded or unbound from a device.
  793. *
  794. * Like migrate_vma_setup() this function will take a reference and lock any
  795. * migrating pages that aren't free before unmapping them. Drivers may then
  796. * allocate destination pages and start copying data from the device to CPU
  797. * memory before calling migrate_device_pages().
  798. */
  799. int migrate_device_range(unsigned long *src_pfns, unsigned long start,
  800. unsigned long npages)
  801. {
  802. unsigned long i, pfn;
  803. for (pfn = start, i = 0; i < npages; pfn++, i++) {
  804. struct page *page = pfn_to_page(pfn);
  805. if (!get_page_unless_zero(page)) {
  806. src_pfns[i] = 0;
  807. continue;
  808. }
  809. if (!trylock_page(page)) {
  810. src_pfns[i] = 0;
  811. put_page(page);
  812. continue;
  813. }
  814. src_pfns[i] = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
  815. }
  816. migrate_device_unmap(src_pfns, npages, NULL);
  817. return 0;
  818. }
  819. EXPORT_SYMBOL(migrate_device_range);
  820. /*
  821. * Migrate a device coherent page back to normal memory. The caller should have
  822. * a reference on page which will be copied to the new page if migration is
  823. * successful or dropped on failure.
  824. */
  825. int migrate_device_coherent_page(struct page *page)
  826. {
  827. unsigned long src_pfn, dst_pfn = 0;
  828. struct page *dpage;
  829. WARN_ON_ONCE(PageCompound(page));
  830. lock_page(page);
  831. src_pfn = migrate_pfn(page_to_pfn(page)) | MIGRATE_PFN_MIGRATE;
  832. /*
  833. * We don't have a VMA and don't need to walk the page tables to find
  834. * the source page. So call migrate_vma_unmap() directly to unmap the
  835. * page as migrate_vma_setup() will fail if args.vma == NULL.
  836. */
  837. migrate_device_unmap(&src_pfn, 1, NULL);
  838. if (!(src_pfn & MIGRATE_PFN_MIGRATE))
  839. return -EBUSY;
  840. dpage = alloc_page(GFP_USER | __GFP_NOWARN);
  841. if (dpage) {
  842. lock_page(dpage);
  843. dst_pfn = migrate_pfn(page_to_pfn(dpage));
  844. }
  845. migrate_device_pages(&src_pfn, &dst_pfn, 1);
  846. if (src_pfn & MIGRATE_PFN_MIGRATE)
  847. copy_highpage(dpage, page);
  848. migrate_device_finalize(&src_pfn, &dst_pfn, 1);
  849. if (src_pfn & MIGRATE_PFN_MIGRATE)
  850. return 0;
  851. return -EBUSY;
  852. }