Re: [PATCH v3 2/3] mm/rmap: split try_to_migrate_hugetlb_one() out of try_to_migrate_one()
From: Garg, Shivank
Date: Wed Dec 02 2026 - 10:28:50 EST
On Wed, 2026-12-02 at 16:18 +0100, David Hildenbrand (Arm) wrote:
> On 12/2/26 15:17, Garg, Shivank wrote:
> > On Tue, 2026-12-01 at 12:03 +0100, David Hildenbrand (Arm) wrote:
> > > On 11/26/26 10:53, Shivank Garg wrote:
> > > > try_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.
> > > >
> > > > Signed-off-by: Shivank Garg <shivankg@xxxxxxx>
> > > > ---
> > > > mm/rmap.c | 251 +++++++++++++++++++++++++++++++++++---------------------------
> > > > 1 file changed, 144 insertions(+), 107 deletions(-)
> > > >
> > > > diff --git a/mm/rmap.c b/mm/rmap.c
> > > > index 304581856d67..fe87df06fa06 100644
> > > > --- a/mm/rmap.c
> > > > +++ b/mm/rmap.c
> > > > @@ -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));
> > >
> > > Do we have such a case for hugetlb?
> > >
> > >
> > > > +
> > > > + page = folio_page(folio, pfn - folio_pfn(folio));
> > >
> > > How can we ever not be at the head page.
> > >
> > >
> > > Likely, most of this sould be very similar to what we have in
> > > try_to_unmap_poisoned_hugetlb_one().
> > >
> >
> > Agreed. So I can directly use folio_page(folio, 0) or &folio->page.
> >
> > try_to_unmap_poisoned_hugetlb_one() covers both case with this
> > VM_WARN_ON_ONCE(!pte_present(pteval));
> > VM_WARN_ON_ONCE(pte_pfn(pteval) != folio_pfn(folio));
> > >
> > > Which raises the question if we can unify some stuff there instead? It's *a lot*
> > > of code duplication.
> > >
> > > Just need to teach try_to_unmap_poisoned_hugetlb_one() to insert migration
> > > entries when asked.
> > >
> > > Sounds reasonable?
> >
> > yes, makes sense. I'll do this in V4.
> > Thanks for suggestion.
>
> If there are ways to have two functions but have them reuse more code, that
> would also work. But likely there is no easy way to get that done.
I'm thinking of something like having:
try_to_unmap_hugetlb_one(..., bool is_migration) {
// all duplicate code
if (migration) {
// migration related stuff
} else {
}
}
then just call them like:
try_to_migrate_hugetlb_one() {
try_to_unmap_hugetlb_one(is_migration=true)
}
try_to_unmap_poisoned_hugetlb_one {
try_to_unmap_hugetlb_one(is_migration=false)
}
This would remove the duplicate code and also easy.
Thanks,
Shivank