mm/rmap: batch the unmap of large folios during migration
Subsystems (2)
v3 cover mm/rmap: batch the unmap of large folios during migration
Migrating a PTE-mapped large folio replaces its present PTEs with migration entries one page at a time: try_to_migrate_one() repeats page_vma_mapped_walk(), the PTE clear and flush, the migration entry setup and the rmap/refcount update for every subpage (256 iterations for a 1M folio).
This series lets try_to_migrate_one() convert a batch of consecutive PTEs that map the same large folio in one go, mirroring what try_to_unmap_one() already does, with some refactoring on the way.
Besides the walk overhead, batching avoids a spurious deferred split:
unmapping an anonymous mTHP one PTE at a time makes it look partially mapped partway through, so it is queued for deferred split even though it is about to be fully unmapped. Joanne's underused mTHP shrinker series [1] sends every underused mTHP split through this path, which makes this a blocker there [2].
Performance:
AMD EPYC Zen5, 2-socket system (1 node per socket), mitigations=off. move_pages() migrating 1GB between the two nodes, mTHP of the given size; base is mm-core/for-next + Dev's v5 [3]. Throughput in GB/s, mean of 40 runs (2 boots, both directions):
folio base patched gain 4K 3.39±0.02 3.39±0.02 +0.1% 16K 4.65±0.02 5.12±0.04 +10.3% 64K 5.51±0.04 6.46±0.05 +17.2% 256K 5.78±0.07 7.47±0.01 +29.2% 1M 5.94±0.02 7.64±0.02 +28.6% 2M 9.09±0.03 9.08±0.03 -0.2% (PMD-mapped)
Time spent in try_to_migrate() during that move (ftrace function profiler, mean of 6 runs):
folio base patched delta 16K 43.7 ms 28.2 ms -35% 64K 45.6 ms 20.0 ms -56% 256K 44.2 ms 8.1 ms -82% 1M 41.8 ms 7.1 ms -83%
On AMD EPYC Zen3 (refer V2 cover), the unmap-side batching gave around +25.0% at 64K, +38.4% at 256K and +39.5% at 1M.
The restore-side batching from v2 is split out and will be posted separately.
Applies on mm-core/for-next + Dev's "Optimize anonymous swapbacked large folio unmapping" v5 [3].
[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/linux-mm/[email protected]/
---
Changes in v3:
- try_to_migrate_one(): memory_failure() can poison a tail page without holding the folio lock, so check PageHWPoison for every subpage in the batch. (Lance, Miaohe, Ying)
- Split the series: this posting is only the unmap side changes. Restore
side batching will be posted separately.
- Rebase onto mm-core/for-next and Dev's series v5; adapt to finish_folio_unmap_batch() and page_anon_exclusive_batch() changes.
- Use vma_test_any_mask() in try_to_migrate_hugetlb_one()
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Rebase onto mm-new, drop the borrowed folio_try_share_anon_rmap_ptes() patch and add it as dependency on Dev's series and adapt to recent migrate/rmap changes.
- Switch to recently added batched helpers - finish_folio_unmap, set_softleaf_ptes, and page_anon_exclusive_batch.
- Rename migration_softleaf_entry_to_pte() to migration_entry_to_pte(). (David)
- Split PTE construction from hugetlb handling into separate patch. (David) Mirror the split in mm/rmap.c patch as well.
- Rename make_migration_swp_pte() to make_migration_pte().
- Early exit the single-entry hugetlb page_vma_mapped_walk() paths - removes unnecessary nesting.
- Add the missing page_vma_mapped_walk_done() in remove_migration_pte_hugetlb(), which leaked the huge-PTE lock. (syzbot ci, Sashiko)
- Fix handling of hwpoisoned subpages in the batched unmap. Read PageHWPoison() once and skip batching when mapped page is poisoned, and use same result for hwpoison handling. (Sashiko)
- Remove the pte_unused(pteval) handling from try_to_migrate_hugetlb_one() (Sashiko).
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Shivank Garg (3):
mm/rmap: factor out migration PTE construction
mm/rmap: split try_to_migrate_hugetlb_one() out of try_to_migrate_one() mm/rmap: batch the unmap of large folios in try_to_migrate_one()
mm/rmap.c | 434 +++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 277 insertions(+), 157 deletions(-)
---
base-commit: 518cbccb58e4ff8ce9d7ed609036c559dfdcf62e
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/mm/core.git for-next
change-id: 20260701-migrate-rmap-batch-8ecc04de1905
prerequisite-message-id: [email protected]
prerequisite-patch-id: 5dc23d00736b03a60a420bce56fdb59aabb57d97
prerequisite-patch-id: 87ecef5648d4102fd0ac1d2c1875aee3476635eb
prerequisite-patch-id: 43f01b72e0fdde751109e0f471c30b15d9f192ac
prerequisite-patch-id: 18bf3505aa2499f4e18446b4930e503d4085346d
prerequisite-patch-id: e79055c5dcb6fae5cb425feddec0e809959fd563
prerequisite-patch-id: 98bc99c6bc274594946d022b3998f5f1dc70012f
prerequisite-patch-id: dd35ec00b82bf235f2e41e286d16eab6a3fd1e9e
prerequisite-patch-id: d6f05c7d00e839fea93a5637b68348a1df345cc4
Best regards,
Signature · 1 line
Shivank Garg <[email protected]>
PATCH v3 1/3 mm/rmap: factor out migration PTE construction
try_to_migrate_one() constructs the migration PTE that replaces a present PTE or non-present swap PTE.
Factor it into make_migration_pte() which can be shared when the hugetlb migration path is separated.
No functional change intended.
--- mm/rmap.c | 65 +++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 27 deletions(-)
mm/rmap.c +38 -27
@@ -2517,6 +2517,42 @@ void try_to_unmap(struct folio *folio, enum ttu_flags flags)rmap_walk (folio, &rwc); } +/* Build the migration PTE that replaces pteval for a page. */ +static pte_t make_migration_pte (struct page *page,pte_t pteval, +bool writable,bool anon_exclusive) +{ +swp_entry_t entry; +pte_t swp_pte; + +if (writable) + entry =make_writable_migration_entry (page_to_pfn (page)); +else if (anon_exclusive) + entry =make_readable_exclusive_migration_entry ( +page_to_pfn (page)); +else + entry =make_readable_migration_entry (page_to_pfn (page)); + +if (likely (pte_present (pteval))) { +if (pte_young (pteval)) + entry =make_migration_entry_young (entry); +if (pte_dirty (pteval)) + entry =make_migration_entry_dirty (entry); + swp_pte =swp_entry_to_pte (entry); +if (pte_soft_dirty (pteval)) + swp_pte =pte_swp_mksoft_dirty (swp_pte); +if (pte_uffd (pteval)) + swp_pte =pte_swp_mkuffd (swp_pte); + }else { + swp_pte =swp_entry_to_pte (entry); +if (pte_swp_soft_dirty (pteval)) + swp_pte =pte_swp_mksoft_dirty (swp_pte); +if (pte_swp_uffd (pteval)) + swp_pte =pte_swp_mkuffd (swp_pte); + } + +return swp_pte; +} +/* * @arg: enum ttu_flags will be passed to this argument. *
@@ -2747,7 +2783,6 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,*/ dec_mm_counter (mm,mm_counter (folio)); }else { -swp_entry_t entry;pte_t swp_pte;/*
@@ -2789,32 +2824,8 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,* pte. do_swap_page() will wait until the migration * pte is removed and then restart fault handling. */ -if (writable) - entry =make_writable_migration_entry ( -page_to_pfn (subpage)); -else if (anon_exclusive) - entry =make_readable_exclusive_migration_entry ( -page_to_pfn (subpage)); -else - entry =make_readable_migration_entry ( -page_to_pfn (subpage)); -if (likely (pte_present (pteval))) { -if (pte_young (pteval)) - entry =make_migration_entry_young (entry); -if (pte_dirty (pteval)) - entry =make_migration_entry_dirty (entry); - swp_pte =swp_entry_to_pte (entry); -if (pte_soft_dirty (pteval)) - swp_pte =pte_swp_mksoft_dirty (swp_pte); -if (pte_uffd (pteval)) - swp_pte =pte_swp_mkuffd (swp_pte); - }else { - swp_pte =swp_entry_to_pte (entry); -if (pte_swp_soft_dirty (pteval)) - swp_pte =pte_swp_mksoft_dirty (swp_pte); -if (pte_swp_uffd (pteval)) - swp_pte =pte_swp_mkuffd (swp_pte); - } + swp_pte =make_migration_pte (subpage, pteval, + writable, anon_exclusive);if (folio_test_hugetlb (folio))set_huge_pte_at (mm, address, pvmw.pte, swp_pte, hsz);
Signature · 1 line
2.53.0
PATCH v3 2/3 mm/rmap: split try_to_migrate_hugetlb_one() out of try_to_migrate_one()
In reply to Shivank Gargtry_to_migrate_one() interleaves hugetlb special handling through the regular PTE/PMD migration path with folio_test_hugetlb() checks scattered across the walk. Simplify this by moving hugetlb handling into its own rmap_one callback, try_to_migrate_hugetlb_one().
Few minor changes:
- Since hugetlb uses separate counters, update_hiwater_rss() is not needed for hugetlb path. Also, use huge_pte_dirty instead of pte_dirty
- No PVMW_SYNC flag is needed for hugetlb
- hugetlb folio has single mapping per VMA, so no loop is needed.
- Convert VM_BUG_ON to VM_WARN_ON_ONCE and not use irrelevant WARN check.
--- mm/rmap.c | 251 +++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 144 insertions(+), 107 deletions(-)
mm/rmap.c +144 -107
@@ -2553,6 +2553,138 @@ static pte_t make_migration_pte(struct page *page, pte_t pteval,return swp_pte; } +/* +* Replace the hugetlb page table mapping for folio with migration entries. +* +* @arg: enum ttu_flags will be passed to this argument. +*/ +static bool try_to_migrate_hugetlb_one (struct folio *folio, +struct vm_area_struct *vma,unsigned long address,void *arg) +{ +const unsigned long hsz =huge_page_size (hstate_vma (vma)); +DEFINE_FOLIO_VMA_WALK (pvmw, folio, vma, address,0 ); +enum ttu_flags flags = (enum ttu_flags )(long )arg; +bool anon_exclusive, writable, ret = true; +bool anon =folio_test_anon (folio); +struct mm_struct *mm = vma->vm_mm; +struct mmu_notifier_range range; +unsigned long pfn; +struct page *page; +pte_t pteval; + + range.end =vma_address_end (&pvmw); +mmu_notifier_range_init (&range,MMU_NOTIFY_CLEAR ,0 , vma->vm_mm, + address, range.end); +/* If sharing is possible, start and end will be adjusted accordingly. */ +adjust_range_if_pmd_sharing_possible (vma, &range.start, &range.end); +mmu_notifier_invalidate_range_start (&range); + +/* There is only a single mapping in a VMA. */ +if (!page_vma_mapped_walk (&pvmw)) + goto range_end; + + address = pvmw.address; + pteval =huge_ptep_get (mm, address, pvmw.pte); +if (likely (pte_present (pteval))) + pfn =pte_pfn (pteval); +else + pfn =softleaf_to_pfn (softleaf_from_pte (pteval)); + + page =folio_page (folio, pfn -folio_pfn (folio)); + anon_exclusive = anon &&PageAnonExclusive (page); + +/* +* huge_pmd_unshare may unmap an entire PMD page. There is no way of +* knowing exactly which PMDs may be cached for this mm, so we must flush +* them all. start/end were already adjusted above to cover this range. +*/ +flush_cache_range (vma, range.start, range.end); + +/* +* To call huge_pmd_unshare, i_mmap_rwsem must be held in write mode. +* Caller needs to explicitly do this outside rmap routines. We also must +* hold hugetlb vma_lock in write mode. Lock order dictates acquiring +* vma_lock BEFORE i_mmap_rwsem. We can only try lock here and fail if +* unsuccessful. +*/ +if (!anon) { +struct mmu_gather tlb; + +VM_WARN_ON_ONCE (!(flags &TTU_RMAP_LOCKED )); +if (!hugetlb_vma_trylock_write (vma)) { + ret = false; + goto walk_done; + } + +tlb_gather_mmu_vma (&tlb, vma); +if (huge_pmd_unshare (&tlb, vma, address, pvmw.pte)) { +hugetlb_vma_unlock_write (vma); +huge_pmd_unshare_flush (&tlb, vma); +tlb_finish_mmu (&tlb); +/* +* The PMD table was unmapped, +* consequently unmapping the folio. +*/ + goto walk_done; + } +hugetlb_vma_unlock_write (vma); +tlb_finish_mmu (&tlb); + } + +/* Nuke the hugetlb page table entry */ + pteval =huge_ptep_clear_flush (vma, address, pvmw.pte); +if (huge_pte_dirty (pteval)) +folio_mark_dirty (folio); + writable =pte_write (pteval); + +VM_WARN_ON_FOLIO (writable && anon && !anon_exclusive, folio); + +if (PageHWPoison (page)) { + pteval =swp_entry_to_pte (make_hwpoison_entry (page)); +hugetlb_count_sub (folio_nr_pages (folio), mm); +set_huge_pte_at (mm, address, pvmw.pte, pteval, hsz); + }else { +pte_t swp_pte; + +/* +* arch_unmap_one() is expected to be a NOP on architectures +* where we could have PFN swap PTEs, so we'll not check/care. +*/ +if (arch_unmap_one (mm, vma, address, pteval) <0 ) { +set_huge_pte_at (mm, address, pvmw.pte, pteval, hsz); + ret = false; + goto walk_done; + } + +/* See folio_try_share_anon_rmap_pte(): clear PTE first. */ +if (anon_exclusive &&hugetlb_try_share_anon_rmap (folio)) { +set_huge_pte_at (mm, address, pvmw.pte, pteval, hsz); + ret = false; + goto walk_done; + } + +/* +* Store the pfn of the page in a special migration pte. A +* hugetlb fault waits for migration to complete before retrying. +*/ + swp_pte =make_migration_pte (page, pteval, writable, anon_exclusive); +set_huge_pte_at (mm, address, pvmw.pte, swp_pte, hsz); +trace_set_migration_pte (address,pte_val (swp_pte), +folio_order (folio)); + } + +hugetlb_remove_rmap (folio); +if (vma_test_any_mask (vma,VMA_LOCKED_MASK )) +mlock_drain_local (); +folio_put (folio); +walk_done: +page_vma_mapped_walk_done (&pvmw); +range_end: +mmu_notifier_invalidate_range_end (&range); + +return ret; +} +/* * @arg: enum ttu_flags will be passed to this argument. *
@@ -2570,7 +2702,6 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,struct mmu_notifier_range range;enum ttu_flags flags = (enum ttu_flags )(long )arg;unsigned long pfn; -unsigned long hsz =0 ;/* * When racing against e.g. zap_pte_range() on another cpu,
@@ -2583,26 +2714,13 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,/* * For THP, we have to assume the worse case ie pmd for invalidation. -* For hugetlb, it could be much worse if we need to do pud -* invalidation in the case of pmd sharing. * * Note that the page can not be free in this function as call of -* try_to_unmap() must hold a reference on the page. +* try_to_migrate() must hold a reference on the page. */ range.end =vma_address_end (&pvmw);mmu_notifier_range_init (&range,MMU_NOTIFY_CLEAR ,0 , vma->vm_mm, address, range.end); -if (folio_test_hugetlb (folio)) { -/* -* If sharing is possible, start and end will be adjusted -* accordingly. -*/ -adjust_range_if_pmd_sharing_possible (vma, &range.start, - &range.end); - -/* We need the huge page size for set_huge_pte_at() */ - hsz =huge_page_size (hstate_vma (vma)); - }mmu_notifier_invalidate_range_start (&range);while (page_vma_mapped_walk (&pvmw)) {
@@ -2649,10 +2767,7 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,VM_BUG_ON_FOLIO (!pvmw.pte, folio); address = pvmw.address; -if (folio_test_hugetlb (folio)) - pteval =huge_ptep_get (mm, address, pvmw.pte); -else - pteval =ptep_get (pvmw.pte); + pteval =ptep_get (pvmw.pte);if (likely (pte_present (pteval))) { pfn =pte_pfn (pteval); }else {
@@ -2663,66 +2778,13 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,const softleaf_t entry =softleaf_from_pte (pteval); pfn =softleaf_to_pfn (entry); -VM_WARN_ON_FOLIO (folio_test_hugetlb (folio), folio); } subpage =folio_page (folio, pfn -folio_pfn (folio)); anon_exclusive =folio_test_anon (folio) &&PageAnonExclusive (subpage); -if (folio_test_hugetlb (folio)) { -bool anon =folio_test_anon (folio); - -/* -* huge_pmd_unshare may unmap an entire PMD page. -* There is no way of knowing exactly which PMDs may -* be cached for this mm, so we must flush them all. -* start/end were already adjusted above to cover this -* range. -*/ -flush_cache_range (vma, range.start, range.end); - -/* -* To call huge_pmd_unshare, i_mmap_rwsem must be -* held in write mode. Caller needs to explicitly -* do this outside rmap routines. -* -* We also must hold hugetlb vma_lock in write mode. -* Lock order dictates acquiring vma_lock BEFORE -* i_mmap_rwsem. We can only try lock here and -* fail if unsuccessful. -*/ -if (!anon) { -struct mmu_gather tlb; - -VM_BUG_ON (!(flags &TTU_RMAP_LOCKED )); -if (!hugetlb_vma_trylock_write (vma)) { -page_vma_mapped_walk_done (&pvmw); - ret = false; -break ; - } - -tlb_gather_mmu_vma (&tlb, vma); -if (huge_pmd_unshare (&tlb, vma, address, pvmw.pte)) { -hugetlb_vma_unlock_write (vma); -huge_pmd_unshare_flush (&tlb, vma); -tlb_finish_mmu (&tlb); -/* -* The PMD table was unmapped, -* consequently unmapping the folio. -*/ -page_vma_mapped_walk_done (&pvmw); -break ; - } -hugetlb_vma_unlock_write (vma); -tlb_finish_mmu (&tlb); - } -/* Nuke the hugetlb page table entry */ - pteval =huge_ptep_clear_flush (vma, address, pvmw.pte); -if (pte_dirty (pteval)) -folio_mark_dirty (folio); - writable =pte_write (pteval); - }else if (likely (pte_present (pteval))) { +if (likely (pte_present (pteval))) {flush_cache_page (vma, address, pfn);/* Nuke the page table entry. */ if (should_defer_flush (mm, flags)) {
@@ -2761,14 +2823,8 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,VM_WARN_ON_FOLIO (folio_is_device_private (folio), folio); pteval =swp_entry_to_pte (make_hwpoison_entry (subpage)); -if (folio_test_hugetlb (folio)) { -hugetlb_count_sub (folio_nr_pages (folio), mm); -set_huge_pte_at (mm, address, pvmw.pte, pteval, - hsz); - }else { -dec_mm_counter (mm,mm_counter (folio)); -set_pte_at (mm, address, pvmw.pte, pteval); - } +dec_mm_counter (mm,mm_counter (folio)); +set_pte_at (mm, address, pvmw.pte, pteval); }else if (likely (pte_present (pteval)) &&pte_unused (pteval) && !userfaultfd_armed (vma)) {/*
@@ -2791,28 +2847,15 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,* so we'll not check/care. */ if (arch_unmap_one (mm, vma, address, pteval) <0 ) { -if (folio_test_hugetlb (folio)) -set_huge_pte_at (mm, address, pvmw.pte, - pteval, hsz); -else -set_pte_at (mm, address, pvmw.pte, pteval); +set_pte_at (mm, address, pvmw.pte, pteval); ret = false;page_vma_mapped_walk_done (&pvmw);break ; }/* See folio_try_share_anon_rmap_pte(): clear PTE first. */ -if (folio_test_hugetlb (folio)) { -if (anon_exclusive && -hugetlb_try_share_anon_rmap (folio)) { -set_huge_pte_at (mm, address, pvmw.pte, - pteval, hsz); - ret = false; -page_vma_mapped_walk_done (&pvmw); -break ; - } - }else if (anon_exclusive && -folio_try_share_anon_rmap_pte (folio, subpage)) { +if (anon_exclusive && +folio_try_share_anon_rmap_pte (folio, subpage)) {set_pte_at (mm, address, pvmw.pte, pteval); ret = false;page_vma_mapped_walk_done (&pvmw);
@@ -2826,11 +2869,7 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,*/ swp_pte =make_migration_pte (subpage, pteval, writable, anon_exclusive); -if (folio_test_hugetlb (folio)) -set_huge_pte_at (mm, address, pvmw.pte, swp_pte, - hsz); -else -set_pte_at (mm, address, pvmw.pte, swp_pte); +set_pte_at (mm, address, pvmw.pte, swp_pte);trace_set_migration_pte (address,pte_val (swp_pte),folio_order (folio));/*
@@ -2839,10 +2878,7 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,*/ } -if (unlikely (folio_test_hugetlb (folio))) -hugetlb_remove_rmap (folio); -else -folio_remove_rmap_pte (folio, subpage, vma); +folio_remove_rmap_pte (folio, subpage, vma);if (vma_test_any_mask (vma,VMA_LOCKED_MASK ))mlock_drain_local ();folio_put (folio);
@@ -2864,7 +2900,8 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,void try_to_migrate (struct folio *folio,enum ttu_flags flags) {struct rmap_walk_control rwc = { - .rmap_one = try_to_migrate_one, + .rmap_one =folio_test_hugetlb (folio) ? + try_to_migrate_hugetlb_one : try_to_migrate_one, .arg = (void *)flags, .done = folio_not_mapped, .anon_lock = folio_lock_anon_vma_read,
Signature · 1 line
2.53.0
PATCH v3 3/3 mm/rmap: batch the unmap of large folios in try_to_migrate_one()
In reply to Shivank Gargtry_to_migrate_one() converts present PTEs to migration entries one at a time. For a PTE-mapped large folio, this repeats calls to ptep clear+flush, the migration entry build and set, folio_remove_rmap_pte() and folio_put(), each re-entering page_vma_mapped_walk() once per base page (256 times for 1M folio).
Mirror try_to_unmap_one() to introduce folio_migrate_pte_batch() to detect eligible batch for PTEs mapping consecutive subpages of a large folio, and convert the whole batch in one shot using the batched helpers.
Batching also avoids a spurious deferred split. Unmapping a PTE-mapped anonymous large folio one PTE at a time leaves it partially mapped between iterations, so __folio_remove_rmap() queues it for deferred split even though the folio is about to be fully unmapped. Removing the rmap for the whole batch at once skips that state when one batch covers the mapping.
A side-effect of this change is trace_set_migration_pte() will record one event per batched run instead of earlier behavior of one per base page.
--- mm/rmap.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 101 insertions(+), 29 deletions(-)
mm/rmap.c +101 -29
@@ -2685,6 +2685,59 @@ static bool try_to_migrate_hugetlb_one(struct folio *folio,return ret; } +static inline unsigned int folio_migrate_pte_batch (struct folio *folio, +struct page_vma_mapped_walk *pvmw,pte_t pte, +struct page *subpage) +{ +unsigned long end_addr, addr = pvmw->address; +struct vm_area_struct *vma = pvmw->vma; +unsigned int max_nr, nr, i; + +#ifdef __HAVE_ARCH_UNMAP_ONE +/* Cannot batch unmap if arch_unmap_one() is defined. */ +return 1 ; +#endif + +if (!folio_test_large (folio)) +return 1 ; +if (folio_is_zone_device (folio) ||folio_test_has_hwpoisoned (folio)) +return 1 ; +if (pte_unused (pte)) +return 1 ; + +/* We may only batch within a single VMA and a single page table. */ + end_addr =pte_table_clamped_end (addr, vma->vm_end); + max_nr = (end_addr - addr) >>PAGE_SHIFT ; +/* +* If unmap fails, we need to restore the ptes. To avoid accidentally +* upgrading write permissions for ptes that were not originally writable, +* and to avoid losing the soft-dirty bit, use the appropriate FPB flags. +*/ + nr =folio_pte_batch_flags (folio, vma, pvmw->pte, &pte, max_nr, +FPB_RESPECT_WRITE |FPB_RESPECT_SOFT_DIRTY ); + +/* Limit possible batch count to a uniform PageAnonExclusive value */ +if (folio_test_anon (folio)) { +bool exclusive; + + nr =page_anon_exclusive_batch (0 , nr, subpage, &exclusive); + } + +/* +* memory_failure() sets PG_hwpoison without holding the folio lock, so +* folio_test_has_hwpoisoned() above can still be false while a tail +* page is poisoned. +*/ +for (i =1 ; i < nr; i++) { +if (PageHWPoison (subpage + i)) { + nr = i; +break ; + } + } + +return nr; +} +/* * @arg: enum ttu_flags will be passed to this argument. *
@@ -2696,12 +2749,12 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, {struct mm_struct *mm = vma->vm_mm;DEFINE_FOLIO_VMA_WALK (pvmw, folio, vma, address,0 ); -bool anon_exclusive, writable, ret = true; +bool anon_exclusive, hwpoison, writable, ret = true;pte_t pteval;struct page *subpage;struct mmu_notifier_range range;enum ttu_flags flags = (enum ttu_flags )(long )arg; -unsigned long pfn; +unsigned long pfn, end_addr, nr_pages;/* * When racing against e.g. zap_pte_range() on another cpu,
@@ -2754,11 +2807,8 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,VM_BUG_ON_FOLIO (folio_test_hugetlb (folio) || !folio_test_pmd_mappable (folio), folio); -if (set_pmd_migration_entry (&pvmw, subpage)) { - ret = false; -page_vma_mapped_walk_done (&pvmw); -break ; - } +if (set_pmd_migration_entry (&pvmw, subpage)) + goto walk_abort;continue ;#endif }
@@ -2783,10 +2833,24 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, subpage =folio_page (folio, pfn -folio_pfn (folio)); anon_exclusive =folio_test_anon (folio) &&PageAnonExclusive (subpage); +/* +* memory_failure() can set PageHWPoison concurrently without holding +* the folio lock. Snapshot the flag here to decide whether to batch +* PTEs or install hwpoison entry. +*/ + hwpoison =PageHWPoison (subpage); + nr_pages =1 ;if (likely (pte_present (pteval))) { -flush_cache_page (vma, address, pfn); -/* Nuke the page table entry. */ +if (!hwpoison) + nr_pages =folio_migrate_pte_batch (folio, &pvmw, + pteval, subpage); + + end_addr = address + nr_pages *PAGE_SIZE ; +flush_cache_range (vma, address, end_addr); + +/* Nuke the page table entries. */ + pteval =get_and_clear_ptes (mm, address, pvmw.pte, nr_pages);if (should_defer_flush (mm, flags)) {/* * We clear the PTE but do not flush so potentially
@@ -2796,11 +2860,9 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,* transition on a cached TLB entry is written through * and traps if the PTE is unmapped. */ - pteval =ptep_get_and_clear (mm, address, pvmw.pte); - -set_tlb_ubc_flush_pending (mm, pteval, address, address +PAGE_SIZE ); +set_tlb_ubc_flush_pending (mm, pteval, address, end_addr); }else { - pteval =ptep_clear_flush (vma, address, pvmw.pte); +flush_tlb_range (vma, address, end_addr); }if (pte_dirty (pteval))folio_mark_dirty (folio);
@@ -2819,7 +2881,8 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,/* Update high watermark before we lower rss */ update_hiwater_rss (mm); -if (PageHWPoison (subpage)) { +if (hwpoison) { +VM_WARN_ON_ONCE (nr_pages !=1 );VM_WARN_ON_FOLIO (folio_is_device_private (folio), folio); pteval =swp_entry_to_pte (make_hwpoison_entry (subpage));
@@ -2847,19 +2910,15 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,* so we'll not check/care. */ if (arch_unmap_one (mm, vma, address, pteval) <0 ) { -set_pte_at (mm, address, pvmw.pte, pteval); - ret = false; -page_vma_mapped_walk_done (&pvmw); -break ; +set_ptes (mm, address, pvmw.pte, pteval, nr_pages); + goto walk_abort; } -/* See folio_try_share_anon_rmap_pte(): clear PTE first. */ +/* See folio_try_share_anon_rmap_ptes(): clear PTE first. */ if (anon_exclusive && -folio_try_share_anon_rmap_pte (folio, subpage)) { -set_pte_at (mm, address, pvmw.pte, pteval); - ret = false; -page_vma_mapped_walk_done (&pvmw); -break ; +folio_try_share_anon_rmap_ptes (folio, subpage, nr_pages)) { +set_ptes (mm, address, pvmw.pte, pteval, nr_pages); + goto walk_abort; }/*
@@ -2869,7 +2928,10 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,*/ swp_pte =make_migration_pte (subpage, pteval, writable, anon_exclusive); -set_pte_at (mm, address, pvmw.pte, swp_pte); + +/* Set nr_pages migration entries, advancing the PFN. */ +set_softleaf_ptes (mm, address, pvmw.pte, swp_pte, nr_pages); +trace_set_migration_pte (address,pte_val (swp_pte),folio_order (folio));/*
@@ -2878,10 +2940,20 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,*/ } -folio_remove_rmap_pte (folio, subpage, vma); -if (vma_test_any_mask (vma,VMA_LOCKED_MASK )) -mlock_drain_local (); -folio_put (folio); +finish_folio_unmap_batch (vma, folio, subpage, nr_pages); + +/* +* If we batched the entire folio, there is nothing left to +* walk; stop right here. +*/ +if (nr_pages ==folio_nr_pages (folio)) + goto walk_done; +continue ; +walk_abort: + ret = false; +walk_done: +page_vma_mapped_walk_done (&pvmw); +break ; }mmu_notifier_invalidate_range_end (&range);
Signature · 1 line
2.53.0