Re: [PATCH v4 2/3] khugepaged: Optimize __collapse_huge_page_copy_succeeded() by PTE batching

From: Lorenzo Stoakes
Date: Thu Jul 24 2025 - 14:02:32 EST


On Thu, Jul 24, 2025 at 07:57:22PM +0200, David Hildenbrand wrote:
>
> > > + if (folio_test_large(src)) {
> > > + unsigned int max_nr_ptes = (end - address) >> PAGE_SHIFT;
> > > +
> > > + nr_ptes = folio_pte_batch(src, _pte, pteval, max_nr_ptes);
> > > + } else {
> > > release_pte_folio(src);
> > > + }
> > > +
> > > /*
> > > * ptl mostly unnecessary, but preempt has to
> > > * be disabled to update the per-cpu stats
> > > * inside folio_remove_rmap_pte().
> > > */
> > > spin_lock(ptl);
> > > - ptep_clear(vma->vm_mm, address, _pte);
> > > - folio_remove_rmap_pte(src, src_page, vma);
> > > + clear_ptes(vma->vm_mm, address, _pte, nr_ptes);
> > > + folio_remove_rmap_ptes(src, src_page, nr_ptes, vma);
> > > spin_unlock(ptl);
> > > - free_folio_and_swap_cache(src);
> > > + free_swap_cache(src);
> > > + folio_put_refs(src, nr_ptes);
> >
> > Hm one thing here though is the free_folio_and_swap_cache() does:
> >
> > free_swap_cache(folio);
> > if (!is_huge_zero_folio(folio))
> > folio_put(folio);
> >
> > Whereas here you unconditionally reduce the reference count. Might this
> > cause issues with the shrinker version of the huge zero folio?
> >
> > Should this be:
> >
> > if (!is_huge_zero_folio(src))
> > folio_put_refs(src, nr_ptes);
> >
> > Or do we otherwise avoid issues with this?
>
> (resending my reply)
>
> The huge zero folio is never PTE-mapped.

OK fine, as mentioned off-list I hate this kind of 'implicit' knowledge, and you
pointed out that really we should be using vm_normal_page() or equivalent in
this code. One to address at some point :)

Anyway with this concern addressed, the patch is fine, will send tag...

>
> --
> Cheers,
>
> David / dhildenb
>