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